Skip to content

Commit e1e1cb4

Browse files
committed
Improve diagnostic for unexpected feature config name from Cargo
1 parent 048e933 commit e1e1cb4

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

compiler/rustc_lint/src/context.rs

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

tests/ui/check-cfg/cargo-feature.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This test checks that when no features are passed by Cargo we
2+
// suggest adding some in the Cargo.toml instead of vomitting a
3+
// list of all the expected names
4+
//
5+
// check-pass
6+
// rustc-env:CARGO=/usr/bin/cargo
7+
// compile-flags: --check-cfg=cfg() -Z unstable-options
8+
// error-pattern:Cargo.toml
9+
10+
#[cfg(feature = "serde")]
11+
//~^ WARNING unexpected `cfg` condition name
12+
fn ser() {}
13+
14+
fn main() {}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: unexpected `cfg` condition name: `feature`
2+
--> $DIR/cargo-feature.rs:10:7
3+
|
4+
LL | #[cfg(feature = "serde")]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= help: consider defining some features in `Cargo.toml`
8+
= note: `#[warn(unexpected_cfgs)]` on by default
9+
10+
warning: 1 warning emitted
11+

0 commit comments

Comments
 (0)