Skip to content

Commit 32c51d2

Browse files
committed
Auto merge of #6201 - Suyash458:master, r=flip1995
Add support for minimum supported rust version add configuration option for minimum supported rust version add msrv attribute to some lints listed in #6097 add tests addresses #6097 changelog: Add `msrv` configuration to Clippy. This should get a longer changelog entry.
2 parents f897d27 + d06076c commit 32c51d2

22 files changed

+427
-22
lines changed

clippy_lints/src/lib.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern crate rustc_target;
4444
extern crate rustc_trait_selection;
4545
extern crate rustc_typeck;
4646

47+
use crate::utils::parse_msrv;
4748
use rustc_data_structures::fx::FxHashSet;
4849
use rustc_lint::LintId;
4950
use rustc_session::Session;
@@ -933,7 +934,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
933934
&zero_div_zero::ZERO_DIVIDED_BY_ZERO,
934935
]);
935936
// end register lints, do not remove this comment, it’s used in `update_lints`
936-
937937
store.register_late_pass(|| box await_holding_invalid::AwaitHolding);
938938
store.register_late_pass(|| box serde_api::SerdeAPI);
939939
store.register_late_pass(|| box utils::internal_lints::CompilerLintFunctions::new());
@@ -969,7 +969,23 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
969969
store.register_late_pass(|| box strings::StringAdd);
970970
store.register_late_pass(|| box implicit_return::ImplicitReturn);
971971
store.register_late_pass(|| box implicit_saturating_sub::ImplicitSaturatingSub);
972-
store.register_late_pass(|| box methods::Methods);
972+
973+
let parsed_msrv = conf.msrv.as_ref().and_then(|s| {
974+
parse_msrv(s, None, None).or_else(|| {
975+
sess.err(&format!("error reading Clippy's configuration file. `{}` is not a valid Rust version", s));
976+
None
977+
})
978+
});
979+
980+
let msrv = parsed_msrv.clone();
981+
store.register_late_pass(move || box methods::Methods::new(msrv.clone()));
982+
let msrv = parsed_msrv.clone();
983+
store.register_late_pass(move || box matches::Matches::new(msrv.clone()));
984+
let msrv = parsed_msrv.clone();
985+
store.register_early_pass(move || box manual_non_exhaustive::ManualNonExhaustive::new(msrv.clone()));
986+
let msrv = parsed_msrv;
987+
store.register_late_pass(move || box manual_strip::ManualStrip::new(msrv.clone()));
988+
973989
store.register_late_pass(|| box map_clone::MapClone);
974990
store.register_late_pass(|| box map_err_ignore::MapErrIgnore);
975991
store.register_late_pass(|| box shadow::Shadow);
@@ -983,7 +999,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
983999
store.register_late_pass(|| box types::Casts);
9841000
let type_complexity_threshold = conf.type_complexity_threshold;
9851001
store.register_late_pass(move || box types::TypeComplexity::new(type_complexity_threshold));
986-
store.register_late_pass(|| box matches::Matches::default());
9871002
store.register_late_pass(|| box minmax::MinMaxPass);
9881003
store.register_late_pass(|| box open_options::OpenOptions);
9891004
store.register_late_pass(|| box zero_div_zero::ZeroDiv);
@@ -1144,7 +1159,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11441159
store.register_late_pass(|| box if_let_mutex::IfLetMutex);
11451160
store.register_late_pass(|| box mut_mutex_lock::MutMutexLock);
11461161
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
1147-
store.register_early_pass(|| box manual_non_exhaustive::ManualNonExhaustive);
11481162
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
11491163
store.register_early_pass(|| box redundant_field_names::RedundantFieldNames);
11501164
store.register_late_pass(|| box vec_resize_to_zero::VecResizeToZero);
@@ -1166,7 +1180,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11661180
store.register_late_pass(|| box manual_ok_or::ManualOkOr);
11671181
store.register_late_pass(|| box float_equality_without_abs::FloatEqualityWithoutAbs);
11681182
store.register_late_pass(|| box async_yields_async::AsyncYieldsAsync);
1169-
store.register_late_pass(|| box manual_strip::ManualStrip);
11701183
store.register_late_pass(|| box utils::internal_lints::MatchTypeOnDiagItem);
11711184
let disallowed_methods = conf.disallowed_methods.iter().cloned().collect::<FxHashSet<_>>();
11721185
store.register_late_pass(move || box disallowed_method::DisallowedMethod::new(&disallowed_methods));

clippy_lints/src/manual_non_exhaustive.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
use crate::utils::{snippet_opt, span_lint_and_then};
1+
use crate::utils::{meets_msrv, snippet_opt, span_lint_and_then};
22
use if_chain::if_chain;
33
use rustc_ast::ast::{Attribute, Item, ItemKind, StructField, Variant, VariantData, VisibilityKind};
44
use rustc_attr as attr;
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass};
7-
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_session::{declare_tool_lint, impl_lint_pass};
88
use rustc_span::{sym, Span};
9+
use semver::{Version, VersionReq};
10+
11+
const MANUAL_NON_EXHAUSTIVE_MSRV: Version = Version {
12+
major: 1,
13+
minor: 40,
14+
patch: 0,
15+
pre: Vec::new(),
16+
build: Vec::new(),
17+
};
918

1019
declare_clippy_lint! {
1120
/// **What it does:** Checks for manual implementations of the non-exhaustive pattern.
@@ -55,10 +64,26 @@ declare_clippy_lint! {
5564
"manual implementations of the non-exhaustive pattern can be simplified using #[non_exhaustive]"
5665
}
5766

58-
declare_lint_pass!(ManualNonExhaustive => [MANUAL_NON_EXHAUSTIVE]);
67+
#[derive(Clone)]
68+
pub struct ManualNonExhaustive {
69+
msrv: Option<VersionReq>,
70+
}
71+
72+
impl ManualNonExhaustive {
73+
#[must_use]
74+
pub fn new(msrv: Option<VersionReq>) -> Self {
75+
Self { msrv }
76+
}
77+
}
78+
79+
impl_lint_pass!(ManualNonExhaustive => [MANUAL_NON_EXHAUSTIVE]);
5980

6081
impl EarlyLintPass for ManualNonExhaustive {
6182
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
83+
if !meets_msrv(self.msrv.as_ref(), &MANUAL_NON_EXHAUSTIVE_MSRV) {
84+
return;
85+
}
86+
6287
match &item.kind {
6388
ItemKind::Enum(def, _) => {
6489
check_manual_non_exhaustive_enum(cx, item, &def.variants);
@@ -73,6 +98,8 @@ impl EarlyLintPass for ManualNonExhaustive {
7398
_ => {},
7499
}
75100
}
101+
102+
extract_msrv_attr!(EarlyContext);
76103
}
77104

78105
fn check_manual_non_exhaustive_enum(cx: &EarlyContext<'_>, item: &Item, variants: &[Variant]) {

clippy_lints/src/manual_strip.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::consts::{constant, Constant};
22
use crate::utils::usage::mutated_variables;
33
use crate::utils::{
4-
eq_expr_value, higher, match_def_path, multispan_sugg, paths, qpath_res, snippet, span_lint_and_then,
4+
eq_expr_value, higher, match_def_path, meets_msrv, multispan_sugg, paths, qpath_res, snippet, span_lint_and_then,
55
};
66

77
use if_chain::if_chain;
@@ -10,12 +10,21 @@ use rustc_hir::def::Res;
1010
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
1111
use rustc_hir::BinOpKind;
1212
use rustc_hir::{BorrowKind, Expr, ExprKind};
13-
use rustc_lint::{LateContext, LateLintPass};
13+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1414
use rustc_middle::hir::map::Map;
1515
use rustc_middle::ty;
16-
use rustc_session::{declare_lint_pass, declare_tool_lint};
16+
use rustc_session::{declare_tool_lint, impl_lint_pass};
1717
use rustc_span::source_map::Spanned;
1818
use rustc_span::Span;
19+
use semver::{Version, VersionReq};
20+
21+
const MANUAL_STRIP_MSRV: Version = Version {
22+
major: 1,
23+
minor: 45,
24+
patch: 0,
25+
pre: Vec::new(),
26+
build: Vec::new(),
27+
};
1928

2029
declare_clippy_lint! {
2130
/// **What it does:**
@@ -51,7 +60,18 @@ declare_clippy_lint! {
5160
"suggests using `strip_{prefix,suffix}` over `str::{starts,ends}_with` and slicing"
5261
}
5362

54-
declare_lint_pass!(ManualStrip => [MANUAL_STRIP]);
63+
pub struct ManualStrip {
64+
msrv: Option<VersionReq>,
65+
}
66+
67+
impl ManualStrip {
68+
#[must_use]
69+
pub fn new(msrv: Option<VersionReq>) -> Self {
70+
Self { msrv }
71+
}
72+
}
73+
74+
impl_lint_pass!(ManualStrip => [MANUAL_STRIP]);
5575

5676
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5777
enum StripKind {
@@ -61,6 +81,10 @@ enum StripKind {
6181

6282
impl<'tcx> LateLintPass<'tcx> for ManualStrip {
6383
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
84+
if !meets_msrv(self.msrv.as_ref(), &MANUAL_STRIP_MSRV) {
85+
return;
86+
}
87+
6488
if_chain! {
6589
if let Some((cond, then, _)) = higher::if_block(&expr);
6690
if let ExprKind::MethodCall(_, _, [target_arg, pattern], _) = cond.kind;
@@ -114,6 +138,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
114138
}
115139
}
116140
}
141+
142+
extract_msrv_attr!(LateContext);
117143
}
118144

119145
// Returns `Some(arg)` if `expr` matches `arg.len()` and `None` otherwise.

clippy_lints/src/matches.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::utils::sugg::Sugg;
33
use crate::utils::usage::is_unused;
44
use crate::utils::{
55
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable,
6-
is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, multispan_sugg, remove_blocks, snippet,
7-
snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
6+
is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, meets_msrv, multispan_sugg, remove_blocks,
7+
snippet, snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
88
span_lint_and_then,
99
};
1010
use crate::utils::{paths, search_same, SpanlessEq, SpanlessHash};
@@ -23,6 +23,7 @@ use rustc_middle::ty::{self, Ty, TyS};
2323
use rustc_session::{declare_tool_lint, impl_lint_pass};
2424
use rustc_span::source_map::{Span, Spanned};
2525
use rustc_span::{sym, Symbol};
26+
use semver::{Version, VersionReq};
2627
use std::cmp::Ordering;
2728
use std::collections::hash_map::Entry;
2829
use std::collections::Bound;
@@ -527,9 +528,20 @@ declare_clippy_lint! {
527528

528529
#[derive(Default)]
529530
pub struct Matches {
531+
msrv: Option<VersionReq>,
530532
infallible_destructuring_match_linted: bool,
531533
}
532534

535+
impl Matches {
536+
#[must_use]
537+
pub fn new(msrv: Option<VersionReq>) -> Self {
538+
Self {
539+
msrv,
540+
..Matches::default()
541+
}
542+
}
543+
}
544+
533545
impl_lint_pass!(Matches => [
534546
SINGLE_MATCH,
535547
MATCH_REF_PATS,
@@ -549,14 +561,27 @@ impl_lint_pass!(Matches => [
549561
MATCH_SAME_ARMS,
550562
]);
551563

564+
const MATCH_LIKE_MATCHES_MACRO_MSRV: Version = Version {
565+
major: 1,
566+
minor: 42,
567+
patch: 0,
568+
pre: Vec::new(),
569+
build: Vec::new(),
570+
};
571+
552572
impl<'tcx> LateLintPass<'tcx> for Matches {
553573
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
554574
if in_external_macro(cx.sess(), expr.span) || in_macro(expr.span) {
555575
return;
556576
}
557577

558578
redundant_pattern_match::check(cx, expr);
559-
if !check_match_like_matches(cx, expr) {
579+
580+
if meets_msrv(self.msrv.as_ref(), &MATCH_LIKE_MATCHES_MACRO_MSRV) {
581+
if !check_match_like_matches(cx, expr) {
582+
lint_match_arms(cx, expr);
583+
}
584+
} else {
560585
lint_match_arms(cx, expr);
561586
}
562587

@@ -640,6 +665,8 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
640665
}
641666
}
642667
}
668+
669+
extract_msrv_attr!(LateContext);
643670
}
644671

645672
#[rustfmt::skip]

clippy_lints/src/methods/mod.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
2020
use rustc_middle::hir::map::Map;
2121
use rustc_middle::lint::in_external_macro;
2222
use rustc_middle::ty::{self, TraitRef, Ty, TyS};
23-
use rustc_session::{declare_lint_pass, declare_tool_lint};
23+
use rustc_session::{declare_tool_lint, impl_lint_pass};
2424
use rustc_span::source_map::Span;
2525
use rustc_span::symbol::{sym, SymbolStr};
2626

@@ -30,10 +30,11 @@ use crate::utils::usage::mutated_variables;
3030
use crate::utils::{
3131
contains_ty, get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro,
3232
is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath,
33-
match_trait_method, match_type, match_var, method_calls, method_chain_args, paths, remove_blocks, return_ty,
34-
single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
33+
match_trait_method, match_type, match_var, meets_msrv, method_calls, method_chain_args, paths, remove_blocks,
34+
return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
3535
span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg, walk_ptrs_ty_depth, SpanlessEq,
3636
};
37+
use semver::{Version, VersionReq};
3738

3839
declare_clippy_lint! {
3940
/// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
@@ -1404,7 +1405,18 @@ declare_clippy_lint! {
14041405
"use `.collect()` instead of `::from_iter()`"
14051406
}
14061407

1407-
declare_lint_pass!(Methods => [
1408+
pub struct Methods {
1409+
msrv: Option<VersionReq>,
1410+
}
1411+
1412+
impl Methods {
1413+
#[must_use]
1414+
pub fn new(msrv: Option<VersionReq>) -> Self {
1415+
Self { msrv }
1416+
}
1417+
}
1418+
1419+
impl_lint_pass!(Methods => [
14081420
UNWRAP_USED,
14091421
EXPECT_USED,
14101422
SHOULD_IMPLEMENT_TRAIT,
@@ -1531,8 +1543,12 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
15311543
check_pointer_offset(cx, expr, arg_lists[0])
15321544
},
15331545
["is_file", ..] => lint_filetype_is_file(cx, expr, arg_lists[0]),
1534-
["map", "as_ref"] => lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], false),
1535-
["map", "as_mut"] => lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], true),
1546+
["map", "as_ref"] => {
1547+
lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], false, self.msrv.as_ref())
1548+
},
1549+
["map", "as_mut"] => {
1550+
lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], true, self.msrv.as_ref())
1551+
},
15361552
["unwrap_or_else", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "unwrap_or"),
15371553
["get_or_insert_with", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "get_or_insert"),
15381554
["ok_or_else", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "ok_or"),
@@ -1738,6 +1754,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17381754
}
17391755
}
17401756
}
1757+
1758+
extract_msrv_attr!(LateContext);
17411759
}
17421760

