Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn for cfg!(target_* = "whatever") usage in build scripts #125441

Open
weiznich opened this issue May 23, 2024 · 1 comment
Open

Warn for cfg!(target_* = "whatever") usage in build scripts #125441

weiznich opened this issue May 23, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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.

Comments

@weiznich
Copy link
Contributor

Code

if cfg!(target_feature = "sse2") {
    // …
}

Current output

No warning

Desired output

A warning pointing at `cfg!(target_feature = "sse2")` stating that this will be likely wrong for the target system if you cross compile

Rationale and extra context

I would expect a warning that explains that cfg!(target_*) will be evaluated in the context of the host system that compiles the build script and not in the context of the target system. It seems to be a common mistake for crate authors writing build scripts to use cfg! and not std::env in combination with the environment variables set by cargo. The warning should suggest that the later variant is the correct solution there.

Other cases

No response

Rust Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

@weiznich weiznich added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 23, 2024
@jieyouxu jieyouxu added the T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. label May 23, 2024
@Urgau
Copy link
Member

Urgau commented May 23, 2024

This is indeed a foot-gun that users might encountered, and I agree that a lint seems appropriate.

However adding this kind of very targetted lint (Cargo specific knowledge) seems out of place for rustc, and given that are use-case for #[cfg(target_* = "...")] in build script, albeit very small, I think this proposed lint should probably live in Clippy.

cc rust-lang/rust-clippy#9419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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
None yet
Development

No branches or pull requests

3 participants