use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
use rustc_macros::HashStable_Generic;
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
+use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_session::parse::{feature_err, ParseSess};
use rustc_session::Session;
use rustc_span::hygiene::Transparency;
let value = cfg.value_str();
if let Some(names_valid) = &sess.check_config.names_valid {
if !names_valid.contains(&name) {
- sess.buffer_lint(
+ sess.buffer_lint_with_diagnostic(
UNEXPECTED_CFGS,
cfg.span,
CRATE_NODE_ID,
"unexpected `cfg` condition name",
+ BuiltinLintDiagnostics::UnexpectedCfg(
+ cfg.ident().unwrap().span,
+ name,
+ None,
+ ),
);
}
}
if let Some(values_valid) = &sess.check_config.values_valid {
if let Some(values) = values_valid.get(&name) {
if !values.contains(&val) {
- sess.buffer_lint(
+ sess.buffer_lint_with_diagnostic(
UNEXPECTED_CFGS,
cfg.span,
CRATE_NODE_ID,
"unexpected `cfg` condition value",
+ BuiltinLintDiagnostics::UnexpectedCfg(
+ cfg.name_value_literal_span().unwrap(),
+ name,
+ Some(val),
+ ),
);
}
}
BuiltinLintDiagnostics::NamedAsmLabel(help) => {
db.help(&help);
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");
- }
+ },
+ BuiltinLintDiagnostics::UnexpectedCfg(span, name, value) => {
+ let mut possibilities: Vec<Symbol> = if value.is_some() {
+ let Some(values_valid) = &sess.parse_sess.check_config.values_valid else {
+ bug!("it shouldn't be possible to have a diagnostic on a value if values checking is not enable");
+ };
+ let Some(values) = values_valid.get(&name) else {
+ bug!("it shouldn't be possible to have a diagnostic on a value whose name is not in values");
+ };
+ values.iter().map(|&s| s).collect()
+ } else {
+ let Some(names_valid) = &sess.parse_sess.check_config.names_valid else {
+ bug!("it shouldn't be possible to have a diagnostic on a value if values checking is not enable");
+ };
+ names_valid.iter().map(|s| *s).collect()
+ };
+
+ // Show the full list if all possible values for a given name, but don't do it
+ // for names as the possibilities could be very long
+ if value.is_some() {
+ // Sorting can take some time, so we only do it if required
+ possibilities.sort();
+
+ let possibilities = possibilities.iter().map(Symbol::as_str).intersperse(", ").collect::<String>();
+ db.note(&format!("possible values for `{name}` are: {possibilities}"));
+ }
+
+ // Suggest the most probable if we found one
+ if let Some(best_match) = find_best_match_for_name(&possibilities, value.unwrap_or(name), None) {
+ let ponctuation = if value.is_some() { "\"" } else { "" };
+ db.span_suggestion(span, "did you mean", format!("{ponctuation}{best_match}{ponctuation}"), Applicability::MaybeIncorrect);
+ }
+ },
}
// Rewrap `db`, and pass control to the user.
decorate(LintDiagnosticBuilder::new(db));
#![feature(box_patterns)]
#![feature(crate_visibility_modifier)]
#![feature(if_let_guard)]
+#![feature(iter_intersperse)]
#![feature(iter_order_by)]
#![feature(let_else)]
#![feature(never_type)]
BreakWithLabelAndLoop(Span),
NamedAsmLabel(String),
UnicodeTextFlow(Span, String),
+ UnexpectedCfg(Span, Symbol, Option<Symbol>),
}
/// Lints that are buffered up early on in the `Session` before the
--> $DIR/invalid-cfg-name.rs:7:7
|
LL | #[cfg(widnows)]
- | ^^^^^^^
+ | ^^^^^^^ help: did you mean: `windows`
|
= note: `#[warn(unexpected_cfgs)]` on by default
| ^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unexpected_cfgs)]` on by default
+ = note: possible values for `feature` are: rand, serde, full
warning: 1 warning emitted
--> $DIR/well-known-names.rs:6:7
|
LL | #[cfg(target_oz = "linux")]
- | ^^^^^^^^^^^^^^^^^^^
+ | ---------^^^^^^^^^^
+ | |
+ | help: did you mean: `target_os`
|
= note: `#[warn(unexpected_cfgs)]` on by default
--> $DIR/well-known-names.rs:13:7
|
LL | #[cfg(features = "foo")]
- | ^^^^^^^^^^^^^^^^
+ | --------^^^^^^^^
+ | |
+ | help: did you mean: `feature`
warning: unexpected `cfg` condition name
--> $DIR/well-known-names.rs:20:7
|
LL | #[cfg(uniw)]
- | ^^^^
+ | ^^^^ help: did you mean: `unix`
warning: 3 warnings emitted