rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

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

Open T-Dark0 opened this issue 4 years ago • 4 comments

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

  • cargo clippy -V: clippy 0.1.54 (1c6868aa 2021-05-27)
  • rustc -Vv:
rustc 1.54.0-nightly (1c6868aa2 2021-05-27)
binary: rustc
commit-hash: 1c6868aa21981b37cbd3fc95828ee3b0ac22d494
commit-date: 2021-05-27
host: x86_64-pc-windows-msvc
release: 1.54.0-nightly
LLVM version: 12.0.1

T-Dark0 avatar May 28 '21 12:05 T-Dark0

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.

camsteffen avatar May 28 '21 13:05 camsteffen

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

danieleades avatar Jan 04 '22 07:01 danieleades

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
$ 

jimblandy avatar Jun 21 '22 00:06 jimblandy

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

dcecile avatar Aug 11 '24 19:08 dcecile