Skip to content

Conversation

madsmtm
Copy link
Contributor

@madsmtm madsmtm commented Oct 10, 2025

Add cargo_cfg_target_family_multivalued which detects simple comparisons like == or match on the CARGO_CFG_TARGET_FAMILY environment variable. These are wrong, since CARGO_CFG_TARGET_FAMILY is multi-valued (currently only on wasm32-unknown-emscripten and wasm32-wali-linux-musl, so admittedly pretty rare).

This should unblock at some point adding #[cfg(target_family = "darwin")], which in turns unblocks #100343.

There exist other multi-valued cfgs such as CARGO_CFG_TARGET_HAS_ATOMIC or CARGO_CFG_TARGET_HAS_ATOMIC, but these were very clearly multi-valued from the beginning, and thus doesn't make sense to lint for.

This is my first time adding a lint, so beware that I'm a bit unsure if I did things right. Especially unsure if I should feature-gate the lint to start with, or if we should insta-stabilize it? I'm pretty sure it needs a lang FCP though.

r? compiler
CC @thomcc @workingjubilee

@madsmtm madsmtm added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-lang Relevant to the language team T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-cfg Area: `cfg` conditional compilation labels Oct 10, 2025
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 10, 2025
///
/// [CARGO_CFG_TARGET_FAMILY]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#:~:text=CARGO_CFG_TARGET_FAMILY
/// [cfg-target_family]: https://doc.rust-lang.org/reference/conditional-compilation.html#target_family
CARGO_CFG_TARGET_FAMILY_MULTIVALUED,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about the name? cargo_cfg_target_family_multivalued is kinda long, but I felt that shortening it risked loosing meaning. _multivalued isn't the best descriptor though, perhaps _direct_comparison would be better?

struct ReplaceWithSplitAny {
#[suggestion_part(code = "!")]
negate: Option<Span>,
#[suggestion_part(code = ".split(',').any(|x| x == ")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could suggest .contains("unix") instead, that'd work as well. I felt .split(',').any(|x| x == "unix") was more principled, but I'd be fine with either.

Warn,
"comparing `CARGO_CFG_TARGET_FAMILY` env var with a single value",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if this is the intended use of a FCW? I would like it to trigger in dependents some day, but there wasn't precedent for using FutureReleaseSemanticsChange anywhere.

@petrochenkov petrochenkov added S-waiting-on-t-lang Status: Awaiting decision from T-lang I-lang-nominated Nominated for discussion during a lang team meeting. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cfg Area: `cfg` conditional compilation A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-t-lang Status: Awaiting decision from T-lang T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants