Skip to content

Commit 902d326

Browse files
committed
Migrate clippy_lints to new MSRV API
1 parent 77ee982 commit 902d326

File tree

89 files changed

+356
-537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+356
-537
lines changed

clippy_lints/src/almost_complete_range.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::msrvs::{self, Msrv};
3+
use clippy_utils::msrvs::{self, MsrvStack};
44
use clippy_utils::source::{trim_span, walk_span_to_context};
55
use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits};
66
use rustc_errors::Applicability;
@@ -32,12 +32,12 @@ declare_clippy_lint! {
3232
impl_lint_pass!(AlmostCompleteRange => [ALMOST_COMPLETE_RANGE]);
3333

3434
pub struct AlmostCompleteRange {
35-
msrv: Msrv,
35+
msrv: MsrvStack,
3636
}
3737
impl AlmostCompleteRange {
3838
pub fn new(conf: &'static Conf) -> Self {
3939
Self {
40-
msrv: conf.msrv.clone(),
40+
msrv: MsrvStack::new(conf.msrv),
4141
}
4242
}
4343
}
@@ -97,7 +97,7 @@ impl EarlyLintPass for AlmostCompleteRange {
9797
}
9898
}
9999

100-
extract_msrv_attr!(EarlyContext);
100+
extract_msrv_attr!();
101101
}
102102

103103
fn is_incomplete_range(start: &Expr, end: &Expr) -> bool {

clippy_lints/src/approx_const.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ pub struct ApproxConstant {
6969

7070
impl ApproxConstant {
7171
pub fn new(conf: &'static Conf) -> Self {
72-
Self {
73-
msrv: conf.msrv.clone(),
74-
}
72+
Self { msrv: conf.msrv }
7573
}
7674

7775
fn check_lit(&self, cx: &LateContext<'_>, lit: &LitKind, e: &Expr<'_>) {
@@ -92,7 +90,7 @@ impl ApproxConstant {
9290
let s = s.as_str();
9391
if s.parse::<f64>().is_ok() {
9492
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
95-
if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(msrv)) {
93+
if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(cx, msrv)) {
9694
span_lint_and_help(
9795
cx,
9896
APPROX_CONSTANT,
@@ -116,8 +114,6 @@ impl<'tcx> LateLintPass<'tcx> for ApproxConstant {
116114
self.check_lit(cx, &lit.node, e);
117115
}
118116
}
119-
120-
extract_msrv_attr!(LateContext);
121117
}
122118

123119
/// Returns `false` if the number of significant figures in `value` are

clippy_lints/src/assigning_clones.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ pub struct AssigningClones {
5959

6060
impl AssigningClones {
6161
pub fn new(conf: &'static Conf) -> Self {
62-
Self {
63-
msrv: conf.msrv.clone(),
64-
}
62+
Self { msrv: conf.msrv }
6563
}
6664
}
6765

@@ -90,7 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
9088
sym::clone if is_diag_trait_item(cx, fn_id, sym::Clone) => CloneTrait::Clone,
9189
_ if fn_name.as_str() == "to_owned"
9290
&& is_diag_trait_item(cx, fn_id, sym::ToOwned)
93-
&& self.msrv.meets(msrvs::CLONE_INTO) =>
91+
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
9492
{
9593
CloneTrait::ToOwned
9694
},
@@ -143,8 +141,6 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
143141
);
144142
}
145143
}
146-
147-
extract_msrv_attr!(LateContext);
148144
}
149145

150146
/// Checks if the data being cloned borrows from the place that is being assigned to:

clippy_lints/src/attrs/deprecated_cfg_attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use super::{Attribute, DEPRECATED_CFG_ATTR, DEPRECATED_CLIPPY_CFG_ATTR, unnecessary_clippy_cfg};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::msrvs::{self, Msrv};
3+
use clippy_utils::msrvs::{self, MsrvStack};
44
use rustc_ast::AttrStyle;
55
use rustc_errors::Applicability;
66
use rustc_lint::EarlyContext;
77
use rustc_span::sym;
88

9-
pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msrv) {
9+
pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &MsrvStack) {
1010
// check cfg_attr
1111
if attr.has_name(sym::cfg_attr)
1212
&& let Some(items) = attr.meta_item_list()

clippy_lints/src/attrs/mod.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod useless_attribute;
1414
mod utils;
1515

1616
use clippy_config::Conf;
17-
use clippy_utils::msrvs::{self, Msrv};
17+
use clippy_utils::msrvs::{self, Msrv, MsrvStack};
1818
use rustc_ast::{self as ast, Attribute, MetaItemInner, MetaItemKind};
1919
use rustc_hir::{ImplItem, Item, TraitItem};
2020
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
@@ -459,9 +459,7 @@ impl_lint_pass!(Attributes => [
459459

460460
impl Attributes {
461461
pub fn new(conf: &'static Conf) -> Self {
462-
Self {
463-
msrv: conf.msrv.clone(),
464-
}
462+
Self { msrv: conf.msrv }
465463
}
466464
}
467465

@@ -471,7 +469,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
471469
if is_relevant_item(cx, item) {
472470
inline_always::check(cx, item.span, item.ident.name, attrs);
473471
}
474-
repr_attributes::check(cx, item.span, attrs, &self.msrv);
472+
repr_attributes::check(cx, item.span, attrs, self.msrv);
475473
}
476474

477475
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
@@ -485,18 +483,16 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
485483
inline_always::check(cx, item.span, item.ident.name, cx.tcx.hir().attrs(item.hir_id()));
486484
}
487485
}
488-
489-
extract_msrv_attr!(LateContext);
490486
}
491487

