Skip to content

Commit 998635d

Browse files
committed
Avoid type_complexity from impl Trait bounds
1 parent a735ace commit 998635d

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

clippy_lints/src/types/type_complexity.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ impl<'tcx> Visitor<'tcx> for TypeComplexityVisitor {
4242
}
4343

4444
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'_, AmbigArg>) {
45+
// Opaque types cannot be extracted into type aliases on stable. Ignore their
46+
// bounds, but keep scoring surrounding type components that can be extracted.
47+
if matches!(ty.kind, TyKind::OpaqueDef(..)) {
48+
return;
49+
}
4550
let (add_score, sub_nest) = match ty.kind {
4651
// &x and *x have only small overhead; don't mess with nesting level
4752
TyKind::Ptr(..) | TyKind::Ref(..) => (1, 0),

tests/ui/type_complexity.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,26 @@ struct D {
8787
}
8888

8989
fn main() {}
90+
91+
// Should not warn, because factoring `impl Trait` into a type alias is not stable (#17195).
92+
fn issue_17195<I, J>(
93+
left: I,
94+
right: J,
95+
) -> std::iter::Map<std::iter::Zip<I::IntoIter, J::IntoIter>, impl FnMut((I::Item, I::Item)) -> [I::Item; 2]>
96+
where
97+
I: IntoIterator,
98+
J: IntoIterator<Item = I::Item>,
99+
{
100+
left.into_iter().zip(right).map(<[I::Item; 2]>::from)
101+
}
102+
103+
// Complexity inside an opaque type cannot be factored into a type alias either.
104+
fn complex_opaque_bound() -> impl Fn(Vec<Vec<Box<(u32, u32, u32, u32)>>>) {
105+
|_| {}
106+
}
107+
108+
// The presence of an opaque type must not hide complexity in a sibling type that can be factored out.
109+
#[expect(clippy::type_complexity)]
110+
fn complex_after_opaque() -> (impl Iterator<Item = u32>, Vec<Vec<Box<(u32, u32, u32, u32)>>>) {
111+
(std::iter::empty(), vec![])
112+
}

0 commit comments

Comments
 (0)