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

Warn for features not defined in a Cargo.toml #91674

Closed
Velfi opened this issue Dec 8, 2021 · 3 comments
Closed

Warn for features not defined in a Cargo.toml #91674

Velfi opened this issue Dec 8, 2021 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Velfi
Copy link

Velfi commented Dec 8, 2021

Given the following code:
playground link

fn main() {
    #[cfg(any(feature = "feature_a", feature = "feature_b"))]
    foo()
}

// Oh no, a typo in a feature name!
#[cfg(any(feature = "feature_a", feature = "feature-b"))]
pub fn foo() {
    println!("foo")
}

The current output is:

// No output

Ideally the output should look like:

You have a feature gate for a feature "feature-b" that is not defined in this crate's Cargo.toml.
   --> main.rs
    |
    | #[cfg(any(feature = "feature_a", feature = "feature-b"))]
    |                                             ^ help: a feature with a similar name exists: `feature_b`

This issue is in regards to the interaction of cargo and rustc. Imagine that the above example is from project with this Cargo.toml:

[package]
name = "a-crate"
version = "0.1.0"
edition = "2018"

[features]
feature_a = []
feature_b = []

I ran into this as part of my work on the AWS Rust SDKs and you can view the PR where I fixed a bug related to this here. I was fixing a bug where it looked like some code should have compiled but it didn't due to a typo in the feature flag. It would have saved me some time if the compiler could have detected this typo and drawn my attention to it.

@Velfi Velfi added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 8, 2021
@estebank estebank added T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Nov 23, 2022
@estebank
Copy link
Contributor

Tagging t-cargo because I believe that it might have to pass crate information in, but rustc could learn to parse Cargo.toml itself as well.

@weihanglo
Copy link
Member

Rustc has the unstable option --check-cfg, and Cargo also got the counterpart -Zcheck-cfg a while back. You can try them with nightly channel. The warning probably need some specialization though.

Click to see a demo of -Zcheck-cfg
$ cargo +nightly check -Z unstable-options -Z check-cfg=features
warning: unexpected `cfg` condition value
 --> src/main.rs:6:11
  |
6 | #[cfg(any(feature = "feature_a", feature = "feature-b"))]
  |           ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: no expected value for `feature`
  = note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition value
 --> src/main.rs:6:34
  |
6 | #[cfg(any(feature = "feature_a", feature = "feature-b"))]
  |                                  ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: no expected value for `feature`

warning: unexpected `cfg` condition value
 --> src/main.rs:2:15
  |
2 |     #[cfg(any(feature = "feature_a", feature = "feature-b"))]
  |               ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: no expected value for `feature`

warning: unexpected `cfg` condition value
 --> src/main.rs:2:38
  |
2 |     #[cfg(any(feature = "feature_a", feature = "feature-b"))]
  |                                      ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: no expected value for `feature`

warning: `f` (bin "f") generated 4 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.19s

I guess this could be closed as we track these flags from #82450 and rust-lang/cargo#10554.

@estebank
Copy link
Contributor

Closing in favor of the tracking issue, and very happy to see some recent progress on this. Looking forward to this being enabled by default in stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants