Skip to content

Commit

Permalink
Rollup merge of rust-lang#75083 - JohnTitor:follow-up-unused-braces, …
Browse files Browse the repository at this point in the history
…r=lcnr

Do not trigger `unused_braces` for `while let`

Follow-up for rust-lang#75031
r? @lcnr
  • Loading branch information
JohnTitor authored Aug 4, 2020
2 parents c2bc733 + 2e5c501 commit 4eb9253
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,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))
Expand Down
11 changes: 2 additions & 9 deletions src/test/ui/lint/issue-74883-unused-paren-baren-yield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
18 changes: 3 additions & 15 deletions src/test/ui/lint/issue-74883-unused-paren-baren-yield.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

12 changes: 12 additions & 0 deletions src/test/ui/lint/unused-braces-while-let-with-mutable-value.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 4eb9253

Please sign in to comment.