Skip to content

Add new misleading_cfg_in_build_script lint#153721

Open
GuillaumeGomez wants to merge 4 commits intorust-lang:mainfrom
GuillaumeGomez:misleading_cfg_in_build_script
Open

Add new misleading_cfg_in_build_script lint#153721
GuillaumeGomez wants to merge 4 commits intorust-lang:mainfrom
GuillaumeGomez:misleading_cfg_in_build_script

Conversation

@GuillaumeGomez
Copy link
Member

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
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
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
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
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).

@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.

…t have a compiler output generated by the lint-docs script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

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.

4 participants