Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

too_many_lines not triggered on functions with #[tracing::instrument] #13753

Open
letkemann opened this issue Nov 29, 2024 · 1 comment
Open
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@letkemann
Copy link

Summary

'too_many_lines' is not trigfgered on functions with #[tracing::instrument].
Other lints seem to work insude the affected functions.

Lint Name

too_many_lines

Reproducer

I tried this code: (with too-many-lines-threshold = 2)

#![warn(clippy::too_many_lines)]
#[allow(unused)]

fn long_function_1() {
    do_something("fn 1.1");
    do_something("fn 1.2");
    do_something("fn 1.3");
}

#[tracing::instrument]
fn long_function_2() {
    do_something("fn 2.1");
    do_something("fn 2.2");
    do_something("fn 2.3");
}

fn do_something(_msg:&str){
    todo!()
}

I expected to see this happen:
Expected warnings on both functions, long_function_1() and long_function_2()

Instead, this happened:
Received only a single warning on long_function_1()

cargo clippy -v
       Fresh unicode-ident v1.0.14
       Fresh proc-macro2 v1.0.92
       Fresh quote v1.0.37
       Fresh syn v2.0.89
       Fresh once_cell v1.20.2
       Fresh tracing-core v0.1.33
       Fresh tracing-attributes v0.1.28
       Fresh pin-project-lite v0.2.15
       Fresh tracing v0.1.41
       Fresh clippy-too-may-lines v0.1.0 (/home/alex/devel/clippy-too-may-lines)
warning: this function has too many lines (3/2)
 --> src/lib.rs:4:1
  |
4 | / fn long_function_1() {
5 | |     do_something("fn 1.1");
6 | |     do_something("fn 1.2");
7 | |     do_something("fn 1.3");
8 | | }
  | |_^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_lines
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::too_many_lines)]
  |         ^^^^^^^^^^^^^^^^^^^^^^

warning: `clippy-too-may-lines` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s

Version

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1
@letkemann letkemann added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Nov 29, 2024
@DylanBulfin
Copy link

This lint runs after macro expansion, and so code associated with an external macro is ignored. The clippy book has a section all about this but the basic idea is that by the time this function is linted it is filled with potentially hundreds of lines of auto-generated code the user has no control over. To avoid users having to manually disable lints like this for methods with external attributes, there's an explicit check here:

if matches!(kind, FnKind::Closure) || in_external_macro(cx.sess(), span) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

2 participants