Skip to content

For expr return (_ = 42); unused_paren lint should not be triggered #132936

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2024
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
9 changes: 9 additions & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ enum UnusedDelimsCtx {
MatchScrutineeExpr,
ReturnValue,
BlockRetValue,
BreakValue,
LetScrutineeExpr,
ArrayLenExpr,
AnonConst,
Expand All @@ -605,6 +606,7 @@ impl From<UnusedDelimsCtx> for &'static str {
UnusedDelimsCtx::MatchScrutineeExpr => "`match` scrutinee expression",
UnusedDelimsCtx::ReturnValue => "`return` value",
UnusedDelimsCtx::BlockRetValue => "block return value",
UnusedDelimsCtx::BreakValue => "`break` value",
UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression",
UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
UnusedDelimsCtx::MatchArmExpr => "match arm expression",
Expand Down Expand Up @@ -913,6 +915,10 @@ trait UnusedDelimLint {
(value, UnusedDelimsCtx::ReturnValue, false, Some(left), None, true)
}

Break(_, Some(ref value)) => {
(value, UnusedDelimsCtx::BreakValue, false, None, None, true)
}

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

Assign(_, ref value, _) | AssignOp(.., ref value) => {
Expand Down Expand Up @@ -1063,6 +1069,9 @@ impl UnusedDelimLint for UnusedParens {
_,
_,
) if node.is_lazy()))
&& !((ctx == UnusedDelimsCtx::ReturnValue
|| ctx == UnusedDelimsCtx::BreakValue)
&& matches!(inner.kind, ast::ExprKind::Assign(_, _, _)))
{
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
}
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/lint/unused/issue-54538-unused-parens-lint.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
unused_mut,
unused_variables
)]
#![deny(unused_parens)]
#![deny(unused_parens, unused_braces)]

fn lint_on_top_level() {
let a = 0; //~ ERROR unnecessary parentheses around pattern
Expand Down Expand Up @@ -43,8 +43,10 @@ fn no_lint_ops() {
fn lint_break_if_not_followed_by_block() {
#![allow(unreachable_code)]
loop { if break {} } //~ ERROR unnecessary parentheses
loop { if break ({ println!("hello") }) {} } //~ ERROR unnecessary parentheses
loop { if (break { println!("hello") }) {} }
loop { if break { println!("hello") } {} }
//~^ ERROR unnecessary parentheses around `if` condition
//~| ERROR unnecessary parentheses around `break` value
loop { if (break println!("hello")) {} } //~ ERROR unnecessary braces around `break` value
}

// Don't lint in these cases (#64106).
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/lint/unused/issue-54538-unused-parens-lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
unused_mut,
unused_variables
)]
#![deny(unused_parens)]
#![deny(unused_parens, unused_braces)]

