Skip to content

Commit c176568

Browse files
committed
Ignore associated items in trait *implementations* when considering type complexity
1 parent 40a6c51 commit c176568

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

clippy_lints/src/types/mod.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,24 @@ impl<'tcx> LateLintPass<'tcx> for Types {
350350

351351
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
352352
match item.kind {
353-
ImplItemKind::Const(ty, _) => self.check_ty(
354-
cx,
355-
ty,
356-
CheckTyContext {
357-
is_in_trait_impl: true,
358-
..CheckTyContext::default()
359-
},
360-
),
353+
ImplItemKind::Const(ty, _) => {
354+
let is_in_trait_impl = if let Some(hir::Node::Item(item)) =
355+
cx.tcx.hir().find(cx.tcx.hir().get_parent_item(item.hir_id()))
356+
{
357+
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
358+
} else {
359+
false
360+
};
361+
362+
self.check_ty(
363+
cx,
364+
ty,
365+
CheckTyContext {
366+
is_in_trait_impl,
367+
..CheckTyContext::default()
368+
},
369+
);
370+
},
361371
// Methods are covered by check_fn.
362372
// Type aliases are ignored because oftentimes it's impossible to
363373
// make type alias declaration in trait simpler, see #1013
@@ -419,6 +429,14 @@ impl Types {
419429
}
420430

421431
fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, context: CheckTyContext) {
432+
// Ignore functions in trait implementations as they are usually forced by the trait definition.
433+
//
434+
// FIXME: idially we would like to warn *if the compicated type can be simplified*, but it's hard to
435+
// check.
436+
if context.is_in_trait_impl {
437+
return;
438+
}
439+
422440
for input in decl.inputs {
423441
self.check_ty(cx, input, context);
424442
}
@@ -437,12 +455,12 @@ impl Types {
437455
return;
438456
}
439457

440-
if !context.is_nested_call && type_complexity::check(cx, hir_ty, self.type_complexity_threshold) {
458+
// Skip trait implementations; see issue #605.
459+
if context.is_in_trait_impl {
441460
return;
442461
}
443462

444-
// Skip trait implementations; see issue #605.
445-
if context.is_in_trait_impl {
463+
if !context.is_nested_call && type_complexity::check(cx, hir_ty, self.type_complexity_threshold) {
446464
return;
447465
}
448466

tests/ui/type_complexity.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ trait T {
3030
fn def_method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
3131
}
3232

33+
// Should not warn since there is likely no way to simplify this (#1013)
3334
impl T for () {
3435
const A: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
3536

36-
// Should not warn since there is likely no way to simplify this (#1013)
3737
type B = Vec<Vec<Box<(u32, u32, u32, u32)>>>;
38+
3839
fn method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
3940
}
4041

tests/ui/type_complexity.stderr

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,22 @@ LL | fn def_method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7474

7575
error: very complex type used. Consider factoring parts into `type` definitions
76-
--> $DIR/type_complexity.rs:34:14
77-
|
78-
LL | const A: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
79-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80-
81-
error: very complex type used. Consider factoring parts into `type` definitions
82-
--> $DIR/type_complexity.rs:38:25
83-
|
84-
LL | fn method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
85-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86-
87-
error: very complex type used. Consider factoring parts into `type` definitions
88-
--> $DIR/type_complexity.rs:41:15
76+
--> $DIR/type_complexity.rs:42:15
8977
|
9078
LL | fn test1() -> Vec<Vec<Box<(u32, u32, u32, u32)>>> {
9179
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9280

9381
error: very complex type used. Consider factoring parts into `type` definitions
94-
--> $DIR/type_complexity.rs:45:14
82+
--> $DIR/type_complexity.rs:46:14
9583
|
9684
LL | fn test2(_x: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
9785
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9886

9987
error: very complex type used. Consider factoring parts into `type` definitions
100-
--> $DIR/type_complexity.rs:48:13
88+
--> $DIR/type_complexity.rs:49:13
10189
|
10290
LL | let _y: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
10391
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10492

105-
error: aborting due to 17 previous errors
93+
error: aborting due to 15 previous errors
10694

0 commit comments

Comments
 (0)