Skip to content

Commit a40fcb3

Browse files
authored
Rollup merge of rust-lang#107539 - PossiblyAShrub:unused-parens-in-index, r=lcnr
Emit warnings on unused parens in index expressions Fixes: rust-lang#96606. I am not sure what the best term for "index expression" is. Is there a better term we could use?
2 parents bd4e1f4 + c3a71ed commit a40fcb3

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

compiler/rustc_lint/src/unused.rs

+4
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ enum UnusedDelimsCtx {
495495
ArrayLenExpr,
496496
AnonConst,
497497
MatchArmExpr,
498+
IndexExpr,
498499
}
499500

500501
impl From<UnusedDelimsCtx> for &'static str {
@@ -514,6 +515,7 @@ impl From<UnusedDelimsCtx> for &'static str {
514515
UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression",
515516
UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
516517
UnusedDelimsCtx::MatchArmExpr => "match arm expression",
518+
UnusedDelimsCtx::IndexExpr => "index expression",
517519
}
518520
}
519521
}
@@ -733,6 +735,8 @@ trait UnusedDelimLint {
733735
(value, UnusedDelimsCtx::ReturnValue, false, Some(left), None)
734736
}
735737

738+
Index(_, ref value) => (value, UnusedDelimsCtx::IndexExpr, false, None, None),
739+
736740
Assign(_, ref value, _) | AssignOp(.., ref value) => {
737741
(value, UnusedDelimsCtx::AssignedValue, false, None, None)
738742
}

tests/ui/lint/unused/issue-96606.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[deny(unused)]
2+
fn main() {
3+
let arr = [0; 10];
4+
let _ = arr[(0)]; //~ ERROR unnecessary parentheses around index expression
5+
let _ = arr[{0}]; //~ ERROR unnecessary braces around index expression
6+
let _ = arr[1 + (0)];
7+
let _ = arr[{ let x = 0; x }];
8+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error: unnecessary parentheses around index expression
2+
--> $DIR/issue-96606.rs:4:17
3+
|
4+
LL | let _ = arr[(0)];
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-96606.rs:1:8
9+
|
10+
LL | #[deny(unused)]
11+
| ^^^^^^
12+
= note: `#[deny(unused_parens)]` implied by `#[deny(unused)]`
13+
help: remove these parentheses
14+
|
15+
LL - let _ = arr[(0)];
16+
LL + let _ = arr[0];
17+
|
18+
19+
error: unnecessary braces around index expression
20+
--> $DIR/issue-96606.rs:5:17
21+
|
22+
LL | let _ = arr[{0}];
23+
| ^ ^
24+
|
25+
= note: `#[deny(unused_braces)]` implied by `#[deny(unused)]`
26+
help: remove these braces
27+
|
28+
LL - let _ = arr[{0}];
29+
LL + let _ = arr[0];
30+
|
31+
32+
error: aborting due to 2 previous errors
33+

0 commit comments

Comments
 (0)