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

Extend maybe_misused_cfg to cover #[cfg(tests)] #11240

Closed
SabrinaJewson opened this issue Jul 27, 2023 · 1 comment · Fixed by #11821
Closed

Extend maybe_misused_cfg to cover #[cfg(tests)] #11240

SabrinaJewson opened this issue Jul 27, 2023 · 1 comment · Fixed by #11821
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@SabrinaJewson
Copy link
Contributor

Description

An easy misspelling to make is to write something like:

pub fn add(left: usize, right: usize) -> usize {
    left + right
}

#[cfg(tests)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

This will not result in any warnings or errors currently, but will simply not run any of the tests. Clippy should instead output a warning:

warning: test may misspelled as tests
 --> src/lib.rs:5:7
  |
5 | #[cfg(tests)]
  |       ^^^^^ help: use: `test`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/
index.html#maybe_misused_cfg
  = note: `#[warn(clippy::maybe_misused_cfg)]` on by default

Version

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-unknown-linux-gnu
release: 1.71.0
LLVM version: 16.0.5

Additional Labels

@rustbot label +C-enhancement

@rustbot rustbot added the C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages label Jul 27, 2023
@SabrinaJewson
Copy link
Contributor Author

Another thing to add to this, since I just saw someone accidentally do it: #[cfg(Test)] and other alternately-cased misspellings.

bors added a commit that referenced this issue Nov 17, 2023
Extend `maybe_misused_cfg` lint over `cfg(test)`

Fixes #11240.

One thought I had is that we could use the levenshtein distance (of 1) to ensure this is indeed `test` that was targeted. But maybe it's overkill, not sure.

r? `@blyxyas`
@bors bors closed this as completed in 31e38fe Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants