Improve diagnostic for unexpected feature config name from Cargo

This commit is contained in:
Urgau 2023-11-19 21:41:55 +01:00
parent 9f15acec47
commit 1c37997fa7
3 changed files with 27 additions and 0 deletions

View File

@ -739,6 +739,8 @@ pub trait LintContext {
} else {
db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect);
}
} else if name == sym::feature && std::env::var_os("CARGO").is_some() {
db.help("consider defining some features in `Cargo.toml`");
} else if !possibilities.is_empty() {
let mut possibilities = possibilities.iter()
.map(Symbol::as_str)

View File

@ -0,0 +1,14 @@
// This test checks that when no features are passed by Cargo we
// suggest adding some in the Cargo.toml instead of vomitting a
// list of all the expected names
//
// check-pass
// rustc-env:CARGO=/usr/bin/cargo
// compile-flags: --check-cfg=cfg() -Z unstable-options
// error-pattern:Cargo.toml
#[cfg(feature = "serde")]
//~^ WARNING unexpected `cfg` condition name
fn ser() {}
fn main() {}

View File

@ -0,0 +1,11 @@
warning: unexpected `cfg` condition name: `feature`
--> $DIR/cargo-feature.rs:10:7
|
LL | #[cfg(feature = "serde")]
| ^^^^^^^^^^^^^^^^^
|
= help: consider defining some features in `Cargo.toml`
= note: `#[warn(unexpected_cfgs)]` on by default
warning: 1 warning emitted