Skip to content

Commit 3c8f90b

Browse files
committed
Auto merge of #8030 - WaffleLapkin:ignore_trait_assoc_types_type_complexity, r=llogiq
Ignore associated types in traits when considering type complexity changelog: Ignore associated types in traits when checking ``[`type_complexity`]`` lint. fixes #1013
2 parents 86cea73 + c176568 commit 3c8f90b

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

clippy_lints/src/types/mod.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,28 @@ 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, _) | ImplItemKind::TyAlias(ty) => self.check_ty(
354-
cx,
355-
ty,
356-
CheckTyContext {
357-
is_in_trait_impl: true,
358-
..CheckTyContext::default()
359-
},
360-
),
361-
// methods are covered by check_fn
362-
ImplItemKind::Fn(..) => (),
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+
},
371+
// Methods are covered by check_fn.
372+
// Type aliases are ignored because oftentimes it's impossible to
373+
// make type alias declaration in trait simpler, see #1013
374+
ImplItemKind::Fn(..) | ImplItemKind::TyAlias(..) => (),
363375
}
364376
}
365377

@@ -417,6 +429,14 @@ impl Types {
417429
}
418430

419431
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+
420440
for input in decl.inputs {
421441
self.check_ty(cx, input, context);
422442
}
@@ -435,12 +455,12 @@ impl Types {
435455
return;
436456
}
437457

438-
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 {
439460
return;
440461
}
441462

442-
// Skip trait implementations; see issue #605.
443-
if context.is_in_trait_impl {
463+
if !context.is_nested_call && type_complexity::check(cx, hir_ty, self.type_complexity_threshold) {
444464
return;
445465
}
446466

tests/ui/type_complexity.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ 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)
34+
impl T for () {
35+
const A: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
36+
37+
type B = Vec<Vec<Box<(u32, u32, u32, u32)>>>;
38+
39+
fn method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
40+
}
41+
3342
fn test1() -> Vec<Vec<Box<(u32, u32, u32, u32)>>> {
3443
vec![]
3544
}

tests/ui/type_complexity.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ 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:33:15
76+
--> $DIR/type_complexity.rs:42:15
7777
|
7878
LL | fn test1() -> Vec<Vec<Box<(u32, u32, u32, u32)>>> {
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8080

8181
error: very complex type used. Consider factoring parts into `type` definitions
82-
--> $DIR/type_complexity.rs:37:14
82+
--> $DIR/type_complexity.rs:46:14
8383
|
8484
LL | fn test2(_x: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8686

8787
error: very complex type used. Consider factoring parts into `type` definitions
88-
--> $DIR/type_complexity.rs:40:13
88+
--> $DIR/type_complexity.rs:49:13
8989
|
9090
LL | let _y: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)