From c3a71ede7cc459b84ddac53f3435391712fa1f12 Mon Sep 17 00:00:00 2001 From: Aidan Olsen Date: Tue, 31 Jan 2023 16:48:08 -0700 Subject: [PATCH] Emit warnings on unused parens/braces in index expressions --- compiler/rustc_lint/src/unused.rs | 4 +++ tests/ui/lint/unused/issue-96606.rs | 8 ++++++ tests/ui/lint/unused/issue-96606.stderr | 33 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/ui/lint/unused/issue-96606.rs create mode 100644 tests/ui/lint/unused/issue-96606.stderr diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 4c9b3df2dbd33..d829ca43328fe 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -495,6 +495,7 @@ enum UnusedDelimsCtx { ArrayLenExpr, AnonConst, MatchArmExpr, + IndexExpr, } impl From for &'static str { @@ -514,6 +515,7 @@ impl From for &'static str { UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression", UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression", UnusedDelimsCtx::MatchArmExpr => "match arm expression", + UnusedDelimsCtx::IndexExpr => "index expression", } } } @@ -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) } diff --git a/tests/ui/lint/unused/issue-96606.rs b/tests/ui/lint/unused/issue-96606.rs new file mode 100644 index 0000000000000..4e7c290fa2a02 --- /dev/null +++ b/tests/ui/lint/unused/issue-96606.rs @@ -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 }]; +} diff --git a/tests/ui/lint/unused/issue-96606.stderr b/tests/ui/lint/unused/issue-96606.stderr new file mode 100644 index 0000000000000..e3627718116be --- /dev/null +++ b/tests/ui/lint/unused/issue-96606.stderr @@ -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 +