Skip to content

Commit e609279

Browse files
committed
Auto merge of rust-lang#11484 - mkrasnitski:fix-11302, r=Jarcho
[`extra_unused_type_parameters`]: Fix edge case FP for parameters in where bounds Generic parameters can end up being used on the left side of where-bounds if they are not directly bound but instead appear nested in some concrete generic type. Therefore, we should walk the left side of where bounds, but only if the bounded type is *not* a generic param, in which case we still need to ignore the bound. Fixes rust-lang#11302 changelog: [`extra_unused_type_parameters`]: Fix edge case false positive for parameters in where bounds
2 parents 2c629cc + f598bb7 commit e609279

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

clippy_lints/src/extra_unused_type_parameters.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,13 @@ impl<'cx, 'tcx> Visitor<'tcx> for TypeWalker<'cx, 'tcx> {
246246
{
247247
self.ty_params.remove(&def_id);
248248
}
249+
} else {
250+
// If the bounded type isn't a generic param, but is instead a concrete generic
251+
// type, any params we find nested inside of it are being used as concrete types,
252+
// and can therefore can be considered used. So, we're fine to walk the left-hand
253+
// side of the where bound.
254+
walk_ty(self, predicate.bounded_ty);
249255
}
250-
// Only walk the right-hand side of where bounds
251256
for bound in predicate.bounds {
252257
walk_param_bound(self, bound);
253258
}

tests/ui/extra_unused_type_parameters.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,19 @@ with_span!(
113113
}
114114
);
115115

116+
mod issue11302 {
117+
use std::fmt::Debug;
118+
use std::marker::PhantomData;
119+
120+
#[derive(Debug)]
121+
struct Wrapper<T>(PhantomData<T>);
122+
123+
fn store<T: 'static>(v: &mut Vec<Box<dyn Debug>>)
124+
where
125+
Wrapper<T>: Debug,
126+
{
127+
v.push(Box::new(Wrapper(PhantomData)));
128+
}
129+
}
130+
116131
fn main() {}

tests/ui/extra_unused_type_parameters.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,19 @@ with_span!(
113113
}
114114
);
115115

116+
mod issue11302 {
117+
use std::fmt::Debug;
118+
use std::marker::PhantomData;
119+
120+
#[derive(Debug)]
121+
struct Wrapper<T>(PhantomData<T>);
122+
123+
fn store<T: 'static>(v: &mut Vec<Box<dyn Debug>>)
124+
where
125+
Wrapper<T>: Debug,
126+
{
127+
v.push(Box::new(Wrapper(PhantomData)));
128+
}
129+
}
130+
116131
fn main() {}

0 commit comments

Comments
 (0)