Add new misleading_cfg_in_build_script lint#153721
Add new misleading_cfg_in_build_script lint#153721GuillaumeGomez wants to merge 6 commits intorust-lang:mainfrom
misleading_cfg_in_build_script lint#153721Conversation
|
Some changes occurred in check-cfg diagnostics cc @Urgau |
262d2e2 to
3e99ab7
Compare
|
Added a ui test to ensure this lint is only emitted on cargo |
This comment has been minimized.
This comment has been minimized.
3e99ab7 to
ea1c733
Compare
This comment has been minimized.
This comment has been minimized.
| 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") | ||
| } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Just put it in t-cargo meeting agenda :)
next meeting time: Tue, Mar 17, 2026, 10:00 UTC-4
ea1c733 to
28c7221
Compare
This comment has been minimized.
This comment has been minimized.
|
CI is finally happy. ^^' |
|
The cargo team discussed this today (notes). Some comments from that discussion:
|
|
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
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. |
There was a problem hiding this comment.
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.
| declare_lint! { | ||
| /// Checks for usage of `#[cfg]`/`#[cfg_attr]`/`cfg!()` in `build.rs` scripts. | ||
| /// | ||
| /// ### Explanation | ||
| /// | ||
| /// It checks the `cfg` values for the *host*, not the target. For example, `cfg!(windows)` is | ||
| /// true when compiling on Windows, so it will give the wrong answer if you are cross compiling. | ||
| /// This is because build scripts run on the machine performing compilation, rather than on the | ||
| /// target. | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust,ignore (can only be run in cargo build scripts) | ||
| /// if cfg!(windows) {} | ||
| /// ``` | ||
| /// | ||
| /// Use instead: | ||
| /// | ||
| /// ```rust | ||
| /// if std::env::var("CARGO_CFG_WINDOWS").is_ok() {} | ||
| /// ``` | ||
| pub MISLEADING_CFG_IN_BUILD_SCRIPT, | ||
| Warn, | ||
| "use of host configs in `build.rs` scripts" | ||
| } |
There was a problem hiding this comment.
Most lints are defined directly in their file in this crate, so I'll keep the same logic.
| fn get_invalid_cfg_attrs(attr: &MetaItem, spans: &mut Vec<Span>, has_unknown: &mut bool) { | ||
| let Some(ident) = attr.ident() else { return }; | ||
| if [sym::unix, sym::windows].contains(&ident.name) { | ||
| spans.push(attr.span); | ||
| } else if attr.value_str().is_some() && ident.name.as_str().starts_with("target_") { | ||
| spans.push(attr.span); | ||
| } else if let Some(sub_attrs) = attr.meta_item_list() { | ||
| for sub_attr in sub_attrs { | ||
| if let Some(meta) = sub_attr.meta_item() { | ||
| get_invalid_cfg_attrs(meta, spans, has_unknown); | ||
| } | ||
| } | ||
| } else { | ||
| *has_unknown = true; | ||
| } | ||
| } |
There was a problem hiding this comment.
To be moved in parse_name_value:
| if !is_build_script(cx) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
To be removed since T-cargo is onboard with enabling the lint for build scripts on their side.
| if !is_build_script(cx) { | |
| return; | |
| } |
2d14a6b to
06e2527
Compare
This comment has been minimized.
This comment has been minimized.
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. |
|
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... ^^'). |
|
👋 Just wanted to weigh in wearing lang hat--
|
These tags put it on the agenda so it can be discussed with the team. That's all. |
Not very nice to call traviscross a bot. 😝
Noted, my bad. Please add them back then. |
|
Is there a reason this can't be implemented via the existing check-cfg checks? Do we not support opting out of permitting built-in cfgs via check-cfg? If it can be done via check-cfg, is the reason this is a new lint just for better error messages (since we can guide the user about the build script specific context)? |
|
I'm supposed to rewrite a big part of this lint to reuse existing |
|
This was discussed in the lang-team call today. The consensus on the call was as follows:
As usual, if multiple teams have purview, we could have all of them on the FCP, but that's pretty tedious. We think it's simpler overall for us to leave the final decision to y'all, as we did on RFC 3013. One last thing -- we've noticed that these kind of issues that lie at the intersection of multiple teams have been a source of friction lately. Might be a good thing to talk about at the All Hands. |
|
In the meeting, @nikomatsakis asked whether, setting aside jurisdictional questions, any of us had concerns about this lint. Nothing came to mind in the meeting. But looking at the situation afterward, I have some questions. Looking around GitHub briefly, these uses stood out to me as valid:
These came up immediately in my GH search; I didn't have to go digging. This makes me wonder about whether this lint would do more harm than good. It would seem, at least, that a crater run would be in order. |
I'm planning on doing one before asking the teams for approval, but I first wanted a vide-check from T-cargo. The implementation needs some work before we can do one though (renaming, removing the custom parser, ...). |
I don't think I follow this. |
It's checked during the AST -> attribute conversion. But considering I need to store some extra info, I'm not sure it's gonna be the right place. First I rewrite using the attribute parser, then I see if I can merge both codes. |
…t have a compiler output generated by the lint-docs script
This comment has been minimized.
This comment has been minimized.
b20987d to
1518fcc
Compare
|
Some changes occurred in compiler/rustc_attr_parsing |
|
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. |
|
The last commit is not finished at all. It's currently blocked on #155097. I'll also likely need to update the |
…, r=JonathanBrouwer Make `rustc_attr_parsing::SharedContext::emit_lint` take a `MultiSpan` instead of a `Span` I'll likely need it for rust-lang#153721 to allow emitting the lint on one attribute at a time instead of each of the wrong values. r? @JonathanBrouwer
Rollup merge of #155097 - GuillaumeGomez:emit_lint-multispan, r=JonathanBrouwer Make `rustc_attr_parsing::SharedContext::emit_lint` take a `MultiSpan` instead of a `Span` I'll likely need it for #153721 to allow emitting the lint on one attribute at a time instead of each of the wrong values. r? @JonathanBrouwer
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.rsfromcargocrate, we will check for anytarget_*,unixandwindowscfg and warn about them.Since you made the original review on clippy.
r? @Urgau