Skip to content

Emit warnings on unused parens in index expressions #107539

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ enum UnusedDelimsCtx {
ArrayLenExpr,
AnonConst,
MatchArmExpr,
IndexExpr,
}

impl From<UnusedDelimsCtx> for &'static str {
Expand All @@ -514,6 +515,7 @@ impl From<UnusedDelimsCtx> for &'static str {
UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression",
UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
UnusedDelimsCtx::MatchArmExpr => "match arm expression",
UnusedDelimsCtx::IndexExpr => "index expression",
}
}
}
Expand Down Expand Up @@ -733,6 +735,8 @@ trait UnusedDelimLint {
(value, UnusedDelimsCtx::ReturnValue, false, Some(left), None)
}

Index(_, ref value) => (value, UnusedDelimsCtx::IndexExpr, false, None, None),

Assign(_, ref value, _) | AssignOp(.., ref value) => {
(value, UnusedDelimsCtx::AssignedValue, false, None, None)
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/unused/issue-96606.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[deny(unused)]
fn main() {
let arr = [0; 10];
let _ = arr[(0)]; //~ ERROR unnecessary parentheses around index expression
let _ = arr[{0}]; //~ ERROR unnecessary braces around index expression
let _ = arr[1 + (0)];
let _ = arr[{ let x = 0; x }];
}
33 changes: 33 additions & 0 deletions tests/ui/lint/unused/issue-96606.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
error: unnecessary parentheses around index expression
--> $DIR/issue-96606.rs:4:17
|
LL | let _ = arr[(0)];
| ^ ^
|
note: the lint level is defined here
--> $DIR/issue-96606.rs:1:8
|
LL | #[deny(unused)]
| ^^^^^^
= note: `#[deny(unused_parens)]` implied by `#[deny(unused)]`
help: remove these parentheses
|
LL - let _ = arr[(0)];
LL + let _ = arr[0];
|

error: unnecessary braces around index expression
--> $DIR/issue-96606.rs:5:17
|
LL | let _ = arr[{0}];
| ^ ^
|
= note: `#[deny(unused_braces)]` implied by `#[deny(unused)]`
help: remove these braces
|
LL - let _ = arr[{0}];
LL + let _ = arr[0];
|

error: aborting due to 2 previous errors