]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #8030 - WaffleLapkin:ignore_trait_assoc_types_type_complexity, r=llogiq
authorbors <bors@rust-lang.org>
Wed, 8 Dec 2021 11:54:03 +0000 (11:54 +0000)
committerbors <bors@rust-lang.org>
Wed, 8 Dec 2021 11:54:03 +0000 (11:54 +0000)
Ignore associated types in traits when considering type complexity

changelog: Ignore associated types in traits when checking ``[`type_complexity`]`` lint.

fixes #1013

clippy_lints/src/types/mod.rs
tests/ui/type_complexity.rs
tests/ui/type_complexity.stderr

index 5a7ef760a3025dcbf890a3175fe1d31200fed05c..481e595743585a8ea3ad5794b0dc53a93b4f459c 100644 (file)
@@ -350,16 +350,28 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
 
     fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
         match item.kind {
-            ImplItemKind::Const(ty, _) | ImplItemKind::TyAlias(ty) => self.check_ty(
-                cx,
-                ty,
-                CheckTyContext {
-                    is_in_trait_impl: true,
-                    ..CheckTyContext::default()
-                },
-            ),
-            // methods are covered by check_fn
-            ImplItemKind::Fn(..) => (),
+            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
+            ImplItemKind::Fn(..) | ImplItemKind::TyAlias(..) => (),
         }
     }
 
@@ -417,6 +429,14 @@ pub fn new(vec_box_size_threshold: u64, type_complexity_threshold: u64, avoid_br
     }
 
     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);
         }
@@ -435,12 +455,12 @@ fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, mut context:
             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;
         }
 
index 383bbb49dbe88567b7b271fe7ce6a749b4a2c937..86a7bd7b62735443e8c305b04a6932bedd213c8d 100644 (file)
@@ -30,6 +30,15 @@ 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![];
+
+    type B = Vec<Vec<Box<(u32, u32, u32, u32)>>>;
+
+    fn method(&self, p: Vec<Vec<Box<(u32, u32, u32, u32)>>>) {}
+}
+
 fn test1() -> Vec<Vec<Box<(u32, u32, u32, u32)>>> {
     vec![]
 }
index 7879233fdf28373f8afb40c6eef9c8548fa94c08..9da7edb1c3b74e27275e6f6f9b1810163040a45b 100644 (file)
@@ -73,19 +73,19 @@ 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:33: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:37: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:40:13
+  --> $DIR/type_complexity.rs:49:13
    |
 LL |     let _y: Vec<Vec<Box<(u32, u32, u32, u32)>>> = vec![];
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^