Rollup merge of #149380 - JonathanBrouwer:cfg_select_lints, r=Urgau

Run `eval_config_entry` on all branches so we always emit lints

Fixes https://github.com/rust-lang/rust/issues/149090

Ideally I'd have liked to fix this issue using https://github.com/rust-lang/rust/pull/149215, and this is still the long term plan, but this is slightly more annoying to implement than I'd have liked to, and this is also a nice and easy solution to the problem.

r? `@tgross35`
This commit is contained in:
Stuart Cook 2025-11-28 15:30:45 +11:00 committed by GitHub
commit 145c81d872
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 2 deletions

View file

@ -10,6 +10,7 @@ use crate::errors::{CfgSelectNoMatches, CfgSelectUnreachable};
/// Selects the first arm whose predicate evaluates to true.
fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenStream, Span)> {
let mut result = None;
for (cfg, tt, arm_span) in branches.reachable {
if let EvalConfigResult::True = attr::eval_config_entry(
&ecx.sess,
@ -17,11 +18,13 @@ fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenSt
ecx.current_expansion.lint_node_id,
ShouldEmit::ErrorsAndLints,
) {
return Some((tt, arm_span));
// FIXME(#149215) Ideally we should short-circuit here, but `eval_config_entry` currently emits lints so we cannot do this yet.
result.get_or_insert((tt, arm_span));
}
}
branches.wildcard.map(|(_, tt, span)| (tt, span))
let wildcard = branches.wildcard.map(|(_, tt, span)| (tt, span));
result.or(wildcard)
}
pub(super) fn expand_cfg_select<'cx>(

View file

@ -0,0 +1,18 @@
//@ check-pass
#![feature(cfg_select)]
#![crate_type = "lib"]
cfg_select! {
true => {}
invalid_cfg1 => {}
//~^ WARN unexpected `cfg` condition name
_ => {}
}
cfg_select! {
invalid_cfg2 => {}
//~^ WARN unexpected `cfg` condition name
true => {}
_ => {}
}

View file

@ -0,0 +1,22 @@
warning: unexpected `cfg` condition name: `invalid_cfg1`
--> $DIR/cfg-select.rs:8:5
|
LL | invalid_cfg1 => {}
| ^^^^^^^^^^^^
|
= help: expected names are: `FALSE` and `test` and 31 more
= help: to expect this configuration use `--check-cfg=cfg(invalid_cfg1)`
= 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: `invalid_cfg2`
--> $DIR/cfg-select.rs:14:5
|
LL | invalid_cfg2 => {}
| ^^^^^^^^^^^^
|
= help: to expect this configuration use `--check-cfg=cfg(invalid_cfg2)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
warning: 2 warnings emitted