492488
pub struct EarlyAttributes {
493-
msrv: Msrv,
489+
msrv: MsrvStack,
494490
}
495491

496492
impl EarlyAttributes {
497493
pub fn new(conf: &'static Conf) -> Self {
498494
Self {
499-
msrv: conf.msrv.clone(),
495+
msrv: MsrvStack::new(conf.msrv),
500496
}
501497
}
502498
}
@@ -515,17 +511,17 @@ impl EarlyLintPass for EarlyAttributes {
515511
non_minimal_cfg::check(cx, attr);
516512
}
517513

518-
extract_msrv_attr!(EarlyContext);
514+
extract_msrv_attr!();
519515
}
520516

521517
pub struct PostExpansionEarlyAttributes {
522-
msrv: Msrv,
518+
msrv: MsrvStack,
523519
}
524520

525521
impl PostExpansionEarlyAttributes {
526522
pub fn new(conf: &'static Conf) -> Self {
527523
Self {
528-
msrv: conf.msrv.clone(),
524+
msrv: MsrvStack::new(conf.msrv),
529525
}
530526
}
531527
}
@@ -589,5 +585,5 @@ impl EarlyLintPass for PostExpansionEarlyAttributes {
589585
duplicated_attributes::check(cx, &item.attrs);
590586
}
591587

592-
extract_msrv_attr!(EarlyContext);
588+
extract_msrv_attr!();
593589
}

clippy_lints/src/attrs/repr_attributes.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@ use rustc_lint::LateContext;
33
use rustc_span::{Span, sym};
44

55
use clippy_utils::diagnostics::span_lint_and_then;
6-
use clippy_utils::msrvs;
6+
use clippy_utils::msrvs::{self, Msrv};
77

88
use super::REPR_PACKED_WITHOUT_ABI;
99

