Continue improvements on the --check-cfg implementation

- Test the combinations of --check-cfg with partial values() and --cfg
- Test that we detect unexpected value when none are expected
This commit is contained in:
Loïc BRANSTETT 2022-02-20 01:26:52 +01:00
parent 3d234770b1
commit 8d3de56da1
9 changed files with 184 additions and 55 deletions

View file

@ -768,17 +768,14 @@ pub trait LintContext: Sized {
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 {
let possibilities: Vec<Symbol> = if value.is_some() {
let Some(values) = &sess.parse_sess.check_config.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");
bug!("it shouldn't be possible to have a diagnostic on a name if name checking is not enabled");
};
names_valid.iter().map(|s| *s).collect()
};
@ -786,17 +783,21 @@ pub trait LintContext: Sized {
// 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();
if !possibilities.is_empty() {
let mut possibilities = possibilities.iter().map(Symbol::as_str).collect::<Vec<_>>();
possibilities.sort();
let possibilities = possibilities.iter().map(Symbol::as_str).intersperse(", ").collect::<String>();
db.note(&format!("possible values for `{name}` are: {possibilities}"));
let possibilities = possibilities.join(", ");
db.note(&format!("expected values for `{name}` are: {possibilities}"));
} else {
db.note(&format!("no expected value for `{name}`"));
}
}
// 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);
let punctuation = if value.is_some() { "\"" } else { "" };
db.span_suggestion(span, "did you mean", format!("{punctuation}{best_match}{punctuation}"), Applicability::MaybeIncorrect);
}
},
}