diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index a33f9206035..6a433f4e7c4 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -520,7 +520,10 @@ trait UnusedDelimLint { (cond, UnusedDelimsCtx::IfCond, true, Some(left), Some(right)) } - While(ref cond, ref block, ..) => { + // Do not lint `unused_braces` in `while let` expressions. + While(ref cond, ref block, ..) + if !matches!(cond.kind, Let(_, _)) || Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => + { let left = e.span.lo() + rustc_span::BytePos(5); let right = block.span.lo(); (cond, UnusedDelimsCtx::WhileCond, true, Some(left), Some(right)) diff --git a/src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs b/src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs index 02182ec2993..8064c3a88d1 100644 --- a/src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs +++ b/src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs @@ -15,15 +15,8 @@ fn main() { while let Some(_) = ((yield)) {} //~ ERROR: unnecessary parentheses {{yield}}; //~ ERROR: unnecessary braces {( yield )}; //~ ERROR: unnecessary parentheses - - // FIXME: Reduce duplicate warnings. - // Perhaps we should tweak checks in `BlockRetValue`? - while let Some(_) = {(yield)} {} - //~^ ERROR: unnecessary braces - //~| ERROR: unnecessary parentheses - while let Some(_) = {{yield}} {} - //~^ ERROR: unnecessary braces - //~| ERROR: unnecessary braces + while let Some(_) = {(yield)} {} //~ ERROR: unnecessary parentheses + while let Some(_) = {{yield}} {} //~ ERROR: unnecessary braces // FIXME: It'd be great if we could also warn them. ((yield)); diff --git a/src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr b/src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr index 267cc9e031a..3f6260dc6e1 100644 --- a/src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr +++ b/src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr @@ -34,29 +34,17 @@ error: unnecessary parentheses around block return value LL | {( yield )}; | ^^^^^^^^^ help: remove these parentheses -error: unnecessary braces around `let` scrutinee expression - --> $DIR/issue-74883-unused-paren-baren-yield.rs:21:29 - | -LL | while let Some(_) = {(yield)} {} - | ^^^^^^^^^ help: remove these braces - error: unnecessary parentheses around block return value - --> $DIR/issue-74883-unused-paren-baren-yield.rs:21:30 + --> $DIR/issue-74883-unused-paren-baren-yield.rs:18:30 | LL | while let Some(_) = {(yield)} {} | ^^^^^^^ help: remove these parentheses -error: unnecessary braces around `let` scrutinee expression - --> $DIR/issue-74883-unused-paren-baren-yield.rs:24:29 - | -LL | while let Some(_) = {{yield}} {} - | ^^^^^^^^^ help: remove these braces - error: unnecessary braces around block return value - --> $DIR/issue-74883-unused-paren-baren-yield.rs:24:30 + --> $DIR/issue-74883-unused-paren-baren-yield.rs:19:30 | LL | while let Some(_) = {{yield}} {} | ^^^^^^^ help: remove these braces -error: aborting due to 8 previous errors +error: aborting due to 6 previous errors diff --git a/src/test/ui/lint/unused-braces-while-let-with-mutable-value.rs b/src/test/ui/lint/unused-braces-while-let-with-mutable-value.rs new file mode 100644 index 00000000000..ac547293c58 --- /dev/null +++ b/src/test/ui/lint/unused-braces-while-let-with-mutable-value.rs @@ -0,0 +1,12 @@ +// check-pass + +#![deny(unused_braces)] + +fn main() { + let mut a = Some(3); + // Shouldn't warn below `a`. + while let Some(ref mut v) = {a} { + a.as_mut().map(|a| std::mem::swap(a, v)); + break; + } +}