Skip to content

Commit 69c5128

Browse files
committed
Auto merge of #9924 - Alexendoo:msrv-stack, r=Jarcho
Add `clippy_utils::msrv::Msrv` to keep track of the current MSRV changelog: Fix the scoping of the `#![clippy::msrv]` attribute Fixes #6920 r? `@Jarcho`
2 parents f6f6d54 + 637139d commit 69c5128

Some content is hidden

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

56 files changed

+466
-433
lines changed

book/src/development/adding_lints.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,22 +443,22 @@ value is passed to the constructor in `clippy_lints/lib.rs`.
443443

444444
```rust
445445
pub struct ManualStrip {
446-
msrv: Option<RustcVersion>,
446+
msrv: Msrv,
447447
}
448448

449449
impl ManualStrip {
450450
#[must_use]
451-
pub fn new(msrv: Option<RustcVersion>) -> Self {
451+
pub fn new(msrv: Msrv) -> Self {
452452
Self { msrv }
453453
}
454454
}
455455
```
456456

457457
The project's MSRV can then be matched against the feature MSRV in the LintPass
458-
using the `meets_msrv` utility function.
458+
using the `Msrv::meets` method.
459459

460460
``` rust
461-
if !meets_msrv(self.msrv, msrvs::STR_STRIP_PREFIX) {
461+
if !self.msrv.meets(msrvs::STR_STRIP_PREFIX) {
462462
return;
463463
}
464464
```

clippy_dev/src/new_lint.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn add_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
120120

