rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.46k stars 1.54k forks source link

`allow` only works with some lints if and only if they're allowed both in the crate root and in the desired module #7290

Open T-Dark0 opened 3 years ago

T-Dark0 commented 3 years ago

I tried this code:

// in main.rs
#![warn(clippy::all, clippy::pedantic)]

mod foo;

fn main() {
    foo::foo();
}

//in foo.rs
#![allow(clippy::single_component_path_imports)]

pub fn foo() {
    usable!();
}

macro_rules! usable {
    () => {};
}
use usable;

I expected to see this happen: The lint is silenced for the module

Instead, this happened: The lint was not silenced, and fired anyway in the output of cargo clippy

It may be worth mentioning that the lint is silenced if and only if the allow exists both in the crate root and in the module

This bug also occurs with other lints, but does not occur with all lints: I've been able to cause it with items_after_statements, but not with enum_glob_use or match_same_arms. In the case of the latter two, the lint was silenced for the module without the need to allow it in the crate root as well.

Meta

camsteffen commented 3 years ago

Thanks! Any lints other than single_component_path_imports and items_after_statements not working? This bug will have to be fixed for individual lints.

danieleades commented 2 years ago

now you mention it, i've seen a similar problem with clippy::print_stderr, where the behaviour is different whether the lint is used in the crate root, or in a module.

see https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/172#note_2767740

jimblandy commented 2 years ago

This makes Clippy hard to use with generated files, like lalrpop output, because the offending patterns may occur hundreds of times, and the legitimate warnings are hard to find.

Here's another case, with a single source file:

mod foo {
    #![allow(clippy::single_component_path_imports)]
    use regex;

    pub fn foo() {
        regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
    }
}

fn main() {
    foo::foo();
}

Output:

$ cargo clippy --version
clippy 0.1.61 (fe5b13d 2022-05-18)
$ cargo clippy
warning: this import is redundant
 --> src/main.rs:3:5
  |
3 |     use regex;
  |     ^^^^^^^^^^ help: remove it entirely
  |
  = note: `#[warn(clippy::single_component_path_imports)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports

warning: `clippy-bug` (bin "clippy-bug") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
$ 
dcecile commented 3 months ago

Finding that single_component_path_imports = "allow" (lint configuration in Cargo.toml) also doesn't seem to have an effect.

Another way to trigger the effect is to use the command line option: -A clippy::single_component_path_imports