17431761
/// Checks for the `OR_FUN_CALL` lint.
@@ -3453,14 +3471,27 @@ fn lint_suspicious_map(cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
34533471
);
34543472
}
34553473

3474+
const OPTION_AS_REF_DEREF_MSRV: Version = Version {
3475+
major: 1,
3476+
minor: 40,
3477+
patch: 0,
3478+
pre: Vec::new(),
3479+
build: Vec::new(),
3480+
};
3481+
34563482
/// lint use of `_.as_ref().map(Deref::deref)` for `Option`s
34573483
fn lint_option_as_ref_deref<'tcx>(
34583484
cx: &LateContext<'tcx>,
34593485
expr: &hir::Expr<'_>,
34603486
as_ref_args: &[hir::Expr<'_>],
34613487
map_args: &[hir::Expr<'_>],
34623488
is_mut: bool,
3489+
msrv: Option<&VersionReq>,
34633490
) {
3491+
if !meets_msrv(msrv, &OPTION_AS_REF_DEREF_MSRV) {
3492+
return;
3493+
}
3494+
34643495
let same_mutability = |m| (is_mut && m == &hir::Mutability::Mut) || (!is_mut && m == &hir::Mutability::Not);
34653496

34663497
let option_ty = cx.typeck_results().expr_ty(&as_ref_args[0]);

0 commit comments

Comments
 (0)