10-
pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute], msrv: &msrvs::Msrv) {
11-
if msrv.meets(msrvs::REPR_RUST) {
12-
check_packed(cx, item_span, attrs);
13-
}
14-
}
15-
16-
fn check_packed(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) {
10+
pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute], msrv: Msrv) {
1711
if let Some(items) = attrs.iter().find_map(|attr| {
1812
if attr.ident().is_some_and(|ident| matches!(ident.name, sym::repr)) {
1913
attr.meta_item_list()
@@ -27,6 +21,7 @@ fn check_packed(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) {
2721
item.ident()
2822
.is_some_and(|ident| matches!(ident.name, sym::C | sym::Rust))
2923
})
24+
&& msrv.meets(cx, msrvs::REPR_RUST)
3025
{
3126
span_lint_and_then(
3227
cx,

clippy_lints/src/booleans.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ pub struct NonminimalBool {
8484

8585
impl NonminimalBool {
8686
pub fn new(conf: &'static Conf) -> Self {
87-
Self {
88-
msrv: conf.msrv.clone(),
89-
}
87+
Self { msrv: conf.msrv }
9088
}
9189
}
9290

@@ -102,7 +100,7 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
102100
_: Span,
103101
_: LocalDefId,
104102
) {
105-
NonminimalBoolVisitor { cx, msrv: &self.msrv }.visit_body(body);
103+
NonminimalBoolVisitor { cx, msrv: self.msrv }.visit_body(body);
106104
}
107105

108106
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
@@ -119,8 +117,6 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
119117
_ => {},
120118
}
121119
}
122-
123-
extract_msrv_attr!(LateContext);
124120
}
125121

126122
fn inverted_bin_op_eq_str(op: BinOpKind) -> Option<&'static str> {
@@ -197,7 +193,7 @@ fn check_inverted_bool_in_condition(
197193
);
198194
}
199195

200-
fn check_simplify_not(cx: &LateContext<'_>, msrv: &Msrv, expr: &Expr<'_>) {
196+
fn check_simplify_not(cx: &LateContext<'_>, msrv: Msrv, expr: &Expr<'_>) {
201197
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind
202198
&& !expr.span.from_expansion()
203199
&& !inner.span.from_expansion()
@@ -233,7 +229,7 @@ fn check_simplify_not(cx: &LateContext<'_>, msrv: &Msrv, expr: &Expr<'_>) {
233229

234230
struct NonminimalBoolVisitor<'a, 'tcx> {
235231
cx: &'a LateContext<'tcx>,
236-
msrv: &'a Msrv,
232+
msrv: Msrv,
237233
}
238234

239235
use quine_mc_cluskey::Bool;
@@ -326,7 +322,7 @@ impl<'v> Hir2Qmm<'_, '_, 'v> {
326322
struct SuggestContext<'a, 'tcx, 'v> {
327323
terminals: &'v [&'v Expr<'v>],
328324
cx: &'a LateContext<'tcx>,
329-
msrv: &'a Msrv,
325+
msrv: Msrv,
330326
output: String,
331327
}
332328

@@ -396,7 +392,7 @@ impl SuggestContext<'_, '_, '_> {
396392
}
397393
}
398394

399-
fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Option<String> {
395+
fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Option<String> {
400396
match &expr.kind {
401397
ExprKind::Binary(binop, lhs, rhs) => {
402398
if !implements_ord(cx, lhs) {
@@ -438,7 +434,9 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Opti
438434
.iter()
439435
.copied()
440436
.flat_map(|(msrv, a, b)| vec![(msrv, a, b), (msrv, b, a)])
441-
.find(|&(msrv, a, _)| msrv.is_none_or(|msrv| curr_msrv.meets(msrv)) && a == path.ident.name.as_str())
437+
.find(|&(msrv, a, _)| {
438+
a == path.ident.name.as_str() && msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv))
439+
})
442440
.and_then(|(_, _, neg_method)| {
443441
let negated_args = args
444442
.iter()
@@ -467,7 +465,7 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Opti
467465
}
468466
}
469467

470-
fn suggest(cx: &LateContext<'_>, msrv: &Msrv, suggestion: &Bool, terminals: &[&Expr<'_>]) -> String {
468+
fn suggest(cx: &LateContext<'_>, msrv: Msrv, suggestion: &Bool, terminals: &[&Expr<'_>]) -> String {
471469
let mut suggest_context = SuggestContext {
472470
terminals,
473471
cx,

clippy_lints/src/casts/borrow_as_ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(super) fn check<'tcx>(
1616
expr: &'tcx Expr<'_>,
1717
cast_expr: &'tcx Expr<'_>,
1818
cast_to: &'tcx Ty<'_>,
19-
msrv: &Msrv,
19+
msrv: Msrv,
2020
) -> bool {
2121
if matches!(cast_to.kind, TyKind::Ptr(_))
2222
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
@@ -34,7 +34,7 @@ pub(super) fn check<'tcx>(
3434
return false;
3535
}
3636

37-
let (suggestion, span) = if msrv.meets(msrvs::RAW_REF_OP) {
37+
let (suggestion, span) = if msrv.meets(cx, msrvs::RAW_REF_OP) {
3838
let operator_kind = match mutability {
3939
Mutability::Not => "const",
4040
Mutability::Mut => "mut",

clippy_lints/src/casts/cast_abs_to_unsigned.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ pub(super) fn check(
1414
cast_expr: &Expr<'_>,
1515
cast_from: Ty<'_>,
1616
cast_to: Ty<'_>,
17-
msrv: &Msrv,
17+
msrv: Msrv,
1818
) {
19-
if msrv.meets(msrvs::UNSIGNED_ABS)
20-
&& let ty::Int(from) = cast_from.kind()
19+
if let ty::Int(from) = cast_from.kind()
2120
&& let ty::Uint(to) = cast_to.kind()
2221
&& let ExprKind::MethodCall(method_path, receiver, [], _) = cast_expr.kind
2322
&& method_path.ident.name.as_str() == "abs"
23+
&& msrv.meets(cx, msrvs::UNSIGNED_ABS)
2424
{
2525
let span = if from.bit_width() == to.bit_width() {
2626
expr.span

clippy_lints/src/casts/cast_lossless.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(super) fn check(
1919
cast_from: Ty<'_>,
2020
cast_to: Ty<'_>,
2121
cast_to_hir: &rustc_hir::Ty<'_>,
22-
msrv: &Msrv,
22+
msrv: Msrv,
2323
) {
2424
if !should_lint(cx, cast_from, cast_to, msrv) {
2525
return;
@@ -70,7 +70,7 @@ pub(super) fn check(
7070
);
7171
}
7272

73-
fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
73+
fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: Msrv) -> bool {
7474
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
7575
if is_in_const_context(cx) {
7676
return false;
@@ -96,7 +96,7 @@ fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &
9696
};
9797
!is_isize_or_usize(cast_from) && from_nbits < to_nbits
9898
},
99-
(false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(msrvs::FROM_BOOL) => true,
99+
(false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(cx, msrvs::FROM_BOOL) => true,
100100
(_, _) => {
101101
matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64))
102102
},

clippy_lints/src/casts/cast_slice_different_sizes.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ use rustc_middle::ty::{self, Ty, TypeAndMut};
99

1010
use super::CAST_SLICE_DIFFERENT_SIZES;
1111

12-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv) {
13-
// suggestion is invalid if `ptr::slice_from_raw_parts` does not exist
14-
if !msrv.meets(msrvs::PTR_SLICE_RAW_PARTS) {
15-
return;
16-
}
17-
12+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv) {
1813
// if this cast is the child of another cast expression then don't emit something for it, the full
1914
// chain will be analyzed
2015
if is_child_of_cast(cx, expr) {
@@ -30,7 +25,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv
3025
if let (Ok(from_layout), Ok(to_layout)) = (cx.layout_of(start_ty.ty), cx.layout_of(end_ty.ty)) {
3126
let from_size = from_layout.size.bytes();
3227
let to_size = to_layout.size.bytes();
33-
if from_size != to_size && from_size != 0 && to_size != 0 {
28+
if from_size != to_size && from_size != 0 && to_size != 0 && msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS) {
3429
span_lint_and_then(
3530
cx,
3631
CAST_SLICE_DIFFERENT_SIZES,

0 commit comments

Comments
 (0)