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 lint over cfg(test) #11821

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,26 @@ declare_clippy_lint! {
/// Checks for `#[cfg(features = "...")]` and suggests to replace it with
/// `#[cfg(feature = "...")]`.
///
/// It also checks if `cfg(test)` was misspelled.
///
/// ### Why is this bad?
/// Misspelling `feature` as `features` can be sometimes hard to spot. It
/// Misspelling `feature` as `features` or `test` as `tests` can be sometimes hard to spot. It
/// may cause conditional compilation not work quietly.
///
/// ### Example
/// ```no_run
/// #[cfg(features = "some-feature")]
/// fn conditional() { }
/// #[cfg(tests)]
/// mod tests { }
/// ```
///
/// Use instead:
/// ```no_run
/// #[cfg(feature = "some-feature")]
/// fn conditional() { }
/// #[cfg(test)]
/// mod tests { }
/// ```
#[clippy::version = "1.69.0"]
pub MAYBE_MISUSED_CFG,
Expand Down Expand Up @@ -946,6 +952,19 @@ fn check_nested_misused_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
}
if let MetaItemKind::List(list) = &meta.kind {
check_nested_misused_cfg(cx, list);
// If this is not a list, then we check for `cfg(test)`.
} else if let Some(ident) = meta.ident()
&& matches!(ident.name.as_str(), "tests" | "Test")
Comment on lines +956 to +957
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of deny-listing tests and Test, would it be possible to allow-list all configs passed in by --cfg plus the built-in ones (test, debug_assertions, …)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check for the full list then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So back to this, I'm not sure it's a good approach because you might use a valid cfg that is not passed to the compiler (so it's cfged-out) and in this case it would be considered as an error since this cfg isn't known to clippy. So false-positive.

However on the other hand, it's already possible to have this issue currently, so not sure if we want to dive into this any further.

I think a next iteration on this would be to eventually check for all existing ones (the builtin ones and the ones provided to rustc) but we need to check pros and cons.

Does that sound acceptable?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right; catching all cfg errors is definitely a different and much larger issue

Copy link
Member

@pietroalbini pietroalbini Nov 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is. I discovered it afterwards and I linked to the tracking issue in my last comment of this PR.

{
span_lint_and_sugg(
cx,
MAYBE_MISUSED_CFG,
meta.span,
&format!("'test' may be misspelled as '{}'", ident.name.as_str()),
"do you mean",
"test".to_string(),
Applicability::MaybeIncorrect,
);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/cfg_features.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ fn main() {
//~^ ERROR: feature may misspelled as features
//~| ERROR: feature may misspelled as features
let _ = 1 + 2;

#[cfg(test)]
//~^ ERROR: 'test' may be misspelled as 'tests'
let _ = 2;
#[cfg(test)]
//~^ ERROR: 'test' may be misspelled as 'Test'
let _ = 2;

#[cfg(all(test, test))]
//~^ ERROR: 'test' may be misspelled as 'tests'
//~| ERROR: 'test' may be misspelled as 'Test'
let _ = 2;
}
12 changes: 12 additions & 0 deletions tests/ui/cfg_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ fn main() {
//~^ ERROR: feature may misspelled as features
//~| ERROR: feature may misspelled as features
let _ = 1 + 2;

#[cfg(tests)]
//~^ ERROR: 'test' may be misspelled as 'tests'
let _ = 2;
#[cfg(Test)]
//~^ ERROR: 'test' may be misspelled as 'Test'
let _ = 2;

#[cfg(all(tests, Test))]
//~^ ERROR: 'test' may be misspelled as 'tests'
//~| ERROR: 'test' may be misspelled as 'Test'
let _ = 2;
}
26 changes: 25 additions & 1 deletion tests/ui/cfg_features.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,29 @@ error: feature may misspelled as features
LL | #[cfg(all(features = "wrong1", any(feature = "right", features = "wrong2", feature, features)))]
| ^^^^^^^^^^^^^^^^^^^ help: use: `feature = "wrong2"`

error: aborting due to 4 previous errors
error: 'test' may be misspelled as 'tests'
--> $DIR/cfg_features.rs:18:11
|
LL | #[cfg(tests)]
| ^^^^^ help: do you mean: `test`

error: 'test' may be misspelled as 'Test'
--> $DIR/cfg_features.rs:21:11
|
LL | #[cfg(Test)]
| ^^^^ help: do you mean: `test`

error: 'test' may be misspelled as 'tests'
--> $DIR/cfg_features.rs:25:15
|
LL | #[cfg(all(tests, Test))]
| ^^^^^ help: do you mean: `test`

error: 'test' may be misspelled as 'Test'
--> $DIR/cfg_features.rs:25:22
|
LL | #[cfg(all(tests, Test))]
| ^^^^ help: do you mean: `test`

error: aborting due to 8 previous errors