diff options
| author | bors <bors@rust-lang.org> | 2023-10-13 08:37:27 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-10-13 08:37:27 +0000 |
| commit | a4a10bdf29a13b8773948c8938d2af47c03becb9 (patch) | |
| tree | 8bbd06d9b6dcbccb1df599295470118fa0da87f1 /compiler/rustc_lint/src | |
| parent | 2763ca50da1192aa28295ef4dbe5d06443e1b90a (diff) | |
| parent | ed922d8c727979d8fe8b03061e83a0841a0e959b (diff) | |
| download | rust-a4a10bdf29a13b8773948c8938d2af47c03becb9.tar.gz rust-a4a10bdf29a13b8773948c8938d2af47c03becb9.zip | |
Auto merge of #116666 - Urgau:check-cfg-pre-mcp636, r=petrochenkov
Improve check-cfg diagnostics This PR tries to improve some of the diagnostics of check-cfg. The main changes is the unexpected name or value being added to the main diagnostic: ```diff - warning: unexpected `cfg` condition name + warning: unexpected `cfg` condition name: `widnows` ``` It also cherry-pick the better sensible logic for when we print the list of expected values when we have a matching value for a very similar name. Address https://github.com/rust-lang/rust/pull/111072#discussion_r1356818100 r? `@petrochenkov`
Diffstat (limited to 'compiler/rustc_lint/src')
| -rw-r--r-- | compiler/rustc_lint/src/context.rs | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 3c5cde4309b..1ed9b86d66c 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -727,11 +727,14 @@ pub trait LintContext: Sized { .collect::<Vec<_>>(); possibilities.sort(); + let mut should_print_possibilities = true; if let Some((value, value_span)) = value { if best_match_values.contains(&Some(value)) { db.span_suggestion(name_span, "there is a config with a similar name and value", best_match, Applicability::MaybeIncorrect); + should_print_possibilities = false; } else if best_match_values.contains(&None) { db.span_suggestion(name_span.to(value_span), "there is a config with a similar name and no value", best_match, Applicability::MaybeIncorrect); + should_print_possibilities = false; } else if let Some(first_value) = possibilities.first() { db.span_suggestion(name_span.to(value_span), "there is a config with a similar name and different values", format!("{best_match} = \"{first_value}\""), Applicability::MaybeIncorrect); } else { @@ -741,13 +744,25 @@ pub trait LintContext: Sized { db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect); } - if !possibilities.is_empty() { + if !possibilities.is_empty() && should_print_possibilities { let possibilities = possibilities.join("`, `"); db.help(format!("expected values for `{best_match}` are: `{possibilities}`")); } } else { db.span_suggestion(name_span, "there is a config with a similar name", best_match, Applicability::MaybeIncorrect); } + } else if !possibilities.is_empty() { + let mut possibilities = possibilities.iter() + .map(Symbol::as_str) + .collect::<Vec<_>>(); + possibilities.sort(); + let possibilities = possibilities.join("`, `"); + + // The list of expected names can be long (even by default) and + // so the diagnostic produced can take a lot of space. To avoid + // cloging the user output we only want to print that diagnostic + // once. + db.help_once(format!("expected names are: `{possibilities}`")); } }, BuiltinLintDiagnostics::UnexpectedCfgValue((name, name_span), value) => { |
