Skip to content

Add new misleading_cfg_in_build_script lint#153721

Open
GuillaumeGomez wants to merge 7 commits into
rust-lang:mainfrom
GuillaumeGomez:misleading_cfg_in_build_script
Open

Add new misleading_cfg_in_build_script lint#153721
GuillaumeGomez wants to merge 7 commits into
rust-lang:mainfrom
GuillaumeGomez:misleading_cfg_in_build_script

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Member

@GuillaumeGomez GuillaumeGomez commented Mar 11, 2026

View all comments

Fixes #125441.
Fixes rust-lang/rust-clippy#9419.

Take-over of rust-lang/rust-clippy#12862.

It was originally implemented in clippy but the clippy thought it made for sense for such a lint to be directly implemented in rustc. I applied all suggestions made on the original PR and also improved the code overall (added docs too).

So on a build.rs from cargo crate, we will check for any target_*, unix and windows cfg and warn about them.

Since you made the original review on clippy.

r? @Urgau

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 11, 2026

Some changes occurred in check-cfg diagnostics

cc @Urgau

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 11, 2026
@GuillaumeGomez GuillaumeGomez force-pushed the misleading_cfg_in_build_script branch from 262d2e2 to 3e99ab7 Compare March 11, 2026 14:35
@GuillaumeGomez
Copy link
Copy Markdown
Member Author

Added a ui test to ensure this lint is only emitted on cargo build.rs scripts.

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the misleading_cfg_in_build_script branch from 3e99ab7 to ea1c733 Compare March 11, 2026 16:00
@rust-log-analyzer

This comment has been minimized.

Comment on lines +202 to +205
fn is_build_script(cx: &EarlyContext<'_>) -> bool {
rustc_session::utils::was_invoked_from_cargo()
&& cx.sess().opts.crate_name.as_deref() == Some("build_script_build")
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems quite hacky and brittle for a lint.

Is there a better to detect if we are compiling a Cargo build script? If there isn't, we could maybe consider having the lint allowed by-default, but have Cargo implicitly set it to warn for build-scripts.

cc @rust-lang/cargo

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed, not great. :-/

I was kinda hoping that we had a "build script" crate kind but we don't (which is logical but still, not fun).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just put it in t-cargo meeting agenda :)
next meeting time: Tue, Mar 17, 2026, 10:00 UTC-4

https://hackmd.io/-vJMV3isQfqu2d2J91t1eA

@GuillaumeGomez GuillaumeGomez force-pushed the misleading_cfg_in_build_script branch from ea1c733 to 28c7221 Compare March 11, 2026 20:58
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

CI is finally happy. ^^'

@ehuss ehuss added the I-cargo-nominated Nominated for discussion during a cargo team meeting. label Mar 13, 2026
@ehuss ehuss moved this to Nominated in Cargo status tracker Mar 17, 2026
@ehuss
Copy link
Copy Markdown
Contributor

ehuss commented Mar 17, 2026

The cargo team discussed this today (notes). Some comments from that discussion:

  1. We were largely OK with the approach of making this allow-by-default and having cargo set it to a warning when building a build script.
  2. I was concerned about the possible amount of false-positives this would generate. Has there been a crater run or other analysis of what kind of false-positives this might hit? For example, if some build script has "cfg(windows) ... run executable that only exists on windows", it seems like this would be a false positive.
  3. I assume this would need lang-team approval. It might be good to get this on their radar.
  4. As an outsider looking at the diff, I am rather surprised to see yet another cfg parser being implemented here. I'm curious why this isn't using the standard cfg parser?

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

  1. Sounds good.
  2. It's a valid concern, to which I don't really have an answer to. Only answer is to run a crater run to see the impact it would have.
  3. We're not clear which team is "in charge" here.
  4. Unless I missed something, the cfg parser requires data I don't have? I'd love to be proven wrong though, would reduce the code quite a lot!

@Urgau
Copy link
Copy Markdown
Member

Urgau commented Mar 17, 2026

I assume this would need lang-team approval. It might be good to get this on their radar.

I would expect a T-cargo FCP, with maybe T-compiler, but not T-lang. The lint proposed here has nothing to do with the language, it's purely a compiler and Cargo affair.

To elaborate a bit more, this lint could be in cargo it-self if we had a custom linting system, and it wouldn't need T-lang approval for that, so I don't think it needs one. We also (T-compiler) didn't ask T-lang to FCP the #[cfg]/--cfg lints.

As an outsider looking at the diff, I am rather surprised to see yet another cfg parser being implemented here. I'm curious why this isn't using the standard cfg parser?

I wouldn't look to closely at the impl. I haven't yet done a review of it, I wanted to wait for T-cargo input first.

Copy link
Copy Markdown
Member

@Urgau Urgau left a comment

Choose a reason for hiding this comment

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

I share the concerns from Eric, about having another cfg parser.

Fortunately, this lint is simple enough that it can use the same infra as the unexpected_cfgs lint and perform the verification directly when parsing the cfgs them-selves in rustc_attr_parsing.

This will require some code re-shuffling between different crates (rustc_attr_parsing, rustc_lint_defs, rustc_lint, ...). You can follow what has been done for the UNEXPECTED_CFGS lint.

View changes since this review

Comment thread compiler/rustc_lint/src/misleading_cfg_in_build_script.rs Outdated
Comment thread compiler/rustc_lint/src/misleading_cfg_in_build_script.rs Outdated
Comment thread compiler/rustc_lint/src/misleading_cfg_in_build_script.rs Outdated
@Urgau Urgau added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 18, 2026
@GuillaumeGomez GuillaumeGomez force-pushed the misleading_cfg_in_build_script branch from 2d14a6b to 06e2527 Compare March 19, 2026 13:34
@rust-log-analyzer

This comment has been minimized.

@ehuss
Copy link
Copy Markdown
Contributor

ehuss commented Mar 23, 2026

I would expect a T-cargo FCP, with maybe T-compiler, but not T-lang. The lint proposed here has nothing to do with the language, it's purely a compiler and Cargo affair.

I don't think that's how lints have been handled in the past. They all need to be approved by the lang team. I'm not aware of any exclusions.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

I'm surprised: I added lints in the past without needing t-lang approval (although it might have been a few years for the last one... ^^').

