Ignore associated items in trait *implementations* when considering type complexity

This commit is contained in:
Maybe Waffle 2021-12-08 14:27:49 +03:00
parent 40a6c519b4
commit c176568abd
3 changed files with 35 additions and 28 deletions

View file

@ -350,14 +350,24 @@ impl<'tcx> LateLintPass<'tcx> for Types {
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
match item.kind {
ImplItemKind::Const(ty, _) => self.check_ty(
cx,
ty,
CheckTyContext {
is_in_trait_impl: true,
..CheckTyContext::default()
},
),
ImplItemKind::Const(ty, _) => {
let is_in_trait_impl = if let Some(hir::Node::Item(item)) =
cx.tcx.hir().find(cx.tcx.hir().get_parent_item(item.hir_id()))
{
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
} else {
false
};
self.check_ty(
cx,
ty,
CheckTyContext {
is_in_trait_impl,
..CheckTyContext::default()
},
);
},
// Methods are covered by check_fn.
// Type aliases are ignored because oftentimes it's impossible to
// make type alias declaration in trait simpler, see #1013
@ -419,6 +429,14 @@ impl Types {
}
fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, context: CheckTyContext) {
// Ignore functions in trait implementations as they are usually forced by the trait definition.
//
// FIXME: idially we would like to warn *if the compicated type can be simplified*, but it's hard to
// check.
if context.is_in_trait_impl {
return;
}
for input in decl.inputs {
self.check_ty(cx, input, context);
}
@ -437,12 +455,12 @@ impl Types {
return;
}
if !context.is_nested_call && type_complexity::check(cx, hir_ty, self.type_complexity_threshold) {
// Skip trait implementations; see issue #605.
if context.is_in_trait_impl {
return;
}
// Skip trait implementations; see issue #605.
if context.is_in_trait_impl {
if !context.is_nested_call && type_complexity::check(cx, hir_ty, self.type_complexity_threshold) {
return;
}

View file

@ -30,11 +30,12 @@ trait T {
fn def_method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
}
// Should not warn since there is likely no way to simplify this (#1013)
impl T for () {
const A: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
// Should not warn since there is likely no way to simplify this (#1013)
type B = Vec<Vec<Box<(u32, u32, u32, u32)>>>;
fn method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
}

View file

@ -73,34 +73,22 @@ LL | fn def_method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: very complex type used. Consider factoring parts into `type` definitions
--> $DIR/type_complexity.rs:34:14
|
LL | const A: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: very complex type used. Consider factoring parts into `type` definitions
--> $DIR/type_complexity.rs:38:25
|
LL | fn method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: very complex type used. Consider factoring parts into `type` definitions
--> $DIR/type_complexity.rs:41:15
--> $DIR/type_complexity.rs:42:15
|
LL | fn test1() -> Vec<Vec<Box<(u32, u32, u32, u32)>>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: very complex type used. Consider factoring parts into `type` definitions
--> $DIR/type_complexity.rs:45:14
--> $DIR/type_complexity.rs:46:14
|
LL | fn test2(_x: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: very complex type used. Consider factoring parts into `type` definitions
--> $DIR/type_complexity.rs:48:13
--> $DIR/type_complexity.rs:49:13
|
LL | let _y: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 17 previous errors
error: aborting due to 15 previous errors