Skip to content

Commit b3d6d6e

Browse files
authored
Rollup merge of rust-lang#6395 - Suyash458:master, r=flip1995
switch Version/VersionReq usages to RustcVersion add `rustc-semver` to dependencies switch `Version/VersionReq` usages to `RustcVersion` changelog: none
2 parents c3db082 + e7258ac commit b3d6d6e

10 files changed

+103
-58
lines changed

clippy_lints/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ smallvec = { version = "1", features = ["union"] }
2828
toml = "0.5.3"
2929
unicode-normalization = "0.1"
3030
semver = "0.11"
31+
rustc-semver="1.1.0"
3132
# NOTE: cargo requires serde feat in its url dep
3233
# see <https://github.com/rust-lang/rust/pull/63587#issuecomment-522343864>
3334
url = { version = "2.1.0", features = ["serde"] }

clippy_lints/src/lib.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -984,22 +984,17 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
984984
store.register_late_pass(|| box implicit_return::ImplicitReturn);
985985
store.register_late_pass(|| box implicit_saturating_sub::ImplicitSaturatingSub);
986986

987-
let parsed_msrv = conf.msrv.as_ref().and_then(|s| {
987+
let msrv = conf.msrv.as_ref().and_then(|s| {
988988
parse_msrv(s, None, None).or_else(|| {
989989
sess.err(&format!("error reading Clippy's configuration file. `{}` is not a valid Rust version", s));
990990
None
991991
})
992992
});
993993

994-
let msrv = parsed_msrv.clone();
995-
store.register_late_pass(move || box methods::Methods::new(msrv.clone()));
996-
let msrv = parsed_msrv.clone();
997-
store.register_late_pass(move || box matches::Matches::new(msrv.clone()));
998-
let msrv = parsed_msrv.clone();
999-
store.register_early_pass(move || box manual_non_exhaustive::ManualNonExhaustive::new(msrv.clone()));
1000-
let msrv = parsed_msrv;
1001-
store.register_late_pass(move || box manual_strip::ManualStrip::new(msrv.clone()));
1002-
994+
store.register_late_pass(move || box methods::Methods::new(msrv));
995+
store.register_late_pass(move || box matches::Matches::new(msrv));
996+
store.register_early_pass(move || box manual_non_exhaustive::ManualNonExhaustive::new(msrv));
997+
store.register_late_pass(move || box manual_strip::ManualStrip::new(msrv));
1003998
store.register_late_pass(|| box map_clone::MapClone);
1004999
store.register_late_pass(|| box map_err_ignore::MapErrIgnore);
10051000
store.register_late_pass(|| box shadow::Shadow);

clippy_lints/src/manual_non_exhaustive.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@ use rustc_ast::ast::{Attribute, Item, ItemKind, StructField, Variant, VariantDat
44
use rustc_attr as attr;
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass};
7+
use rustc_semver::RustcVersion;
78
use rustc_session::{declare_tool_lint, impl_lint_pass};
89
use rustc_span::{sym, Span};
9-
use semver::{Version, VersionReq};
1010

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-
};
11+
const MANUAL_NON_EXHAUSTIVE_MSRV: RustcVersion = RustcVersion::new(1, 40, 0);
1812

1913
declare_clippy_lint! {
2014
/// **What it does:** Checks for manual implementations of the non-exhaustive pattern.
@@ -66,12 +60,12 @@ declare_clippy_lint! {
6660

6761
#[derive(Clone)]
6862
pub struct ManualNonExhaustive {
69-
msrv: Option<VersionReq>,
63+
msrv: Option<RustcVersion>,
7064
}
7165

7266
impl ManualNonExhaustive {
7367
#[must_use]
74-
pub fn new(msrv: Option<VersionReq>) -> Self {
68+
pub fn new(msrv: Option<RustcVersion>) -> Self {
7569
Self { msrv }
7670
}
7771
}

clippy_lints/src/manual_strip.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@ use rustc_hir::{BorrowKind, Expr, ExprKind};
1313
use rustc_lint::{LateContext, LateLintPass, LintContext};
1414
use rustc_middle::hir::map::Map;
1515
use rustc_middle::ty;
16+
use rustc_semver::RustcVersion;
1617
use rustc_session::{declare_tool_lint, impl_lint_pass};
1718
use rustc_span::source_map::Spanned;
1819
use rustc_span::Span;
19-
use semver::{Version, VersionReq};
2020

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-
};
21+
const MANUAL_STRIP_MSRV: RustcVersion = RustcVersion::new(1, 45, 0);
2822

2923
declare_clippy_lint! {
3024
/// **What it does:**
@@ -61,12 +55,12 @@ declare_clippy_lint! {
6155
}
6256

6357
pub struct ManualStrip {
64-
msrv: Option<VersionReq>,
58+
msrv: Option<RustcVersion>,
6559
}
6660

6761
impl ManualStrip {
6862
#[must_use]
69-
pub fn new(msrv: Option<VersionReq>) -> Self {
63+
pub fn new(msrv: Option<RustcVersion>) -> Self {
7064
Self { msrv }
7165
}
7266
}

clippy_lints/src/matches.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use rustc_hir::{
2020
use rustc_lint::{LateContext, LateLintPass, LintContext};
2121
use rustc_middle::lint::in_external_macro;
2222
use rustc_middle::ty::{self, Ty, TyS};
23+
use rustc_semver::RustcVersion;
2324
use rustc_session::{declare_tool_lint, impl_lint_pass};
2425
use rustc_span::source_map::{Span, Spanned};
2526
use rustc_span::{sym, Symbol};
26-
use semver::{Version, VersionReq};
2727
use std::cmp::Ordering;
2828
use std::collections::hash_map::Entry;
2929
use std::collections::Bound;
@@ -535,13 +535,13 @@ declare_clippy_lint! {
535535

536536
#[derive(Default)]
537537
pub struct Matches {
538-
msrv: Option<VersionReq>,
538+
msrv: Option<RustcVersion>,
539539
infallible_destructuring_match_linted: bool,
540540
}
541541

542542
impl Matches {
543543
#[must_use]
544-
pub fn new(msrv: Option<VersionReq>) -> Self {
544+
pub fn new(msrv: Option<RustcVersion>) -> Self {
545545
Self {
546546
msrv,
547547
..Matches::default()
@@ -568,13 +568,7 @@ impl_lint_pass!(Matches => [
568568
MATCH_SAME_ARMS,
569569
]);
570570

571-
const MATCH_LIKE_MATCHES_MACRO_MSRV: Version = Version {
572-
major: 1,
573-
minor: 42,
574-
patch: 0,
575-
pre: Vec::new(),
576-
build: Vec::new(),
577-
};
571+
const MATCH_LIKE_MATCHES_MACRO_MSRV: RustcVersion = RustcVersion::new(1, 42, 0);
578572

579573
impl<'tcx> LateLintPass<'tcx> for Matches {
580574
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {

clippy_lints/src/methods/mod.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_hir::{TraitItem, TraitItemKind};
1818
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
1919
use rustc_middle::lint::in_external_macro;
2020
use rustc_middle::ty::{self, TraitRef, Ty, TyS};
21+
use rustc_semver::RustcVersion;
2122
use rustc_session::{declare_tool_lint, impl_lint_pass};
2223
use rustc_span::source_map::Span;
2324
use rustc_span::symbol::{sym, SymbolStr};
@@ -33,7 +34,6 @@ use crate::utils::{
3334
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg,
3435
walk_ptrs_ty_depth, SpanlessEq,
3536
};
36-
use semver::{Version, VersionReq};
3737

3838
declare_clippy_lint! {
3939
/// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
@@ -1405,12 +1405,12 @@ declare_clippy_lint! {
14051405
}
14061406

14071407
pub struct Methods {
1408-
msrv: Option<VersionReq>,
1408+
msrv: Option<RustcVersion>,
14091409
}
14101410

14111411
impl Methods {
14121412
#[must_use]
1413-
pub fn new(msrv: Option<VersionReq>) -> Self {
1413+
pub fn new(msrv: Option<RustcVersion>) -> Self {
14141414
Self { msrv }
14151415
}
14161416
}
@@ -3470,13 +3470,7 @@ fn lint_suspicious_map(cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
34703470
);
34713471
}
34723472

3473-
const OPTION_AS_REF_DEREF_MSRV: Version = Version {
3474-
major: 1,
3475-
minor: 40,
3476-
patch: 0,
3477-
pre: Vec::new(),
3478-
build: Vec::new(),
3479-
};
3473+
const OPTION_AS_REF_DEREF_MSRV: RustcVersion = RustcVersion::new(1, 40, 0);
34803474

34813475
/// lint use of `_.as_ref().map(Deref::deref)` for `Option`s
34823476
fn lint_option_as_ref_deref<'tcx>(
@@ -3485,7 +3479,7 @@ fn lint_option_as_ref_deref<'tcx>(
34853479
as_ref_args: &[hir::Expr<'_>],
34863480
map_args: &[hir::Expr<'_>],
34873481
is_mut: bool,
3488-
msrv: Option<&VersionReq>,
3482+
msrv: Option<&RustcVersion>,
34893483
) {
34903484
if !meets_msrv(msrv, &OPTION_AS_REF_DEREF_MSRV) {
34913485
return;

clippy_lints/src/utils/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use rustc_lint::{LateContext, Level, Lint, LintContext};
5252
use rustc_middle::hir::map::Map;
5353
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
5454
use rustc_middle::ty::{self, layout::IntegerExt, Ty, TyCtxt, TypeFoldable};
55+
use rustc_semver::RustcVersion;
5556
use rustc_session::Session;
5657
use rustc_span::hygiene::{ExpnKind, MacroKind};
5758
use rustc_span::source_map::original_sp;
@@ -60,13 +61,12 @@ use rustc_span::symbol::{self, kw, Symbol};
6061
use rustc_span::{BytePos, Pos, Span, DUMMY_SP};
6162
use rustc_target::abi::Integer;
6263
use rustc_trait_selection::traits::query::normalize::AtExt;
63-
use semver::{Version, VersionReq};
6464
use smallvec::SmallVec;
6565

6666
use crate::consts::{constant, Constant};
6767

68-
pub fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option<Span>) -> Option<VersionReq> {
69-
if let Ok(version) = VersionReq::parse(msrv) {
68+
pub fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option<Span>) -> Option<RustcVersion> {
69+
if let Ok(version) = RustcVersion::parse(msrv) {
7070
return Some(version);
7171
} else if let Some(sess) = sess {
7272
if let Some(span) = span {
@@ -76,8 +76,8 @@ pub fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option<Span>) -> Opt
7676
None
7777
}
7878

79-
pub fn meets_msrv(msrv: Option<&VersionReq>, lint_msrv: &Version) -> bool {
80-
msrv.map_or(true, |msrv| !msrv.matches(lint_msrv))
79+
pub fn meets_msrv(msrv: Option<&RustcVersion>, lint_msrv: &RustcVersion) -> bool {
80+
msrv.map_or(true, |msrv| msrv.meets(*lint_msrv))
8181
}
8282

8383
macro_rules! extract_msrv_attr {

tests/ui/min_rust_version_attr.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn match_same_arms2() {
3535
};
3636
}
3737

38-
fn manual_strip_msrv() {
38+
pub fn manual_strip_msrv() {
3939
let s = "hello, world!";
4040
if s.starts_with("hello, ") {
4141
assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
@@ -49,3 +49,39 @@ fn main() {
4949
match_same_arms2();
5050
manual_strip_msrv();
5151
}
52+
53+
mod meets_msrv {
54+
#![feature(custom_inner_attributes)]
55+
#![clippy::msrv = "1.45.0"]
56+
57+
fn main() {
58+
let s = "hello, world!";
59+
if s.starts_with("hello, ") {
60+
assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
61+
}
62+
}
63+
}
64+
65+
mod just_under_msrv {
66+
#![feature(custom_inner_attributes)]
67+
#![clippy::msrv = "1.46.0"]
68+
69+
fn main() {
70+
let s = "hello, world!";
71+
if s.starts_with("hello, ") {
72+
assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
73+
}
74+
}
75+
}
76+
77+
mod just_above_msrv {
78+
#![feature(custom_inner_attributes)]
79+
#![clippy::msrv = "1.44.0"]
80+
81+
fn main() {
82+
let s = "hello, world!";
83+
if s.starts_with("hello, ") {
84+
assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
85+
}
86+
}
87+
}

tests/ui/min_rust_version_attr.stderr

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error: stripping a prefix manually
2+
--> $DIR/min_rust_version_attr.rs:60:24
3+
|
4+
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::manual-strip` implied by `-D warnings`
8+
note: the prefix was tested here
9+
--> $DIR/min_rust_version_attr.rs:59:9
10+
|
11+
LL | if s.starts_with("hello, ") {
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
help: try using the `strip_prefix` method
14+
|
15+
LL | if let Some(<stripped>) = s.strip_prefix("hello, ") {
16+
LL | assert_eq!(<stripped>.to_uppercase(), "WORLD!");
17+
|
18+
19+
error: stripping a prefix manually
20+
--> $DIR/min_rust_version_attr.rs:72:24
21+
|
22+
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
23+
| ^^^^^^^^^^^^^^^^^^^^
24+
|
25+
note: the prefix was tested here
26+
--> $DIR/min_rust_version_attr.rs:71:9
27+
|
28+
LL | if s.starts_with("hello, ") {
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
help: try using the `strip_prefix` method
31+
|
32+
LL | if let Some(<stripped>) = s.strip_prefix("hello, ") {
33+
LL | assert_eq!(<stripped>.to_uppercase(), "WORLD!");
34+
|
35+
36+
error: aborting due to 2 previous errors
37+

tests/ui/min_rust_version_no_patch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::redundant_clone)]
22
#![feature(custom_inner_attributes)]
3-
#![clippy::msrv = "^1.0"]
3+
#![clippy::msrv = "1.0"]
44

55
fn manual_strip_msrv() {
66
let s = "hello, world!";

0 commit comments

Comments
 (0)