Skip to content

Commit 31e38fe

Browse files
committed
Auto merge of rust-lang#11821 - GuillaumeGomez:misspelled-cfg, r=blyxyas
Extend `maybe_misused_cfg` lint over `cfg(test)` Fixes rust-lang#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. changelog: [`maybe_misused_cfg`]: Extend lint over `cfg(test)` r? `@blyxyas`
2 parents 25f4f85 + f08037c commit 31e38fe

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

clippy_lints/src/attrs.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -405,20 +405,26 @@ declare_clippy_lint! {
405405
/// Checks for `#[cfg(features = "...")]` and suggests to replace it with
406406
/// `#[cfg(feature = "...")]`.
407407
///
408+
/// It also checks if `cfg(test)` was misspelled.
409+
///
408410
/// ### Why is this bad?
409-
/// Misspelling `feature` as `features` can be sometimes hard to spot. It
411+
/// Misspelling `feature` as `features` or `test` as `tests` can be sometimes hard to spot. It
410412
/// may cause conditional compilation not work quietly.
411413
///
412414
/// ### Example
413415
/// ```no_run
414416
/// #[cfg(features = "some-feature")]
415417
/// fn conditional() { }
418+
/// #[cfg(tests)]
419+
/// mod tests { }
416420
/// ```
417421
///
418422
/// Use instead:
419423
/// ```no_run
420424
/// #[cfg(feature = "some-feature")]
421425
/// fn conditional() { }
426+
/// #[cfg(test)]
427+
/// mod tests { }
422428
/// ```
423429
#[clippy::version = "1.69.0"]
424430
pub MAYBE_MISUSED_CFG,
@@ -938,6 +944,19 @@ fn check_nested_misused_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
938944
}
939945
if let MetaItemKind::List(list) = &meta.kind {
940946
check_nested_misused_cfg(cx, list);
947+
// If this is not a list, then we check for `cfg(test)`.
948+
} else if let Some(ident) = meta.ident()
949+
&& matches!(ident.name.as_str(), "tests" | "Test")
950+
{
951+
span_lint_and_sugg(
952+
cx,
953+
MAYBE_MISUSED_CFG,
954+
meta.span,
955+
&format!("'test' may be misspelled as '{}'", ident.name.as_str()),
956+
"do you mean",
957+
"test".to_string(),
958+
Applicability::MaybeIncorrect,
959+
);
941960
}
942961
}
943962
}

tests/ui/cfg_features.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ fn main() {
1414
//~^ ERROR: feature may misspelled as features
1515
//~| ERROR: feature may misspelled as features
1616
let _ = 1 + 2;
17+
18+
#[cfg(test)]
19+
//~^ ERROR: 'test' may be misspelled as 'tests'
20+
let _ = 2;
21+
#[cfg(test)]
22+
//~^ ERROR: 'test' may be misspelled as 'Test'
23+
let _ = 2;
24+
25+
#[cfg(all(test, test))]
26+
//~^ ERROR: 'test' may be misspelled as 'tests'
27+
//~| ERROR: 'test' may be misspelled as 'Test'
28+
let _ = 2;
1729
}

tests/ui/cfg_features.rs

+12
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ fn main() {
1414
//~^ ERROR: feature may misspelled as features
1515
//~| ERROR: feature may misspelled as features
1616
let _ = 1 + 2;
17+
18+
#[cfg(tests)]
19+
//~^ ERROR: 'test' may be misspelled as 'tests'
20+
let _ = 2;
21+
#[cfg(Test)]
22+
//~^ ERROR: 'test' may be misspelled as 'Test'
23+
let _ = 2;
24+
25+
#[cfg(all(tests, Test))]
26+
//~^ ERROR: 'test' may be misspelled as 'tests'
27+
//~| ERROR: 'test' may be misspelled as 'Test'
28+
let _ = 2;
1729
}

tests/ui/cfg_features.stderr

+25-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,29 @@ error: feature may misspelled as features
2525
LL | #[cfg(all(features = "wrong1", any(feature = "right", features = "wrong2", feature, features)))]
2626
| ^^^^^^^^^^^^^^^^^^^ help: use: `feature = "wrong2"`
2727

28-
error: aborting due to 4 previous errors
28+
error: 'test' may be misspelled as 'tests'
29+
--> $DIR/cfg_features.rs:18:11
30+
|
31+
LL | #[cfg(tests)]
32+
| ^^^^^ help: do you mean: `test`
33+
34+
error: 'test' may be misspelled as 'Test'
35+
--> $DIR/cfg_features.rs:21:11
36+
|
37+
LL | #[cfg(Test)]
38+
| ^^^^ help: do you mean: `test`
39+
40+
error: 'test' may be misspelled as 'tests'
41+
--> $DIR/cfg_features.rs:25:15
42+
|
43+
LL | #[cfg(all(tests, Test))]
44+
| ^^^^^ help: do you mean: `test`
45+
46+
error: 'test' may be misspelled as 'Test'
47+
--> $DIR/cfg_features.rs:25:22
48+
|
49+
LL | #[cfg(all(tests, Test))]
50+
| ^^^^ help: do you mean: `test`
51+
52+
error: aborting due to 8 previous errors
2953

0 commit comments

Comments
 (0)