Skip to content

Commit 2e082ba

Browse files
committed
fix: check if the used path is a trait
1 parent fb4bacb commit 2e082ba

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

clippy_lints/src/needless_traits_in_scope.rs

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessTraitsInScope {
4747
let ItemKind::Use(use_path, UseKind::Single) = item.kind else {
4848
return
4949
};
50+
// Check if it's a trait
51+
if !use_path
52+
.res
53+
.iter()
54+
.any(|res| matches!(res, def::Res::Def(def::DefKind::Trait, _)))
55+
{
56+
return;
57+
}
5058
// Check if the `use` is aliased with ` as `.
5159
// If aliased, then do not process, it's probably for a good reason
5260
if item.ident != use_path.segments.last().unwrap().ident {

tests/ui/needless_traits_in_scope.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ pub mod trait_not_in_scope {
2222
}
2323
}
2424

25+
pub mod is_not_a_trait {
26+
mod inner {
27+
pub struct Read;
28+
}
29+
use inner::Read;
30+
31+
pub fn ok() {
32+
let _ = Read;
33+
}
34+
}
35+
2536
// FIXME: when the trait is explicitely used, the lint should not trigger
2637
// pub mod useful_trait_in_scope {
2738
// use std::io::Read;
@@ -36,5 +47,6 @@ pub mod trait_not_in_scope {
3647
fn main() {
3748
useless_trait_in_scope::warn();
3849
trait_not_in_scope::ok();
50+
is_not_a_trait::ok();
3951
// useful_trait_in_scope::ok();
4052
}

tests/ui/needless_traits_in_scope.rs

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ pub mod trait_not_in_scope {
2222
}
2323
}
2424

25+
pub mod is_not_a_trait {
26+
mod inner {
27+
pub struct Read;
28+
}
29+
use inner::Read;
30+
31+
pub fn ok() {
32+
let _ = Read;
33+
}
34+
}
35+
2536
// FIXME: when the trait is explicitely used, the lint should not trigger
2637
// pub mod useful_trait_in_scope {
2738
// use std::io::Read;
@@ -36,5 +47,6 @@ pub mod trait_not_in_scope {
3647
fn main() {
3748
useless_trait_in_scope::warn();
3849
trait_not_in_scope::ok();
50+
is_not_a_trait::ok();
3951
// useful_trait_in_scope::ok();
4052
}

0 commit comments

Comments
 (0)