about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-09-30 08:19:58 +0000
committerbors <bors@rust-lang.org>2021-09-30 08:19:58 +0000
commitab99eec15f322eb06ddc0ff235e3c7fbac96e541 (patch)
tree490006362922b301c4f4a0a7d0713cfec721c64d
parent984d4661c7ea776a6ef7044e69666ef056bd5000 (diff)
parenta3d373551551bc1bd5a993d586f42144f0f4542a (diff)
downloadrust-ab99eec15f322eb06ddc0ff235e3c7fbac96e541.tar.gz
rust-ab99eec15f322eb06ddc0ff235e3c7fbac96e541.zip
Auto merge of #7684 - surechen:solve_derivable_impls, r=flip1995
fix for issue #7683

Fixes #7683.

For Repeat  [x; y] (x is the type and y is the times to repeat) . When y > 32, the compiler will report an error:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7148558162685e91056e0550797ea74c

Because https://github.com/rust-lang/rust/blob/6cdd42f9f8dd4e5e5ba0aa816bc4c99ab8b102f9/library/std/src/primitive_docs.rs#L538
/// Arrays of sizes from 0 to 32 (inclusive) implement [`Default`] trait
/// if the element type allows it. As a stopgap, trait implementations are
/// statically generated up to size 32.

So here to detect this situation.

changelog: [`derivable_impls`]: No longer lints when arrays bigger than 32 elements are involved
-rw-r--r--clippy_utils/src/lib.rs12
-rw-r--r--tests/ui/derivable_impls.rs20
-rw-r--r--tests/ui/derivable_impls.stderr14
3 files changed, 44 insertions, 2 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs
index 7f5a1bf9c07..79fbd255024 100644
--- a/clippy_utils/src/lib.rs
+++ b/clippy_utils/src/lib.rs
@@ -684,7 +684,17 @@ pub fn is_default_equivalent(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
             _ => false,
         },
         ExprKind::Tup(items) | ExprKind::Array(items) => items.iter().all(|x| is_default_equivalent(cx, x)),
-        ExprKind::Repeat(x, _) => is_default_equivalent(cx, x),
+        ExprKind::Repeat(x, y) => if_chain! {
+            if let ExprKind::Lit(ref const_lit) = cx.tcx.hir().body(y.body).value.kind;
+            if let LitKind::Int(v, _) = const_lit.node;
+            if v <= 32 && is_default_equivalent(cx, x);
+            then {
+                true
+            }
+            else {
+                false
+            }
+        },
         ExprKind::Call(repl_func, _) => if_chain! {
             if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
             if let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
diff --git a/tests/ui/derivable_impls.rs b/tests/ui/derivable_impls.rs
index ebbc0c77e32..3eeebc117f3 100644
--- a/tests/ui/derivable_impls.rs
+++ b/tests/ui/derivable_impls.rs
@@ -207,4 +207,24 @@ impl Default for Color2 {
     }
 }
 
+pub struct RepeatDefault1 {
+    a: [i8; 32],
+}
+
+impl Default for RepeatDefault1 {
+    fn default() -> Self {
+        RepeatDefault1 { a: [0; 32] }
+    }
+}
+
+pub struct RepeatDefault2 {
+    a: [i8; 33],
+}
+
+impl Default for RepeatDefault2 {
+    fn default() -> Self {
+        RepeatDefault2 { a: [0; 33] }
+    }
+}
+
 fn main() {}
diff --git a/tests/ui/derivable_impls.stderr b/tests/ui/derivable_impls.stderr
index 4ed64fade02..49fb471a219 100644
--- a/tests/ui/derivable_impls.stderr
+++ b/tests/ui/derivable_impls.stderr
@@ -73,5 +73,17 @@ LL | | }
    |
    = help: try annotating `WithoutSelfParan` with `#[derive(Default)]`
 
-error: aborting due to 6 previous errors
+error: this `impl` can be derived
+  --> $DIR/derivable_impls.rs:214:1
+   |
+LL | / impl Default for RepeatDefault1 {
+LL | |     fn default() -> Self {
+LL | |         RepeatDefault1 { a: [0; 32] }
+LL | |     }
+LL | | }
+   | |_^
+   |
+   = help: try annotating `RepeatDefault1` with `#[derive(Default)]`
+
+error: aborting due to 7 previous errors