Skip to content

Various improvements to the incompatible_msrv lint#14433

Merged
Jarcho merged 6 commits intorust-lang:masterfrom
samueltardieu:push-lorsokqwwpny
Jul 16, 2025
Merged

Various improvements to the incompatible_msrv lint#14433
Jarcho merged 6 commits intorust-lang:masterfrom
samueltardieu:push-lorsokqwwpny

Conversation

@samueltardieu
Copy link
Copy Markdown
Member

This PR supersedes #14328 following the 2025-03-18 Clippy meeting discussion. It uses a simpler approach than what was proposed initially in #14328 and does not add new options.

First, it documents how cfg_attr can be used to change the MSRV considered by Clippy to trigger the lint. This allows the MSRV to be feature gated, or to be raised in tests.

Also, the lint stops warning about items which have been explicitly allowed through a rustc feature. This works even if the feature has been stabilized since. It allows using an older compiler with some features turned on, as is done in Rust for Linux. This fixes #14425.

Then, if the lint triggers, and it looks like the code is located below a cfg or cfg_attr attribute, an additional note is issued, once, to indicate that the clippy::msrv attribute can be controlled by an attribute.

Finally, the lint is extended to cover any path, not just method and function calls. For example, enumeration variants, or constants, were not MSRV checked. This required replacing two u32::MAX by u32::max_value() in MSRV-limited tests.

An extra commit adds a TODO for checking the const stability also, as this is not done right now.

@Centri3 I'll assign this to you because you were assigned #14328 and you were the one who nominated the issue for discussion (thanks!), but of course feel free to reroll!

r? @Centri3

changelog: [incompatible_msrv]: better documentation, honor the features attribute, and lint non-function entities as well

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 18, 2025
Comment thread clippy_lints/src/incompatible_msrv.rs Outdated
@samueltardieu samueltardieu force-pushed the push-lorsokqwwpny branch 3 times, most recently from 2c30fe4 to 1296d5e Compare March 20, 2025 22:32
@samueltardieu
Copy link
Copy Markdown
Member Author

Rebased after rustup

Comment thread clippy_lints/src/incompatible_msrv.rs Outdated

/// Heuristic checking if the node `hir_id` is under a `#[cfg()]` or `#[cfg_attr()]`
/// attribute.
fn is_under_cfg_attribute(cx: &LateContext<'_>, hir_id: HirId) -> bool {
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.

Should this only add the note under cfg(feature)? I think this is only useful otherwise for cfg(test), which feels like an edge case to me IMO. I feel like it's silly to even lint in tests, but what are your thoughts here?

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.

My rationale was that the lint only triggers when a MSRV is set on the current crate. However, when features are used, the author might want to express the fact that, in some configurations, they can go beyond the default MSRV. If the used entity is not behind a crate feature or a cfg item (possibly set by the build script), then it is likely that this is a real issue with the default MSRV, and it is probably a bad idea to suggest silencing Clippy with a #[clippy::msrv] attribute there.

For example, anyhow has a MSRV of 1.39, but when compiled with more recent compilers, it takes advantage of std::backtrace::Backtrace. Those uses are cfg gated. I feel this is the best place to make the suggestion without risking making a counterproductive one.

Comment thread tests/ui/incompatible_msrv.rs Outdated
Comment thread clippy_lints/src/incompatible_msrv.rs
Comment thread clippy_lints/src/incompatible_msrv.rs Outdated
@Centri3
Copy link
Copy Markdown
Member

Centri3 commented Mar 30, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 30, 2025
@samueltardieu
Copy link
Copy Markdown
Member Author

@rustbot review

@rustbot rustbot 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 from the author. (Use `@rustbot ready` to update this status) labels Mar 31, 2025
@samueltardieu
Copy link
Copy Markdown
Member Author

Rebased, and adapted to the newer Rust sources: symbols for #[cfg] and #[cfg_attr] are now sym::cfg_trace and sym::cfg_attr_trace.

@samueltardieu
Copy link
Copy Markdown
Member Author

r? clippy

@rustbot rustbot assigned flip1995 and unassigned Centri3 May 7, 2025
@rustbot

This comment has been minimized.

The first non-MSRV-compatible item located under a `#[cfg(…)]` or
`#[cfg_attr(…)]` attribute will get an additional note suggesting
changing `#[clippy::msrv]` locally.
A later commit will introduce the non-linting of feature-enabled items.
Switch to public items from `core` for the tests as to not interfere.
If an item has been enabled through a feature, it will not be linted
even though the MSRV is not compatible. This use case may happen when
stable compilers are allowed to enable unstable features, e.g. in
the Rust for Linux toolchain.
New Rust releases also stabilize enum variants, constants, and so on.
Lint them as well.
Also, update error marker for consistency.
@samueltardieu
Copy link
Copy Markdown
Member Author

Rebased

@samueltardieu
Copy link
Copy Markdown
Member Author

r? clippy

@rustbot rustbot assigned Jarcho and unassigned flip1995 May 26, 2025
@samueltardieu
Copy link
Copy Markdown
Member Author

Ping @Jarcho

Copy link
Copy Markdown
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

Thank you.

@Jarcho Jarcho added this pull request to the merge queue Jul 16, 2025
Merged via the queue into rust-lang:master with commit c0dc3b6 Jul 16, 2025
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 16, 2025
Copy link
Copy Markdown
Contributor

@tamird tamird left a comment

Choose a reason for hiding this comment

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

Thank you @samueltardieu!


impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
// TODO: check for const stability when in const context
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this limitation documented? Might be good to file an issue.

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.

I've opened #15297 about it, thanks for reminding me of it.

@samueltardieu samueltardieu deleted the push-lorsokqwwpny branch July 16, 2025 17:05
ojeda added a commit to Rust-for-Linux/linux that referenced this pull request Apr 7, 2026
`clippy::incompatible_msrv` is not buying us much, and we discussed
allowing it several times in the past.

For instance, there was recently another patch sent to `allow` it where
needed [1]. While that particular case would not be needed after the
minimum version bump to 1.85.0, it is simpler to just allow it to prevent
future instances.

[ In addition, the lint fired without taking into account the features
  that have been enabled in a crate [2]. While this was improved in Rust
  1.90.0 [3], it would still fire in a case like this patch. ]

Thus do so, and remove the last instance of locally allowing it we have
in the tree (except the one in the vendored `proc_macro2` crate).

Note that we still keep the `msrv` config option in `clippy.toml` since
that affects other lints as well.

Link: https://lore.kernel.org/rust-for-linux/20260404212831.78971-4-jhubbard@nvidia.com/ [1]
Link: rust-lang/rust-clippy#14425 [2]
Link: rust-lang/rust-clippy#14433 [3]
Link: https://patch.msgid.link/20260405235309.418950-8-ojeda@kernel.org
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Tamir Duberstein <tamird@kernel.org>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

incompatible_msrv false positive: enabled unstable feature

6 participants