Skip to content

Commit bb8fb44

Browse files
committed
Add ignore-msrv-check-for option to incompatible_msrv lint
This option prevents the lint from triggering on entities that may have been feature gated by the user and thus don't need to be warned about.
1 parent 8178530 commit bb8fb44

File tree

9 files changed

+75
-5
lines changed

9 files changed

+75
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6360,6 +6360,7 @@ Released 2018-09-13
63606360
[`excessive-nesting-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#excessive-nesting-threshold
63616361
[`future-size-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#future-size-threshold
63626362
[`ignore-interior-mutability`]: https://doc.rust-lang.org/clippy/lint_configuration.html#ignore-interior-mutability
6363+
[`ignore-msrv-check-for`]: https://doc.rust-lang.org/clippy/lint_configuration.html#ignore-msrv-check-for
63636364
[`large-error-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#large-error-threshold
63646365
[`lint-inconsistent-struct-field-initializers`]: https://doc.rust-lang.org/clippy/lint_configuration.html#lint-inconsistent-struct-field-initializers
63656366
[`literal-representation-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#literal-representation-threshold

book/src/lint_configuration.md

+17
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,23 @@ A list of paths to types that should be treated as if they do not contain interi
603603
* [`mutable_key_type`](https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type)
604604

605605

606+
## `ignore-msrv-check-for`
607+
A list of path to items that should not be checked for a compatible MSRV. This can be used to ignore
608+
MSRV checks for code which is gated by a feature which depend on the version of the Rust compiler.
609+
610+
#### Example
611+
612+
```toml
613+
ignore-msrv-check-for = [ "str::split_once", "std::option::Option::as_slice" ]
614+
```
615+
616+
**Default Value:** `[]`
617+
618+
---
619+
**Affected lints:**
620+
* [`incompatible_msrv`](https://rust-lang.github.io/rust-clippy/master/index.html#incompatible_msrv)
621+
622+
606623
## `large-error-threshold`
607624
The maximum size of the `Err`-variant in a `Result` returned from a function
608625

clippy_config/src/conf.rs

+11
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,17 @@ define_Conf! {
539539
/// A list of paths to types that should be treated as if they do not contain interior mutability
540540
#[lints(borrow_interior_mutable_const, declare_interior_mutable_const, ifs_same_cond, mutable_key_type)]
541541
ignore_interior_mutability: Vec<String> = Vec::from(["bytes::Bytes".into()]),
542+
/// A list of path to items that should not be checked for a compatible MSRV. This can be used to ignore
543+
/// MSRV checks for code which is gated by a feature which depends on the version of the Rust compiler.
544+
///
545+
/// #### Example
546+
///
547+
/// ```toml
548+
/// # Ignore those as we use them only when our `modern_compiler` feature is active.
549+
/// ignore-msrv-check-for = [ "str::split_once", "std::option::Option::as_slice" ]
550+
/// ```
551+
#[lints(incompatible_msrv)]
552+
ignore_msrv_check_for: Vec<String> = Vec::new(),
542553
/// The maximum size of the `Err`-variant in a `Result` returned from a function
543554
#[lints(result_large_err)]
544555
large_error_threshold: u64 = 128,

clippy_lints/src/incompatible_msrv.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::is_in_test;
43
use clippy_utils::msrvs::Msrv;
4+
use clippy_utils::{def_path_def_ids, is_in_test};
55
use rustc_attr_parsing::{RustcVersion, StabilityLevel, StableSince};
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_hir::{Expr, ExprKind, HirId};
@@ -43,16 +43,23 @@ pub struct IncompatibleMsrv {
4343
msrv: Msrv,
4444
is_above_msrv: FxHashMap<DefId, RustcVersion>,
4545
check_in_tests: bool,
46+
ignored_def_ids: Vec<DefId>,
4647
}
4748

4849
impl_lint_pass!(IncompatibleMsrv => [INCOMPATIBLE_MSRV]);
4950

5051
impl IncompatibleMsrv {
51-
pub fn new(conf: &'static Conf) -> Self {
52+
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
53+
let ignored_def_ids = conf
54+
.ignore_msrv_check_for
55+
.iter()
56+
.flat_map(|x| def_path_def_ids(tcx, &x.split("::").collect::<Vec<_>>()))
57+
.collect();
5258
Self {
5359
msrv: conf.msrv,
5460
is_above_msrv: FxHashMap::default(),
5561
check_in_tests: conf.check_incompatible_msrv_in_tests,
62+
ignored_def_ids,
5663
}
5764
}
5865

@@ -84,8 +91,9 @@ impl IncompatibleMsrv {
8491
}
8592

8693
fn emit_lint_if_under_msrv(&mut self, cx: &LateContext<'_>, def_id: DefId, node: HirId, span: Span) {
87-
if def_id.is_local() {
88-
// We don't check local items since their MSRV is supposed to always be valid.
94+
// We don't check local items since their MSRV is supposed to always be valid, nor ignored items
95+
// which may be feature-gated.
96+
if def_id.is_local() || self.ignored_def_ids.contains(&def_id) {
8997
return;
9098
}
9199
if let ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) = span.ctxt().outer_expn_data().kind {

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
955955
store.register_late_pass(|_| Box::<unconditional_recursion::UnconditionalRecursion>::default());
956956
store.register_late_pass(move |_| Box::new(pub_underscore_fields::PubUnderscoreFields::new(conf)));
957957
store.register_late_pass(move |_| Box::new(missing_const_for_thread_local::MissingConstForThreadLocal::new(conf)));
958-
store.register_late_pass(move |_| Box::new(incompatible_msrv::IncompatibleMsrv::new(conf)));
958+
store.register_late_pass(move |tcx| Box::new(incompatible_msrv::IncompatibleMsrv::new(tcx, conf)));
959959
store.register_late_pass(|_| Box::new(to_string_trait_impl::ToStringTraitImpl));
960960
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
961961
store.register_late_pass(move |_| Box::new(assigning_clones::AssigningClones::new(conf)));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignore-msrv-check-for = ["str::split_once", "std::option::Option::as_slice"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![warn(clippy::incompatible_msrv)]
2+
3+
#[clippy::msrv = "1.46"]
4+
fn main() {
5+
if let Some((a, b)) = "foo:bar".split_once(":") {
6+
println!("a = {a}, b = {b}");
7+
}
8+
9+
let x: Option<u32> = Some(42u32);
10+
for i in x.as_slice() {
11+
println!("i = {i}");
12+
}
13+
14+
if x.is_none_or(|x| x + 2 == 17) {
15+
//~^ incompatible_msrv
16+
println!("impossible");
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: current MSRV (Minimum Supported Rust Version) is `1.46.0` but this item is stable since `1.82.0`
2+
--> tests/ui-toml/incompatible_msrv/incompatible_msrv.rs:14:10
3+
|
4+
LL | if x.is_none_or(|x| x + 2 == 17) {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::incompatible-msrv` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::incompatible_msrv)]`
9+
10+
error: aborting due to 1 previous error
11+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
4848
excessive-nesting-threshold
4949
future-size-threshold
5050
ignore-interior-mutability
51+
ignore-msrv-check-for
5152
large-error-threshold
5253
lint-inconsistent-struct-field-initializers
5354
literal-representation-threshold
@@ -140,6 +141,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
140141
excessive-nesting-threshold
141142
future-size-threshold
142143
ignore-interior-mutability
144+
ignore-msrv-check-for
143145
large-error-threshold
144146
lint-inconsistent-struct-field-initializers
145147
literal-representation-threshold
@@ -232,6 +234,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
232234
excessive-nesting-threshold
233235
future-size-threshold
234236
ignore-interior-mutability
237+
ignore-msrv-check-for
235238
large-error-threshold
236239
lint-inconsistent-struct-field-initializers
237240
literal-representation-threshold

0 commit comments

Comments
 (0)