Skip to content

Commit

Permalink
Arrays of sizes from 0 to 32 (inclusive) implement [Default] trait, e…
Browse files Browse the repository at this point in the history
…dit method is_default_equivalent() to satisfy with this.
  • Loading branch information
surechen committed Sep 30, 2021
1 parent 2316f4d commit a3d3735
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
12 changes: 11 additions & 1 deletion clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/derivable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
14 changes: 13 additions & 1 deletion tests/ui/derivable_impls.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a3d3735

Please sign in to comment.