@traviscross traviscross added T-lang Relevant to the language team I-lang-nominated Nominated for discussion during a lang team meeting. labels Mar 23, 2026
@Urgau
Copy link
Copy Markdown
Member

Urgau commented May 13, 2026

How does it look to you?

I haven't done yet a deep-dive review, but that looks pretty good.

Happy to see the logic moved directly in the parsing, it seems to simplify a bunch of things. Even perf seems to be happy.

I will try to do a review before RustWeek, otherwise it will after.

@Urgau Urgau added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. perf-regression Performance regression. labels May 13, 2026
@GuillaumeGomez GuillaumeGomez force-pushed the misleading_cfg_in_build_script branch from 0ecdc4f to a114a87 Compare May 14, 2026 00:42
@rust-bors

This comment has been minimized.

@Urgau
Copy link
Copy Markdown
Member

Urgau commented May 29, 2026

Implementation seems fine to me.

@GuillaumeGomez what do say we do a crater run with the lint as deny-by-default to see the impact of the lint?

For the purpose of the Crater run (since we don't have Cargo support yet), I think we can check CARGO_CRATE_NAME=build_script_build to know if we are in a build script.

@rustbot label +needs-crater
@rustbot author

@rustbot rustbot added needs-crater This change needs a crater run to check for possible breakage in the ecosystem. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 29, 2026
@GuillaumeGomez GuillaumeGomez force-pushed the misleading_cfg_in_build_script branch from a114a87 to b547e5d Compare May 29, 2026 22:01
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 29, 2026

Some changes occurred in check-cfg diagnostics

cc @Urgau

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 29, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

@Urgau: Rebased and added one extra commit to change the lint level and trigger it on cargo build.rs. I'll let you start the crater run if it looks good to you.

@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
error: ui test did not emit an error
note: by default, ui tests are expected not to compile.
hint: use check-pass, build-pass, or run-pass directive to change this behavior.
status: exit status: 0
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/lint/misleading_cfg_in_build_script.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/lint/misleading_cfg_in_build_script" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
stderr: none

---- [ui] tests/ui/lint/misleading_cfg_in_build_script.rs stdout end ----

@Urgau
Copy link
Copy Markdown
Member

Urgau commented May 30, 2026

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request May 30, 2026
…, r=<try>

Add new `misleading_cfg_in_build_script` lint
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 30, 2026

☀️ Try build successful (CI)
Build commit: dcc22e5 (dcc22e5232dd0dbe95a4b3ab84c8d24a0880771e, parent: 6eda7419e71fdbc1185ed5be7e6bff1a474ab5cd)

@Urgau
Copy link
Copy Markdown
Member

Urgau commented May 30, 2026

@craterbot check

@craterbot
Copy link
Copy Markdown
Collaborator

👌 Experiment pr-153721 created and queued.
🤖 Automatically detected try build dcc22e5
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-crater Status: Waiting on a crater run to be completed. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Warn for cfg!(target_* = "whatever") usage in build scripts Lint for cfg usage which would provide a misleading result in a build script.