Skip to content

incompatible_msrv doesn't respect #[cfg(version(...))] #14827

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

Open
est31 opened this issue May 17, 2025 · 3 comments · May be fixed by #14940
Open

incompatible_msrv doesn't respect #[cfg(version(...))] #14827

est31 opened this issue May 17, 2025 · 3 comments · May be fixed by #14940

Comments

@est31
Copy link
Member

est31 commented May 17, 2025

The incompatible_msrv lint doesn't recognize that code is gated by #[cfg(version(...))], and complains about the usage anyways.

Take this code:

#![feature(cfg_version)]

pub fn maybe_pipe() {
    #[cfg(version("1.87"))]
    let _ = std::io::pipe(); // stabilized in 1.87.0
}

With this toml:

[package]
name = "cfg-version-test"
version = "0.1.0"
edition = "2024"
rust-version = "1.85.0"

[dependencies]

It will lint, even though the code is fine:

warning: current MSRV (Minimum Supported Rust Version) is `1.85.0` but this item is stable since `1.87.0`
 --> src/lib.rs:5:13
  |
5 |     let _ = std::io::pipe();
  |             ^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#incompatible_msrv
  = note: `#[warn(clippy::incompatible_msrv)]` on by default

cc rust-lang/rust#64796 #[cfg(version(...))] tracking issue

@Alexendoo
Copy link
Member

Was planning to quickly take care of that but any()/all() complicate it a bit. Just parsing the plain cfg(version) is an option

Up for grabs for now, may take another look at it next week if nobody else does in the meanwhile

@est31
Copy link
Member Author

est31 commented May 30, 2025

I think as a simplification, we can treat any and all the same: traverse them and the moment we find a cfg(version) we add it. it might cause some code to not be detected by incompatible_msrv but that's better than a false positive.

As for not, I would ignore anything directly inside not, but still traverse the children for any potential reappearances of not: a version(...) there should count again, i.e. if the number of not nestings is even, count version(...), if it's not even, don't count it.

Can be done via some recursive search. We are interested in the maximum number of this whole tree I guess, and use it as the MSRV of the whole block (if it's larger than the MSRV specified in Cargo.toml, if not then we take that one).

@Alexendoo if you did a PR to check for plain cfg(version), that would help a lot already. From there, I can implement the recursive search as a followup.

@Alexendoo
Copy link
Member

Opened #14940, handling any/etc was relatively straightforward whereas the sym::cfg_trace thing stumped me for a while

It picks the minimum version from the tree rather than maximum since that's the lowest version the code might be compiled with

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 a pull request may close this issue.

2 participants