Skip to content

Commit 5477205

Browse files
committed
do the same for Arrays
1 parent f37b200 commit 5477205

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,15 @@ fn reduce_expression<'a>(
339339
ExprKind::Binary(ref binop, a, b) if binop.node != BinOpKind::And && binop.node != BinOpKind::Or => {
340340
Some(vec![a, b])
341341
},
342-
ExprKind::Array(v) | ExprKind::Tup(v) => Some(v.iter().collect()),
342+
ExprKind::Array(elems) => {
343+
if elems.iter().any(|elem| !expr_type_is_certain(cx, elem)) {
344+
// there's a risk that if we take the elem exprs out of the context of the array,
345+
// their types might become ambiguous
346+
*applicability = Applicability::MaybeIncorrect;
347+
}
348+
Some(elems.iter().collect())
349+
},
350+
ExprKind::Tup(v) => Some(v.iter().collect()),
343351
ExprKind::Repeat(inner, _)
344352
| ExprKind::Type(inner, _)
345353
| ExprKind::Unary(_, inner)

tests/ui/unnecessary_operation_unfixable.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ fn issue15381() {
2525
struct Tuple(Vec<u32>);
2626
Tuple(Vec::new());
2727
//~^ unnecessary_operation
28+
29+
// the type of the second slice gets inferred based on it needing to be the same to that of the
30+
// first one, but that doesn't happen when they're outside the array
31+
[[1, 2, 3].as_slice(), [].as_slice()];
32+
//~^ unnecessary_operation
2833
}

tests/ui/unnecessary_operation_unfixable.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@ error: unnecessary operation
2525
LL | Tuple(Vec::new());
2626
| ^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `Vec::new();`
2727

28-
error: aborting due to 4 previous errors
28+
error: unnecessary operation
29+
--> tests/ui/unnecessary_operation_unfixable.rs:31:5
30+
|
31+
LL | [[1, 2, 3].as_slice(), [].as_slice()];
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: statement can be reduced to: `[1, 2, 3].as_slice();[].as_slice();`
33+
34+
error: aborting due to 5 previous errors
2935

0 commit comments

Comments
 (0)