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

blacklisted_name should not be triggered in tests #8986

Open
tamaroning opened this issue Jun 11, 2022 · 5 comments
Open

blacklisted_name should not be triggered in tests #8986

tamaroning opened this issue Jun 11, 2022 · 5 comments
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@tamaroning
Copy link
Contributor

tamaroning commented Jun 11, 2022

Summary

blacklisted_name should toralate use of such name as foo.
As far as I see the source code, it tries to allow foo in tests, but it doesn't.

Similar to #8758

Lint Name

blacklisted_name

Reproducer

I tried this code:

#![warn(clippy::blacklisted_name)]
fn main() {}

#[cfg(test)]
mod abc {
    fn _abc() {
        let foo = ();
        dbg!(foo);
    }
}

#[test]
fn bcd() {
    let foo = ();
    dbg!(foo);
}

fn _test() {
    let foo = ();
    dbg!(foo);
}

I saw this happen:

warning: use of a blacklisted/placeholder name `foo`
  --> src\main.rs:19:9
   |
19 |     let foo = ();
   |         ^^^
   |
note: the lint level is defined here
  --> src\main.rs:1:9
   |
1  | #![warn(clippy::blacklisted_name)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name

warning: use of a blacklisted/placeholder name `foo`
 --> src\main.rs:7:13
  |
7 |         let foo = ();
  |             ^^^
  |
note: the lint level is defined here
 --> src\main.rs:1:9
  |
1 | #![warn(clippy::blacklisted_name)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name

warning: use of a blacklisted/placeholder name `foo`
  --> src\main.rs:14:9
   |
14 |     let foo = ();
   |         ^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name

warning: use of a blacklisted/placeholder name `foo`
  --> src\main.rs:19:9
   |
19 |     let foo = ();
   |         ^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name

I expected to see this happen:
No warning

Version

rustc 1.61.0-nightly (458262b13 2022-03-09)
binary: rustc
commit-hash: 458262b1315e0de7be940fe95e111bb045e4a2a4
commit-date: 2022-03-09
host: x86_64-pc-windows-msvc
release: 1.61.0-nightly
LLVM version: 14.0.0

Additional Labels

@rustbot label +E-easy

@tamaroning tamaroning added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jun 11, 2022
@tamaroning tamaroning changed the title blacklisted_name should not tbe triggered in tests blacklisted_name should not be triggered in tests Jun 11, 2022
@kyoto7250
Copy link
Contributor

kyoto7250 commented Jun 21, 2022

@tamaroning

Hello.

I tried your code, but the following two cases cannot be reproduced. (rust playrground)

(If the verification method is incorrect, please let me know.)

#![warn(clippy::blacklisted_name)]
fn main() {}

#[cfg(test)]
mod abc {
    fn _abc() {
        let foo = ();
        dbg!(foo);
    }
}

#[test]
fn bcd() {
    let foo = ();
    dbg!(foo);
}

last case, I think we should not lint.
They should be ignored in test cases only.

#[cfg(test)] and #[test] are codes that are ignored except when the test is run, but I don't think the name fn test has such a restriction.

@tamaroning
Copy link
Contributor Author

@kyoto7250

I tried your code, but the following two cases cannot be reproduced.

Playground seems not to pass --tests flag to the compiler so I ran clippy in local enviroment.

..., but I don't think the name fn test has such a restriction.

Yes. I reconsider this and agree with you. But the lint has been fixed in PR #7379 in order to trigger on fn test.

@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label Jun 22, 2022
@kyoto7250
Copy link
Contributor

kyoto7250 commented Jun 22, 2022

@tamaroning

Playground seems not to pass --tests flag to the compiler so I ran clippy in local enviroment.

I can reproduce now, thanks!

It doesn't seem urgent, so I hope someone interested will do it.

@yerke
Copy link
Contributor

yerke commented Jul 17, 2022

@rustbot claim

@yerke
Copy link
Contributor

yerke commented Jul 24, 2022

I think the lint is working as intended right now.

For the following input:

#![warn(clippy::blacklisted_name)]
fn main() {}

#[cfg(test)]
mod abc {
    fn _abc() {
        let foo = ();
        dbg!(foo);
    }
}

#[test]
fn bcd() {
    let foo = ();
    dbg!(foo);
}

fn _test() {
    let foo = ();
    dbg!(foo);
}

mod tests {
    fn _mf() {
        let foo = ();
        dbg!(foo);
    }
}

I get only 1 warning:

warning: use of a blacklisted/placeholder name `foo`
  --> src/main.rs:19:9
   |
19 |     let foo = ();
   |         ^^^
   |
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::blacklisted_name)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name

warning: `clippy-example-1` (bin "clippy-example-1") generated 1 warning

Which is expected, since the function _test is a standalone function and is not part of any tests.
mod tests is ignored, since it contains tests in it.

The is_test_module_or_function function this lint relies on seems to work according to its description:

/// Checks whether item either has `test` attribute applied, or
/// is a module with `test` in its name.
///
/// Note: Add `// compile-flags: --test` to UI tests with a `#[test]` function
pub fn is_test_module_or_function(tcx: TyCtxt<'_>, item: &Item<'_>) -> bool {
is_in_test_function(tcx, item.hir_id())
|| matches!(item.kind, ItemKind::Mod(..))
&& item.ident.name.as_str().split('_').any(|a| a == "test" || a == "tests")
}

Does my reasoning make sense or not?

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 good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

4 participants