fn lint_on_top_level() {
let (a) = 0; //~ ERROR unnecessary parentheses around pattern
Expand Down Expand Up @@ -43,8 +43,10 @@ fn no_lint_ops() {
fn lint_break_if_not_followed_by_block() {
#![allow(unreachable_code)]
loop { if (break) {} } //~ ERROR unnecessary parentheses
loop { if (break ({ println!("hello") })) {} } //~ ERROR unnecessary parentheses
loop { if (break { println!("hello") }) {} }
loop { if (break ({ println!("hello") })) {} }
//~^ ERROR unnecessary parentheses around `if` condition
//~| ERROR unnecessary parentheses around `break` value
loop { if (break { println!("hello") }) {} } //~ ERROR unnecessary braces around `break` value
}

// Don't lint in these cases (#64106).
Expand Down
69 changes: 49 additions & 20 deletions tests/ui/lint/unused/issue-54538-unused-parens-lint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | let (a) = 0;
note: the lint level is defined here
--> $DIR/issue-54538-unused-parens-lint.rs:13:9
|
LL | #![deny(unused_parens)]
LL | #![deny(unused_parens, unused_braces)]
| ^^^^^^^^^^^^^
help: remove these parentheses
|
Expand Down Expand Up @@ -99,8 +99,37 @@ LL - loop { if (break ({ println!("hello") })) {} }
LL + loop { if break ({ println!("hello") }) {} }
|

error: unnecessary parentheses around `break` value
--> $DIR/issue-54538-unused-parens-lint.rs:46:22
|
LL | loop { if (break ({ println!("hello") })) {} }
| ^ ^
|
help: remove these parentheses
|
LL - loop { if (break ({ println!("hello") })) {} }
LL + loop { if (break { println!("hello") }) {} }
|

error: unnecessary braces around `break` value
--> $DIR/issue-54538-unused-parens-lint.rs:49:22
|
LL | loop { if (break { println!("hello") }) {} }
| ^^ ^^
|
note: the lint level is defined here
--> $DIR/issue-54538-unused-parens-lint.rs:13:24
|
LL | #![deny(unused_parens, unused_braces)]
| ^^^^^^^^^^^^^
help: remove these braces
|
LL - loop { if (break { println!("hello") }) {} }
LL + loop { if (break println!("hello")) {} }
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:71:12
--> $DIR/issue-54538-unused-parens-lint.rs:73:12
|
LL | if let (0 | 1) = 0 {}
| ^ ^
Expand All @@ -112,7 +141,7 @@ LL + if let 0 | 1 = 0 {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:72:13
--> $DIR/issue-54538-unused-parens-lint.rs:74:13
|
LL | if let ((0 | 1),) = (0,) {}
| ^ ^
Expand All @@ -124,7 +153,7 @@ LL + if let (0 | 1,) = (0,) {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:73:13
--> $DIR/issue-54538-unused-parens-lint.rs:75:13
|
LL | if let [(0 | 1)] = [0] {}
| ^ ^
Expand All @@ -136,7 +165,7 @@ LL + if let [0 | 1] = [0] {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:74:16
--> $DIR/issue-54538-unused-parens-lint.rs:76:16
|
LL | if let 0 | (1 | 2) = 0 {}
| ^ ^
Expand All @@ -148,7 +177,7 @@ LL + if let 0 | 1 | 2 = 0 {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:76:15
--> $DIR/issue-54538-unused-parens-lint.rs:78:15
|
LL | if let TS((0 | 1)) = TS(0) {}
| ^ ^
Expand All @@ -160,7 +189,7 @@ LL + if let TS(0 | 1) = TS(0) {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:78:20
--> $DIR/issue-54538-unused-parens-lint.rs:80:20
|
LL | if let NS { f: (0 | 1) } = (NS { f: 0 }) {}
| ^ ^
Expand All @@ -172,7 +201,7 @@ LL + if let NS { f: 0 | 1 } = (NS { f: 0 }) {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:88:9
--> $DIR/issue-54538-unused-parens-lint.rs:90:9
|
LL | (_) => {}
| ^ ^
Expand All @@ -184,7 +213,7 @@ LL + _ => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:89:9
--> $DIR/issue-54538-unused-parens-lint.rs:91:9
|
LL | (y) => {}
| ^ ^
Expand All @@ -196,7 +225,7 @@ LL + y => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:90:9
--> $DIR/issue-54538-unused-parens-lint.rs:92:9
|
LL | (ref r) => {}
| ^ ^
Expand All @@ -208,7 +237,7 @@ LL + ref r => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:91:9
--> $DIR/issue-54538-unused-parens-lint.rs:93:9
|
LL | (e @ 1...2) => {}
| ^ ^
Expand All @@ -220,7 +249,7 @@ LL + e @ 1...2 => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:97:9
--> $DIR/issue-54538-unused-parens-lint.rs:99:9
|
LL | (e @ &(1...2)) => {}
| ^ ^
Expand All @@ -232,7 +261,7 @@ LL + e @ &(1...2) => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:98:10
--> $DIR/issue-54538-unused-parens-lint.rs:100:10
|
LL | &(_) => {}
| ^ ^
Expand All @@ -244,7 +273,7 @@ LL + &_ => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:109:9
--> $DIR/issue-54538-unused-parens-lint.rs:111:9
|
LL | (_) => {}
| ^ ^
Expand All @@ -256,7 +285,7 @@ LL + _ => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:110:9
--> $DIR/issue-54538-unused-parens-lint.rs:112:9
|
LL | (y) => {}
| ^ ^
Expand All @@ -268,7 +297,7 @@ LL + y => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:111:9
--> $DIR/issue-54538-unused-parens-lint.rs:113:9
|
LL | (ref r) => {}
| ^ ^
Expand All @@ -280,7 +309,7 @@ LL + ref r => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:112:9
--> $DIR/issue-54538-unused-parens-lint.rs:114:9
|
LL | (e @ 1..=2) => {}
| ^ ^
Expand All @@ -292,7 +321,7 @@ LL + e @ 1..=2 => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:118:9
--> $DIR/issue-54538-unused-parens-lint.rs:120:9
|
LL | (e @ &(1..=2)) => {}
| ^ ^
Expand All @@ -304,7 +333,7 @@ LL + e @ &(1..=2) => {}
|

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:119:10
--> $DIR/issue-54538-unused-parens-lint.rs:121:10
|
LL | &(_) => {}
| ^ ^
Expand All @@ -315,5 +344,5 @@ LL - &(_) => {}
LL + &_ => {}
|

error: aborting due to 26 previous errors
error: aborting due to 28 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//@ run-rustfix
#![deny(unused_parens)]
#![allow(unreachable_code)]

fn foo() {
loop {
break (_ = 42);
// lint unused_parens should not be triggered here.
}

let _ = loop {
let a = 1;
let b = 2;
break a + b; //~ERROR unnecessary parentheses
};

loop {
if break return () {
//~^ ERROR unnecessary parentheses
}
if break return () {
//~^ ERROR unnecessary parentheses
}
}

return (_ = 42);
// lint unused_parens should not be triggered here.
}

fn main() {
let _ = foo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//@ run-rustfix
#![deny(unused_parens)]
#![allow(unreachable_code)]

fn foo() {
loop {
break (_ = 42);
// lint unused_parens should not be triggered here.
}

let _ = loop {
let a = 1;
let b = 2;
break (a + b); //~ERROR unnecessary parentheses
};

loop {
if (break return ()) {
//~^ ERROR unnecessary parentheses
}
if break (return ()) {
//~^ ERROR unnecessary parentheses
}
}

return (_ = 42);
// lint unused_parens should not be triggered here.
}

fn main() {
let _ = foo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error: unnecessary parentheses around `break` value
--> $DIR/unused-parens-assign-expr-in-ret-issue-131989.rs:14:15
|
LL | break (a + b);
| ^ ^
|
note: the lint level is defined here
--> $DIR/unused-parens-assign-expr-in-ret-issue-131989.rs:2:9
|
LL | #![deny(unused_parens)]
| ^^^^^^^^^^^^^
help: remove these parentheses
|
LL - break (a + b);
LL + break a + b;
|

error: unnecessary parentheses around `if` condition
--> $DIR/unused-parens-assign-expr-in-ret-issue-131989.rs:18:12
|
LL | if (break return ()) {
| ^ ^
|
help: remove these parentheses
|
LL - if (break return ()) {
LL + if break return () {
|

error: unnecessary parentheses around `break` value
--> $DIR/unused-parens-assign-expr-in-ret-issue-131989.rs:21:18
|
LL | if break (return ()) {
| ^ ^
|
help: remove these parentheses
|
LL - if break (return ()) {
LL + if break return () {
|

error: aborting due to 3 previous errors

Loading
Loading