diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2023-02-03 23:04:51 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-03 23:04:51 +0530 |
| commit | d9db35785d33e2b6c6e9b4971dfdbe0984a69b9e (patch) | |
| tree | dd5a43214576713eb4d160c8b57367a987456b00 | |
| parent | e1bf3a13869ceef2bd9a62f3153ba2241b0047d7 (diff) | |
| parent | c3a71ede7cc459b84ddac53f3435391712fa1f12 (diff) | |
| download | rust-d9db35785d33e2b6c6e9b4971dfdbe0984a69b9e.tar.gz rust-d9db35785d33e2b6c6e9b4971dfdbe0984a69b9e.zip | |
Rollup merge of #107539 - PossiblyAShrub:unused-parens-in-index, r=lcnr
Emit warnings on unused parens in index expressions Fixes: #96606. I am not sure what the best term for "index expression" is. Is there a better term we could use?
| -rw-r--r-- | compiler/rustc_lint/src/unused.rs | 4 | ||||
| -rw-r--r-- | tests/ui/lint/unused/issue-96606.rs | 8 | ||||
| -rw-r--r-- | tests/ui/lint/unused/issue-96606.stderr | 33 |
3 files changed, 45 insertions, 0 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 4c9b3df2dbd..d829ca43328 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<UnusedDelimsCtx> for &'static str { @@ -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", } } } @@ -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 00000000000..4e7c290fa2a --- /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 00000000000..e3627718116 --- /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 + |
