Skip to content

Commit

Permalink
Improve handling of raw-idents in check-cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Sep 18, 2024
1 parent 46b0f8b commit 89f04c2
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 4 deletions.
14 changes: 11 additions & 3 deletions compiler/rustc_lint/src/context/diagnostics/check_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustc_middle::bug;
use rustc_session::config::ExpectedValues;
use rustc_session::Session;
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::symbol::Ident;
use rustc_span::{sym, Span, Symbol};

use crate::lints;
Expand Down Expand Up @@ -30,7 +31,7 @@ enum EscapeQuotes {
No,
}

fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, quotes: EscapeQuotes) -> String {
fn to_check_cfg_arg(name: Ident, value: Option<Symbol>, quotes: EscapeQuotes) -> String {
if let Some(value) = value {
let value = str::escape_debug(value.as_str()).to_string();
let values = match quotes {
Expand Down Expand Up @@ -110,6 +111,7 @@ pub(super) fn unexpected_cfg_name(
}
};

let best_match = Ident::new(best_match, name_span);
if let Some((value, value_span)) = value {
if best_match_values.contains(&Some(value)) {
lints::unexpected_cfg_name::CodeSuggestion::SimilarNameAndValue {
Expand Down Expand Up @@ -163,6 +165,8 @@ pub(super) fn unexpected_cfg_name(
};
let expected_names = if !possibilities.is_empty() {
let (possibilities, and_more) = sort_and_truncate_possibilities(sess, possibilities);
let possibilities: Vec<_> =
possibilities.into_iter().map(|s| Ident::new(s, name_span)).collect();
Some(lints::unexpected_cfg_name::ExpectedNames {
possibilities: possibilities.into(),
and_more,
Expand All @@ -176,7 +180,9 @@ pub(super) fn unexpected_cfg_name(
}
};

let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
let inst = |escape_quotes| {
to_check_cfg_arg(Ident::new(name, name_span), value.map(|(v, _s)| v), escape_quotes)
};

let invocation_help = if is_from_cargo {
let sub = if !is_feature_cfg { Some(cargo_help_sub(sess, &inst)) } else { None };
Expand Down Expand Up @@ -273,7 +279,9 @@ pub(super) fn unexpected_cfg_value(
|| (matches!(sess.psess.unstable_features, rustc_feature::UnstableFeatures::Cheat)
&& !sess.opts.unstable_opts.ui_testing);

let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
let inst = |escape_quotes| {
to_check_cfg_arg(Ident::new(name, name_span), value.map(|(v, _s)| v), escape_quotes)
};

let invocation_help = if is_from_cargo {
let help = if name == sym::feature {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,7 @@ pub(crate) struct UnexpectedCfgName {
pub(crate) mod unexpected_cfg_name {
use rustc_errors::DiagSymbolList;
use rustc_macros::Subdiagnostic;
use rustc_span::symbol::Ident;
use rustc_span::{Span, Symbol};

#[derive(Subdiagnostic)]
Expand Down Expand Up @@ -2260,7 +2261,7 @@ pub(crate) mod unexpected_cfg_name {
#[derive(Subdiagnostic)]
#[help_once(lint_unexpected_cfg_name_expected_names)]
pub(crate) struct ExpectedNames {
pub possibilities: DiagSymbolList,
pub possibilities: DiagSymbolList<Ident>,
pub and_more: usize,
}

Expand Down
40 changes: 40 additions & 0 deletions tests/ui/check-cfg/raw-keywords.edition2015.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
warning: unexpected `cfg` condition name: `tru`
--> $DIR/raw-keywords.rs:14:7
|
LL | #[cfg(tru)]
| ^^^ help: there is a config with a similar name: `r#true`
|
= help: to expect this configuration use `--check-cfg=cfg(tru)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition name: `r#false`
--> $DIR/raw-keywords.rs:19:7
|
LL | #[cfg(r#false)]
| ^^^^^^^
|
= help: expected names are: `async`, `clippy`, `debug_assertions`, `doc`, `doctest`, `edition2015`, `edition2021`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `r#true`, `ub_checks`, `unix`, and `windows`
= help: to expect this configuration use `--check-cfg=cfg(r#false)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `await`
--> $DIR/raw-keywords.rs:27:29
|
LL | #[cfg_attr(edition2015, cfg(await))]
| ^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(await)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `raw`
--> $DIR/raw-keywords.rs:33:7
|
LL | #[cfg(r#raw)]
| ^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(raw)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: 4 warnings emitted

40 changes: 40 additions & 0 deletions tests/ui/check-cfg/raw-keywords.edition2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
warning: unexpected `cfg` condition name: `tru`
--> $DIR/raw-keywords.rs:14:7
|
LL | #[cfg(tru)]
| ^^^ help: there is a config with a similar name: `r#true`
|
= help: to expect this configuration use `--check-cfg=cfg(tru)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition name: `r#false`
--> $DIR/raw-keywords.rs:19:7
|
LL | #[cfg(r#false)]
| ^^^^^^^
|
= help: expected names are: `r#async`, `clippy`, `debug_assertions`, `doc`, `doctest`, `edition2015`, `edition2021`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `r#true`, `ub_checks`, `unix`, and `windows`
= help: to expect this configuration use `--check-cfg=cfg(r#false)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `r#await`
--> $DIR/raw-keywords.rs:28:29
|
LL | #[cfg_attr(edition2021, cfg(r#await))]
| ^^^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(r#await)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition name: `raw`
--> $DIR/raw-keywords.rs:33:7
|
LL | #[cfg(r#raw)]
| ^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(raw)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration

warning: 4 warnings emitted

40 changes: 40 additions & 0 deletions tests/ui/check-cfg/raw-keywords.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This test check that using raw keywords works with --cfg and --check-cfg
// and that the diagnostics suggestions are coherent
//
//@ check-pass
//@ no-auto-check-cfg
//@ compile-flags: --cfg=true --cfg=async --check-cfg=cfg(r#true,r#async,edition2015,edition2021)
//
//@ revisions: edition2015 edition2021
//@ [edition2021] compile-flags: --edition 2021

#[cfg(r#true)]
fn foo() {}

#[cfg(tru)]
//~^ WARNING unexpected `cfg` condition name: `tru`
//~^^ SUGGESTION r#true
fn foo() {}

#[cfg(r#false)]
//~^ WARNING unexpected `cfg` condition name: `r#false`
fn foo() {}

#[cfg_attr(edition2015, cfg(async))]
#[cfg_attr(edition2021, cfg(r#async))]
fn bar() {}

#[cfg_attr(edition2015, cfg(await))]
#[cfg_attr(edition2021, cfg(r#await))]
//[edition2015]~^^ WARNING unexpected `cfg` condition name: `await`
//[edition2021]~^^ WARNING unexpected `cfg` condition name: `r#await`
fn zoo() {}

#[cfg(r#raw)]
//~^ WARNING unexpected `cfg` condition name: `raw`
fn foo() {}

fn main() {
foo();
bar();
}

0 comments on commit 89f04c2

Please sign in to comment.