121121
let new_lint = if enable_msrv {
122122
format!(
123-
"store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv)));\n ",
123+
"store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv())));\n ",
124124
lint_pass = lint.pass,
125125
ctor_arg = if lint.pass == "late" { "_" } else { "" },
126126
module_name = lint.name,
@@ -238,10 +238,9 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
238238
result.push_str(&if enable_msrv {
239239
formatdoc!(
240240
r#"
241-
use clippy_utils::msrvs;
241+
use clippy_utils::msrvs::{{self, Msrv}};
242242
{pass_import}
243243
use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
244-
use rustc_semver::RustcVersion;
245244
use rustc_session::{{declare_tool_lint, impl_lint_pass}};
246245
247246
"#
@@ -263,12 +262,12 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
263262
formatdoc!(
264263
r#"
265264
pub struct {name_camel} {{
266-
msrv: Option<RustcVersion>,
265+
msrv: Msrv,
267266
}}
268267
269268
impl {name_camel} {{
270269
#[must_use]
271-
pub fn new(msrv: Option<RustcVersion>) -> Self {{
270+
pub fn new(msrv: Msrv) -> Self {{
272271
Self {{ msrv }}
273272
}}
274273
}}
@@ -357,15 +356,14 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
357356
let _ = writedoc!(
358357
lint_file_contents,
359358
r#"
360-
use clippy_utils::{{meets_msrv, msrvs}};
359+
use clippy_utils::msrvs::{{self, Msrv}};
361360
use rustc_lint::{{{context_import}, LintContext}};
362-
use rustc_semver::RustcVersion;
363361
364362
use super::{name_upper};
365363
366364
// TODO: Adjust the parameters as necessary
367-
pub(super) fn check(cx: &{context_import}, msrv: Option<RustcVersion>) {{
368-
if !meets_msrv(msrv, todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{
365+
pub(super) fn check(cx: &{context_import}, msrv: &Msrv) {{
366+
if !msrv.meets(todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{
369367
return;
370368
}}
371369
todo!();

clippy_lints/src/almost_complete_letter_range.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::msrvs::{self, Msrv};
23
use clippy_utils::source::{trim_span, walk_span_to_context};
3-
use clippy_utils::{meets_msrv, msrvs};
44
use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits};
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
77
use rustc_middle::lint::in_external_macro;
8-
use rustc_semver::RustcVersion;
98
use rustc_session::{declare_tool_lint, impl_lint_pass};
109
use rustc_span::Span;
1110

@@ -33,10 +32,10 @@ declare_clippy_lint! {
3332
impl_lint_pass!(AlmostCompleteLetterRange => [ALMOST_COMPLETE_LETTER_RANGE]);
3433

3534
pub struct AlmostCompleteLetterRange {
36-
msrv: Option<RustcVersion>,
35+
msrv: Msrv,
3736
}
3837
impl AlmostCompleteLetterRange {
39-
pub fn new(msrv: Option<RustcVersion>) -> Self {
38+
pub fn new(msrv: Msrv) -> Self {
4039
Self { msrv }
4140
}
4241
}
@@ -46,7 +45,7 @@ impl EarlyLintPass for AlmostCompleteLetterRange {
4645
let ctxt = e.span.ctxt();
4746
let sugg = if let Some(start) = walk_span_to_context(start.span, ctxt)
4847
&& let Some(end) = walk_span_to_context(end.span, ctxt)
49-
&& meets_msrv(self.msrv, msrvs::RANGE_INCLUSIVE)
48+
&& self.msrv.meets(msrvs::RANGE_INCLUSIVE)
5049
{
5150
Some((trim_span(cx.sess().source_map(), start.between(end)), "..="))
5251
} else {
@@ -60,7 +59,7 @@ impl EarlyLintPass for AlmostCompleteLetterRange {
6059
if let PatKind::Range(Some(start), Some(end), kind) = &p.kind
6160
&& matches!(kind.node, RangeEnd::Excluded)
6261
{
63-
let sugg = if meets_msrv(self.msrv, msrvs::RANGE_INCLUSIVE) {
62+
let sugg = if self.msrv.meets(msrvs::RANGE_INCLUSIVE) {
6463
"..="
6564
} else {
6665
"..."

clippy_lints/src/approx_const.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::{meets_msrv, msrvs};
2+
use clippy_utils::msrvs::{self, Msrv};
33
use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
@@ -63,12 +63,12 @@ const KNOWN_CONSTS: [(f64, &str, usize, Option<RustcVersion>); 19] = [
6363
];
6464

6565
pub struct ApproxConstant {
66-
msrv: Option<RustcVersion>,
66+
msrv: Msrv,
6767
}
6868

6969
impl ApproxConstant {
7070
#[must_use]
71-
pub fn new(msrv: Option<RustcVersion>) -> Self {
71+
pub fn new(msrv: Msrv) -> Self {
7272
Self { msrv }
7373
}
7474

@@ -87,7 +87,7 @@ impl ApproxConstant {
8787
let s = s.as_str();
8888
if s.parse::<f64>().is_ok() {
8989
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
90-
if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| meets_msrv(self.msrv, msrv)) {
90+
if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| self.msrv.meets(msrv)) {
9191
span_lint_and_help(
9292
cx,
9393
APPROX_CONSTANT,

clippy_lints/src/attrs.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
44
use clippy_utils::macros::{is_panic, macro_backtrace};
5-
use clippy_utils::msrvs;
5+
use clippy_utils::msrvs::{self, Msrv};
66
use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments};
7-
use clippy_utils::{extract_msrv_attr, meets_msrv};
87
use if_chain::if_chain;
98
use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
109
use rustc_errors::Applicability;
@@ -14,7 +13,6 @@ use rustc_hir::{
1413
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext};
1514
use rustc_middle::lint::in_external_macro;
1615
use rustc_middle::ty;
17-
use rustc_semver::RustcVersion;
1816
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
1917
use rustc_span::source_map::Span;
2018
use rustc_span::symbol::Symbol;
@@ -599,7 +597,7 @@ fn is_word(nmi: &NestedMetaItem, expected: Symbol) -> bool {
599597
}
600598

601599
pub struct EarlyAttributes {
602-
pub msrv: Option<RustcVersion>,
600+
pub msrv: Msrv,
603601
}
604602

605603
impl_lint_pass!(EarlyAttributes => [
@@ -614,7 +612,7 @@ impl EarlyLintPass for EarlyAttributes {
614612
}
615613

616614
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
617-
check_deprecated_cfg_attr(cx, attr, self.msrv);
615+
check_deprecated_cfg_attr(cx, attr, &self.msrv);
618616
check_mismatched_target_os(cx, attr);
619617
}
620618

@@ -654,9 +652,9 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
654652
}
655653
}
656654

657-
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: Option<RustcVersion>) {
655+
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msrv) {
658656
if_chain! {
659-
if meets_msrv(msrv, msrvs::TOOL_ATTRIBUTES);
657+
if msrv.meets(msrvs::TOOL_ATTRIBUTES);
660658
// check cfg_attr
661659
if attr.has_name(sym::cfg_attr);
662660
if let Some(items) = attr.meta_item_list();

clippy_lints/src/casts/cast_abs_to_unsigned.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::msrvs::{self, Msrv};
23
use clippy_utils::sugg::Sugg;
3-
use clippy_utils::{meets_msrv, msrvs};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty::{self, Ty};
8-
use rustc_semver::RustcVersion;
98

109
use super::CAST_ABS_TO_UNSIGNED;
1110

@@ -15,9 +14,9 @@ pub(super) fn check(
1514
cast_expr: &Expr<'_>,
1615
cast_from: Ty<'_>,
1716
cast_to: Ty<'_>,
18-
msrv: Option<RustcVersion>,
17+
msrv: &Msrv,
1918
) {
20-
if meets_msrv(msrv, msrvs::UNSIGNED_ABS)
19+
if msrv.meets(msrvs::UNSIGNED_ABS)
2120
&& let ty::Int(from) = cast_from.kind()
2221
&& let ty::Uint(to) = cast_to.kind()
2322
&& let ExprKind::MethodCall(method_path, receiver, ..) = cast_expr.kind

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::in_constant;
3+
use clippy_utils::msrvs::{self, Msrv};
24
use clippy_utils::source::snippet_opt;
35
use clippy_utils::ty::is_isize_or_usize;
4-
use clippy_utils::{in_constant, meets_msrv, msrvs};
56
use rustc_errors::Applicability;
67
use rustc_hir::{Expr, ExprKind};
78
use rustc_lint::LateContext;
89
use rustc_middle::ty::{self, FloatTy, Ty};
9-
use rustc_semver::RustcVersion;
1010

1111
use super::{utils, CAST_LOSSLESS};
1212

@@ -16,7 +16,7 @@ pub(super) fn check(
1616
cast_op: &Expr<'_>,
1717
cast_from: Ty<'_>,
1818
cast_to: Ty<'_>,
19-
msrv: Option<RustcVersion>,
19+
msrv: &Msrv,
2020
) {
2121
if !should_lint(cx, expr, cast_from, cast_to, msrv) {
2222
return;
@@ -57,13 +57,7 @@ pub(super) fn check(
5757
);
5858
}
5959

60-
fn should_lint(
61-
cx: &LateContext<'_>,
62-
expr: &Expr<'_>,
63-
cast_from: Ty<'_>,
64-
cast_to: Ty<'_>,
65-
msrv: Option<RustcVersion>,
66-
) -> bool {
60+
fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
6761
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
6862
if in_constant(cx, expr.hir_id) {
6963
return false;
@@ -89,7 +83,7 @@ fn should_lint(
8983
};
9084
!is_isize_or_usize(cast_from) && from_nbits < to_nbits
9185
},
92-
(false, true) if matches!(cast_from.kind(), ty::Bool) && meets_msrv(msrv, msrvs::FROM_BOOL) => true,
86+
(false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(msrvs::FROM_BOOL) => true,
9387
(_, _) => {
9488
matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64))
9589
},

clippy_lints/src/casts/cast_slice_different_sizes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use clippy_utils::{diagnostics::span_lint_and_then, meets_msrv, msrvs, source};
1+
use clippy_utils::msrvs::{self, Msrv};
2+
use clippy_utils::{diagnostics::span_lint_and_then, source};
23
use if_chain::if_chain;
34
use rustc_ast::Mutability;
45
use rustc_hir::{Expr, ExprKind, Node};
56
use rustc_lint::LateContext;
67
use rustc_middle::ty::{self, layout::LayoutOf, Ty, TypeAndMut};
7-
use rustc_semver::RustcVersion;
88

99
use super::CAST_SLICE_DIFFERENT_SIZES;
1010

11-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Option<RustcVersion>) {
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv) {
1212
// suggestion is invalid if `ptr::slice_from_raw_parts` does not exist
13-
if !meets_msrv(msrv, msrvs::PTR_SLICE_RAW_PARTS) {
13+
if !msrv.meets(msrvs::PTR_SLICE_RAW_PARTS) {
1414
return;
1515
}
1616

clippy_lints/src/casts/cast_slice_from_raw_parts.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::msrvs::{self, Msrv};
23
use clippy_utils::source::snippet_with_applicability;
3-
use clippy_utils::{match_def_path, meets_msrv, msrvs, paths};
4+
use clippy_utils::{match_def_path, paths};
45
use if_chain::if_chain;
56
use rustc_errors::Applicability;
67
use rustc_hir::{def_id::DefId, Expr, ExprKind};
78
use rustc_lint::LateContext;
89
use rustc_middle::ty::{self, Ty};
9-
use rustc_semver::RustcVersion;
1010

1111
use super::CAST_SLICE_FROM_RAW_PARTS;
1212

@@ -25,15 +25,9 @@ fn raw_parts_kind(cx: &LateContext<'_>, did: DefId) -> Option<RawPartsKind> {
2525
}
2626
}
2727

28-
pub(super) fn check(
29-
cx: &LateContext<'_>,
30-
expr: &Expr<'_>,
31-
cast_expr: &Expr<'_>,
32-
cast_to: Ty<'_>,
33-
msrv: Option<RustcVersion>,
34-
) {
28+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>, msrv: &Msrv) {
3529
if_chain! {
36-
if meets_msrv(msrv, msrvs::PTR_SLICE_RAW_PARTS);
30+
if msrv.meets(msrvs::PTR_SLICE_RAW_PARTS);
3731
if let ty::RawPtr(ptrty) = cast_to.kind();
3832
if let ty::Slice(_) = ptrty.ty.kind();
3933
if let ExprKind::Call(fun, [ptr_arg, len_arg]) = cast_expr.peel_blocks().kind;

0 commit comments

Comments
 (0)