Skip to content

Commit 3d23477

Browse files
committed
Improve diagnostic of the unexpected_cfgs lint
1 parent fbe1c15 commit 3d23477

File tree

7 files changed

+57
-7
lines changed

7 files changed

+57
-7
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_errors::{struct_span_err, Applicability};
88
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
99
use rustc_macros::HashStable_Generic;
1010
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
11+
use rustc_session::lint::BuiltinLintDiagnostics;
1112
use rustc_session::parse::{feature_err, ParseSess};
1213
use rustc_session::Session;
1314
use rustc_span::hygiene::Transparency;
@@ -465,23 +466,33 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
465466
let value = cfg.value_str();
466467
if let Some(names_valid) = &sess.check_config.names_valid {
467468
if !names_valid.contains(&name) {
468-
sess.buffer_lint(
469+
sess.buffer_lint_with_diagnostic(
469470
UNEXPECTED_CFGS,
470471
cfg.span,
471472
CRATE_NODE_ID,
472473
"unexpected `cfg` condition name",
474+
BuiltinLintDiagnostics::UnexpectedCfg(
475+
cfg.ident().unwrap().span,
476+
name,
477+
None,
478+
),
473479
);
474480
}
475481
}
476482
if let Some(val) = value {
477483
if let Some(values_valid) = &sess.check_config.values_valid {
478484
if let Some(values) = values_valid.get(&name) {
479485
if !values.contains(&val) {
480-
sess.buffer_lint(
486+
sess.buffer_lint_with_diagnostic(
481487
UNEXPECTED_CFGS,
482488
cfg.span,
483489
CRATE_NODE_ID,
484490
"unexpected `cfg` condition value",
491+
BuiltinLintDiagnostics::UnexpectedCfg(
492+
cfg.name_value_literal_span().unwrap(),
493+
name,
494+
Some(val),
495+
),
485496
);
486497
}
487498
}

compiler/rustc_lint/src/context.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,39 @@ pub trait LintContext: Sized {
766766
BuiltinLintDiagnostics::NamedAsmLabel(help) => {
767767
db.help(&help);
768768
db.note("see the asm section of Rust By Example <https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels> for more information");
769-
}
769+
},
770+
BuiltinLintDiagnostics::UnexpectedCfg(span, name, value) => {
771+
let mut possibilities: Vec<Symbol> = if value.is_some() {
772+
let Some(values_valid) = &sess.parse_sess.check_config.values_valid else {
773+
bug!("it shouldn't be possible to have a diagnostic on a value if values checking is not enable");
774+
};
775+
let Some(values) = values_valid.get(&name) else {
776+
bug!("it shouldn't be possible to have a diagnostic on a value whose name is not in values");
777+
};
778+
values.iter().map(|&s| s).collect()
779+
} else {
780+
let Some(names_valid) = &sess.parse_sess.check_config.names_valid else {
781+
bug!("it shouldn't be possible to have a diagnostic on a value if values checking is not enable");
782+
};
783+
names_valid.iter().map(|s| *s).collect()
784+
};
785+
786+
// Show the full list if all possible values for a given name, but don't do it
787+
// for names as the possibilities could be very long
788+
if value.is_some() {
789+
// Sorting can take some time, so we only do it if required
790+
possibilities.sort();
791+
792+
let possibilities = possibilities.iter().map(Symbol::as_str).intersperse(", ").collect::<String>();
793+
db.note(&format!("possible values for `{name}` are: {possibilities}"));
794+
}
795+
796+
// Suggest the most probable if we found one
797+
if let Some(best_match) = find_best_match_for_name(&possibilities, value.unwrap_or(name), None) {
798+
let ponctuation = if value.is_some() { "\"" } else { "" };
799+
db.span_suggestion(span, "did you mean", format!("{ponctuation}{best_match}{ponctuation}"), Applicability::MaybeIncorrect);
800+
}
801+
},
770802
}
771803
// Rewrap `db`, and pass control to the user.
772804
decorate(LintDiagnosticBuilder::new(db));

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(box_patterns)]
3232
#![feature(crate_visibility_modifier)]
3333
#![feature(if_let_guard)]
34+
#![feature(iter_intersperse)]
3435
#![feature(iter_order_by)]
3536
#![feature(let_else)]
3637
#![feature(never_type)]

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ pub enum BuiltinLintDiagnostics {
310310
BreakWithLabelAndLoop(Span),
311311
NamedAsmLabel(String),
312312
UnicodeTextFlow(Span, String),
313+
UnexpectedCfg(Span, Symbol, Option<Symbol>),
313314
}
314315

315316
/// Lints that are buffered up early on in the `Session` before the

src/test/ui/check-cfg/invalid-cfg-name.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unexpected `cfg` condition name
22
--> $DIR/invalid-cfg-name.rs:7:7
33
|
44
LL | #[cfg(widnows)]
5-
| ^^^^^^^
5+
| ^^^^^^^ help: did you mean: `windows`
66
|
77
= note: `#[warn(unexpected_cfgs)]` on by default
88

src/test/ui/check-cfg/invalid-cfg-value.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | #[cfg(feature = "sedre")]
55
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(unexpected_cfgs)]` on by default
8+
= note: possible values for `feature` are: rand, serde, full
89

910
warning: 1 warning emitted
1011

src/test/ui/check-cfg/well-known-names.stderr

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@ warning: unexpected `cfg` condition name
22
--> $DIR/well-known-names.rs:6:7
33
|
44
LL | #[cfg(target_oz = "linux")]
5-
| ^^^^^^^^^^^^^^^^^^^
5+
| ---------^^^^^^^^^^
6+
| |
7+
| help: did you mean: `target_os`
68
|
79
= note: `#[warn(unexpected_cfgs)]` on by default
810

911
warning: unexpected `cfg` condition name
1012
--> $DIR/well-known-names.rs:13:7
1113
|
1214
LL | #[cfg(features = "foo")]
13-
| ^^^^^^^^^^^^^^^^
15+
| --------^^^^^^^^
16+
| |
17+
| help: did you mean: `feature`
1418

1519
warning: unexpected `cfg` condition name
1620
--> $DIR/well-known-names.rs:20:7
1721
|
1822
LL | #[cfg(uniw)]
19-
| ^^^^
23+
| ^^^^ help: did you mean: `unix`
2024

2125
warning: 3 warnings emitted
2226

0 commit comments

Comments
 (0)