Skip to content

cfg_select should check for unexpected_cfgs on other arms #149090

Closed

Description

@tgross35

Code:

#![feature(cfg_select)]

fn main() {
    let demo1 = cfg_select! {
        true => "valid",
        invalid_cfg1 => "invalid",
        _ => "fallback"
    };
    let demo2 = cfg_select! {
        invalid_cfg2 => "invalid",
        true => "valid",
        _ => "fallback"
    };
    dbg!(demo1, demo2);
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=170a24120aafa2dcf4d941fd1b189a56

Output:

   Compiling playground v0.0.1 (/playground)
warning: unexpected `cfg` condition name: `invalid_cfg2`
  --> src/main.rs:10:9
   |
10 |         invalid_cfg2 => "invalid",
   |         ^^^^^^^^^^^^
   |
   = help: expected names are: `docsrs`, `feature`, and `test` and 31 more
   = help: consider using a Cargo feature instead
   = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
            [lints.rust]
            unexpected_cfgs = { level = "warn", check-cfg = ['cfg(invalid_cfg2)'] }
   = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(invalid_cfg2)");` to the top of the `build.rs`
   = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
   = note: `#[warn(unexpected_cfgs)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.60s
     Running `target/debug/playground`
[src/main.rs:14:5] demo1 = "valid"
[src/main.rs:14:5] demo2 = "valid"

Here we get a warning on invalid_cfg2 since the invalid arm for demo2 comes before the matched arm (true). Ideally we should get the same warning for invalid_cfg1 because it also isn't a valid configuration: however, since true is hit first in the demo2 case, the rest of the arms don't currently get checked.

Version: 1.93.0-nightly (2025-11-18 3d461af)

Activity

  1. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Nov 19, 2025
  2. JonathanBrouwer commented on Nov 19, 2025

    @JonathanBrouwer
    Member

    I think I got really close to accidentally fixing this in #148712
    I'll see how easy this is to fix
    @rustbot claim

  3. tgross35 commented on Nov 19, 2025

    @tgross35
    MemberAuthor

    Cc @rust-lang/lang to make sure this makes sense. I believe it's what users would expect following the approximate mental model of cfg_select!{ pred1 => res1, pred2 => res2 } expanding to #[cfg(pred1)] res1; #[cfg(pred2)] res2;.

  4. added
    I-lang-nominatedNominated for discussion during a lang team meeting.
    I-lang-radarItems that are on lang's radar and will need eventual work or consideration.
    P-lang-drag-1Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang
    on Nov 20, 2025
  5. Urgau commented on Nov 20, 2025

    @Urgau
    Member

    If we think of them as arms, we should examine all the arms and lint on them, regardless of whether they have been evaluated.

    We already do lint in a similar situation for configs that are "not evaluated" in predicates: cfg(any(true, invalid_cfg)).

  6. joshtriplett commented on Nov 26, 2025

    @joshtriplett
    Member

    I do think it makes sense to check as much as we can, here.

    At some point, we may want to evaluate to what degree we want to support people doing cfg(all(rust_version(...), new_predicate_in_current_rust)) without error. But that isn't specific to cfg_select!.

  7. removed
    I-lang-nominatedNominated for discussion during a lang team meeting.
    P-lang-drag-1Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang
    on Nov 26, 2025
  8. joshtriplett commented on Nov 26, 2025

    @joshtriplett
    Member

    We discussed this in today's @rust-lang/lang meeting.

    We agreed that we should go ahead with the proposed change to apply the lint to the other arms.

    Future work on short circuiting would need to be consistently applied to all/any/cfg_select/etc, and would need some further design to balance version handling with thorough checking. (For instance, being more permissive after a version check.)

  9. added a commit that references this issue on Nov 28, 2025
  10. added a commit that references this issue on Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-cfg_selectArea: `cfg_select!`A-check-cfgArea: --check-cfgA-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions