From 1c37997fa70129efea84c02cf4d863a44660ab2e Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 19 Nov 2023 21:41:55 +0100 Subject: [PATCH] Improve diagnostic for unexpected feature config name from Cargo --- compiler/rustc_lint/src/context.rs | 2 ++ tests/ui/check-cfg/cargo-feature.rs | 14 ++++++++++++++ tests/ui/check-cfg/cargo-feature.stderr | 11 +++++++++++ 3 files changed, 27 insertions(+) create mode 100644 tests/ui/check-cfg/cargo-feature.rs create mode 100644 tests/ui/check-cfg/cargo-feature.stderr diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index d500c3b66d6..ad2047c1903 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -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) diff --git a/tests/ui/check-cfg/cargo-feature.rs b/tests/ui/check-cfg/cargo-feature.rs new file mode 100644 index 00000000000..ea48c6ea201 --- /dev/null +++ b/tests/ui/check-cfg/cargo-feature.rs @@ -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() {} diff --git a/tests/ui/check-cfg/cargo-feature.stderr b/tests/ui/check-cfg/cargo-feature.stderr new file mode 100644 index 00000000000..619410a28f3 --- /dev/null +++ b/tests/ui/check-cfg/cargo-feature.stderr @@ -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 +