Skip to content

Add ignore-msrv-check-for option to incompatible_msrv lint #14328

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6360,6 +6360,7 @@ Released 2018-09-13
[`excessive-nesting-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#excessive-nesting-threshold
[`future-size-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#future-size-threshold
[`ignore-interior-mutability`]: https://doc.rust-lang.org/clippy/lint_configuration.html#ignore-interior-mutability
[`ignore-msrv-check-for`]: https://doc.rust-lang.org/clippy/lint_configuration.html#ignore-msrv-check-for
[`large-error-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#large-error-threshold
[`lint-inconsistent-struct-field-initializers`]: https://doc.rust-lang.org/clippy/lint_configuration.html#lint-inconsistent-struct-field-initializers
[`literal-representation-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#literal-representation-threshold
Expand Down
18 changes: 18 additions & 0 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,24 @@ A list of paths to types that should be treated as if they do not contain interi
* [`mutable_key_type`](https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type)


## `ignore-msrv-check-for`
A list of path to items that should not be checked for a compatible MSRV. This can be used to ignore
MSRV checks for code which is gated by a feature which depends on the version of the Rust compiler.

#### Example

```toml
# Ignore those as we use them only when our `modern_compiler` feature is active.
ignore-msrv-check-for = [ "str::split_once", "std::option::Option::as_slice" ]
```

**Default Value:** `[]`

---
**Affected lints:**
* [`incompatible_msrv`](https://rust-lang.github.io/rust-clippy/master/index.html#incompatible_msrv)


## `large-error-threshold`
The maximum size of the `Err`-variant in a `Result` returned from a function

Expand Down
11 changes: 11 additions & 0 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,17 @@ define_Conf! {
/// A list of paths to types that should be treated as if they do not contain interior mutability
#[lints(borrow_interior_mutable_const, declare_interior_mutable_const, ifs_same_cond, mutable_key_type)]
ignore_interior_mutability: Vec<String> = Vec::from(["bytes::Bytes".into()]),
/// A list of path to items that should not be checked for a compatible MSRV. This can be used to ignore
/// MSRV checks for code which is gated by a feature which depends on the version of the Rust compiler.
///
/// #### Example
///
/// ```toml
/// # Ignore those as we use them only when our `modern_compiler` feature is active.
/// ignore-msrv-check-for = [ "str::split_once", "std::option::Option::as_slice" ]
/// ```
#[lints(incompatible_msrv)]
ignore_msrv_check_for: Vec<String> = Vec::new(),
/// The maximum size of the `Err`-variant in a `Result` returned from a function
#[lints(result_large_err)]
large_error_threshold: u64 = 128,
Expand Down
16 changes: 12 additions & 4 deletions clippy_lints/src/incompatible_msrv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_in_test;
use clippy_utils::msrvs::Msrv;
use clippy_utils::{def_path_def_ids, is_in_test};
use rustc_attr_parsing::{RustcVersion, StabilityLevel, StableSince};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::{Expr, ExprKind, HirId};
Expand Down Expand Up @@ -43,16 +43,23 @@ pub struct IncompatibleMsrv {
msrv: Msrv,
is_above_msrv: FxHashMap<DefId, RustcVersion>,
check_in_tests: bool,
ignored_def_ids: Vec<DefId>,
}

impl_lint_pass!(IncompatibleMsrv => [INCOMPATIBLE_MSRV]);

impl IncompatibleMsrv {
pub fn new(conf: &'static Conf) -> Self {
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
let ignored_def_ids = conf
.ignore_msrv_check_for
.iter()
.flat_map(|x| def_path_def_ids(tcx, &x.split("::").collect::<Vec<_>>()))
.collect();
Self {
msrv: conf.msrv,
is_above_msrv: FxHashMap::default(),
check_in_tests: conf.check_incompatible_msrv_in_tests,
ignored_def_ids,
}
}

Expand Down Expand Up @@ -84,8 +91,9 @@ impl IncompatibleMsrv {
}

fn emit_lint_if_under_msrv(&mut self, cx: &LateContext<'_>, def_id: DefId, node: HirId, span: Span) {
if def_id.is_local() {
// We don't check local items since their MSRV is supposed to always be valid.
// We don't check local items since their MSRV is supposed to always be valid, nor ignored items
// which may be feature-gated.
if def_id.is_local() || self.ignored_def_ids.contains(&def_id) {
return;
}
if let ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) = span.ctxt().outer_expn_data().kind {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(|_| Box::<unconditional_recursion::UnconditionalRecursion>::default());
store.register_late_pass(move |_| Box::new(pub_underscore_fields::PubUnderscoreFields::new(conf)));
store.register_late_pass(move |_| Box::new(missing_const_for_thread_local::MissingConstForThreadLocal::new(conf)));
store.register_late_pass(move |_| Box::new(incompatible_msrv::IncompatibleMsrv::new(conf)));
store.register_late_pass(move |tcx| Box::new(incompatible_msrv::IncompatibleMsrv::new(tcx, conf)));
store.register_late_pass(|_| Box::new(to_string_trait_impl::ToStringTraitImpl));
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
store.register_late_pass(move |_| Box::new(assigning_clones::AssigningClones::new(conf)));
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/incompatible_msrv/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore-msrv-check-for = ["str::split_once", "std::option::Option::as_slice"]
18 changes: 18 additions & 0 deletions tests/ui-toml/incompatible_msrv/incompatible_msrv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![warn(clippy::incompatible_msrv)]

#[clippy::msrv = "1.46"]
fn main() {
if let Some((a, b)) = "foo:bar".split_once(":") {
Copy link
Member

Choose a reason for hiding this comment

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

This should definitely include a cfg(feature) IMO so as to show the intent of this config option. Not ignoring the check when there's zero cfg(feature) attributes is an option too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In #14425 there is a case for ignoring the MSRV check without having a crate-local feature enabled.

println!("a = {a}, b = {b}");
}

let x: Option<u32> = Some(42u32);
for i in x.as_slice() {
println!("i = {i}");
}

if x.is_none_or(|x| x + 2 == 17) {
//~^ incompatible_msrv
println!("impossible");
}
}
11 changes: 11 additions & 0 deletions tests/ui-toml/incompatible_msrv/incompatible_msrv.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: current MSRV (Minimum Supported Rust Version) is `1.46.0` but this item is stable since `1.82.0`
--> tests/ui-toml/incompatible_msrv/incompatible_msrv.rs:14:10
|
LL | if x.is_none_or(|x| x + 2 == 17) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::incompatible-msrv` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::incompatible_msrv)]`

error: aborting due to 1 previous error

3 changes: 3 additions & 0 deletions tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
excessive-nesting-threshold
future-size-threshold
ignore-interior-mutability
ignore-msrv-check-for
large-error-threshold
lint-inconsistent-struct-field-initializers
literal-representation-threshold
Expand Down Expand Up @@ -140,6 +141,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
excessive-nesting-threshold
future-size-threshold
ignore-interior-mutability
ignore-msrv-check-for
large-error-threshold
lint-inconsistent-struct-field-initializers
literal-representation-threshold
Expand Down Expand Up @@ -232,6 +234,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
excessive-nesting-threshold
future-size-threshold
ignore-interior-mutability
ignore-msrv-check-for
large-error-threshold
lint-inconsistent-struct-field-initializers
literal-representation-threshold
Expand Down