Skip to content

Commit 3762844

Browse files
Review add msrv check
1 parent 0650dee commit 3762844

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

clippy_config/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ msrv_aliases! {
4848
1,36,0 { ITERATOR_COPIED }
4949
1,35,0 { OPTION_COPIED, RANGE_CONTAINS }
5050
1,34,0 { TRY_FROM }
51+
1,33,0 { UNDERSCORE_IMPORTS }
5152
1,30,0 { ITERATOR_FIND_MAP, TOOL_ATTRIBUTES }
5253
1,29,0 { ITER_FLATTEN }
5354
1,28,0 { FROM_BOOL }

clippy_lints/src/anon_trait_import.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use clippy_config::msrvs::{self, Msrv};
2+
use clippy_config::Conf;
13
use clippy_utils::diagnostics::span_lint_and_sugg;
24
use clippy_utils::source::snippet_opt;
35
use rustc_errors::Applicability;
46
use rustc_hir::def::{DefKind, Res};
57
use rustc_hir::{Item, ItemKind, UseKind};
68
use rustc_lint::{LateContext, LateLintPass};
79
use rustc_middle::ty::Visibility;
8-
use rustc_session::declare_lint_pass;
10+
use rustc_session::impl_lint_pass;
911
use rustc_span::symbol::kw;
1012

1113
declare_clippy_lint! {
@@ -42,11 +44,24 @@ declare_clippy_lint! {
4244
"use items that import a trait but only use it anonymously"
4345
}
4446

45-
declare_lint_pass!(AnonTraitImport => [ANON_TRAIT_IMPORT]);
47+
pub struct AnonTraitImport {
48+
msrv: Msrv,
49+
}
50+
51+
impl AnonTraitImport {
52+
pub fn new(conf: &'static Conf) -> Self {
53+
Self {
54+
msrv: conf.msrv.clone(),
55+
}
56+
}
57+
}
58+
59+
impl_lint_pass!(AnonTraitImport => [ANON_TRAIT_IMPORT]);
4660

4761
impl<'tcx> LateLintPass<'tcx> for AnonTraitImport {
4862
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
49-
if let ItemKind::Use(path, UseKind::Single) = item.kind
63+
if self.msrv.meets(msrvs::UNDERSCORE_IMPORTS)
64+
&& let ItemKind::Use(path, UseKind::Single) = item.kind
5065
// Ignore imports that already use Underscore
5166
&& item.ident.name != kw::Underscore
5267
// Only check traits
@@ -70,4 +85,6 @@ impl<'tcx> LateLintPass<'tcx> for AnonTraitImport {
7085
);
7186
}
7287
}
88+
89+
extract_msrv_attr!(LateContext);
7390
}

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
936936
store.register_early_pass(|| Box::new(byte_char_slices::ByteCharSlice));
937937
store.register_early_pass(|| Box::new(cfg_not_test::CfgNotTest));
938938
store.register_late_pass(|_| Box::new(zombie_processes::ZombieProcesses));
939-
store.register_late_pass(|_| Box::new(anon_trait_import::AnonTraitImport));
939+
store.register_late_pass(move |_| Box::new(anon_trait_import::AnonTraitImport::new(conf)));
940940
// add lints here, do not remove this comment, it's used in `new_lint`
941941
}

tests/ui/anon_trait_import.fixed

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,28 @@ mod nested_mod_used_good5 {
161161
}
162162
}
163163
}
164+
165+
mod simple_trait {
166+
pub trait MyTrait {
167+
fn do_things(&self);
168+
}
169+
170+
pub struct MyStruct;
171+
172+
impl MyTrait for MyStruct {
173+
fn do_things(&self) {}
174+
}
175+
}
176+
177+
// Underscore imports were stabilized in 1.33
178+
#[clippy::msrv = "1.32"]
179+
fn msrv_1_32() {
180+
use simple_trait::{MyStruct, MyTrait};
181+
MyStruct.do_things();
182+
}
183+
184+
#[clippy::msrv = "1.33"]
185+
fn msrv_1_33() {
186+
use simple_trait::{MyStruct, MyTrait as _};
187+
MyStruct.do_things();
188+
}

tests/ui/anon_trait_import.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,28 @@ mod nested_mod_used_good5 {
161161
}
162162
}
163163
}
164+
165+
mod simple_trait {
166+
pub trait MyTrait {
167+
fn do_things(&self);
168+
}
169+
170+
pub struct MyStruct;
171+
172+
impl MyTrait for MyStruct {
173+
fn do_things(&self) {}
174+
}
175+
}
176+
177+
// Underscore imports were stabilized in 1.33
178+
#[clippy::msrv = "1.32"]
179+
fn msrv_1_32() {
180+
use simple_trait::{MyStruct, MyTrait};
181+
MyStruct.do_things();
182+
}
183+
184+
#[clippy::msrv = "1.33"]
185+
fn msrv_1_33() {
186+
use simple_trait::{MyStruct, MyTrait};
187+
MyStruct.do_things();
188+
}

tests/ui/anon_trait_import.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ error: importing trait that is only used anonymously
4343
LL | use std::any::Any;
4444
| ^^^ help: use: `Any as _`
4545

46-
error: aborting due to 7 previous errors
46+
error: importing trait that is only used anonymously
47+
--> tests/ui/anon_trait_import.rs:186:34
48+
|
49+
LL | use simple_trait::{MyStruct, MyTrait};
50+
| ^^^^^^^ help: use: `MyTrait as _`
51+
52+
error: aborting due to 8 previous errors
4753

0 commit comments

Comments
 (0)