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

Detect cfg based on an undeclared feature #12634

Closed
gendx opened this issue Apr 5, 2024 · 1 comment
Closed

Detect cfg based on an undeclared feature #12634

gendx opened this issue Apr 5, 2024 · 1 comment
Labels
A-lint Area: New lints

Comments

@gendx
Copy link
Contributor

gendx commented Apr 5, 2024

What it does

Detect and warn on uses of cfg(feature = "foo") where the feature ("foo") isn't declared in the Cargo.toml file.

Advantage

The cfg always evaluates to false, which is likely not intended (e.g. typo, feature that was renamed in the Cargo.toml file but not everywhere in the code, etc.).

Drawbacks

I don't see any reason to have a cfg based on a feature that doesn't exist.

Example

# Cargo.toml
[features]
bar = []
let x = 0;
// Feature "foo" isn't defined in the Cargo.toml file.
#[cfg(feature = "foo")]
x = 42;
@gendx gendx added the A-lint Area: New lints label Apr 5, 2024
@y21
Copy link
Member

y21 commented Apr 5, 2024

Closing as a duplicate of #11649. This already exists unstably in cargo as -Zcheck-cfg, and is close to being stabilized it looks like (tracking issue: rust-lang/rust#82450)

warning: unexpected `cfg` condition value: `foo`
 --> src/main.rs:4:11
  |
4 |     #[cfg(feature = "foo")]
  |           ^^^^^^^^^^^^^^^ help: remove the condition
  |
  = note: no expected values for `feature`
  = help: consider adding `foo` as a feature in `Cargo.toml`

Thanks for opening this regardless!

@y21 y21 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants