Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d0468a2
rm var_span_label to var_subdiag & eager subdiag
AndyJado Feb 10, 2023
bdf0e74
migrate ftl msg accroding to #103042
AndyJado Feb 24, 2023
8df1f41
fix false positives for `unused_parens` around unary and binary opera…
Apr 12, 2023
0d0949d
emit `unused_parens` for `break` if it is not immediately followed by…
Apr 12, 2023
b59ec16
allow `repr(align = x)` on inherent methods
fee1-dead Apr 14, 2023
7a07c74
Correct default value for default-linker-libraries
iterion Apr 14, 2023
32f6e7a
Remove `EnumTypeTraversalImpl`.
nnethercote Apr 16, 2023
d2b5a64
Simplify `CloneLiftImpls` and `TrivialTypeTraversalImpls`.
nnethercote Apr 16, 2023
dda8994
Allow all associated functions and add test
fee1-dead Apr 16, 2023
c98895d
Various minor Idx-related tweaks
scottmcm Apr 16, 2023
e28e190
Check freeze with right param-env
compiler-errors Apr 16, 2023
1ee189c
Encode def span for ConstParam
compiler-errors Apr 16, 2023
84de041
add test for invalid places of repr align
fee1-dead Apr 17, 2023
bef3502
tests: adapt for LLVM change 5b386b864c7619897c51a1da97d78f1cf6f3eff6
durin42 Apr 17, 2023
91fe117
Rollup merge of #104055 - AndyJado:bck_errors, r=davidtwco
matthiaskrgr Apr 17, 2023
06d12f6
Rollup merge of #110257 - lukas-code:why-would-anyone-write-code-like…
matthiaskrgr Apr 17, 2023
eb05246
Rollup merge of #110313 - fee1-dead-contrib:repr_align_method, r=Waff…
matthiaskrgr Apr 17, 2023
a785328
Rollup merge of #110337 - iterion:patch-1, r=jyn514
matthiaskrgr Apr 17, 2023
a6c1fa5
Rollup merge of #110386 - nnethercote:clean-up-traversal-macros, r=lcnr
matthiaskrgr Apr 17, 2023
0790996
Rollup merge of #110394 - scottmcm:less-idx-new, r=WaffleLapkin
matthiaskrgr Apr 17, 2023
d01c62b
Rollup merge of #110425 - compiler-errors:def-span-for-ct-param, r=pe…
matthiaskrgr Apr 17, 2023
a76b157
Rollup merge of #110434 - compiler-errors:issue-110171, r=oli-obk
matthiaskrgr Apr 17, 2023
c81e8b8
Rollup merge of #110455 - durin42:tls-D148269-fix, r=nikic
matthiaskrgr Apr 17, 2023
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
Prev Previous commit
Next Next commit
fix false positives for unused_parens around unary and binary opera…
…tions
  • Loading branch information
Lukas Markeffsky committed Apr 13, 2023
commit 8df1f41b9c11443a52050c653ac9d89ceea1cae3
48 changes: 30 additions & 18 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,36 +569,48 @@ trait UnusedDelimLint {
}
}

// Prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`
let lhs_needs_parens = {
// Check if LHS needs parens to prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`.
{
let mut innermost = inner;
loop {
innermost = match &innermost.kind {
ExprKind::Binary(_, lhs, _rhs) => lhs,
ExprKind::Binary(_op, lhs, _rhs) => lhs,
ExprKind::Call(fn_, _params) => fn_,
ExprKind::Cast(expr, _ty) => expr,
ExprKind::Type(expr, _ty) => expr,
ExprKind::Index(base, _subscript) => base,
_ => break false,
_ => break,
};
if !classify::expr_requires_semi_to_be_stmt(innermost) {
break true;
return true;
}
}
};
}

lhs_needs_parens
|| (followed_by_block
&& match &inner.kind {
ExprKind::Ret(_)
| ExprKind::Break(..)
| ExprKind::Yield(..)
| ExprKind::Yeet(..) => true,
ExprKind::Range(_lhs, Some(rhs), _limits) => {
matches!(rhs.kind, ExprKind::Block(..))
}
_ => parser::contains_exterior_struct_lit(&inner),
})
// Check if RHS needs parens to prevent false-positives in cases like `if (() == return) {}`.
if !followed_by_block {
return false;
}
let mut innermost = inner;
loop {
innermost = match &innermost.kind {
ExprKind::Unary(_op, expr) => expr,
ExprKind::Binary(_op, _lhs, rhs) => rhs,
ExprKind::AssignOp(_op, _lhs, rhs) => rhs,
ExprKind::Assign(_lhs, rhs, _span) => rhs,

ExprKind::Ret(_)
| ExprKind::Break(..)
| ExprKind::Yield(..)
| ExprKind::Yeet(..) => return true,

ExprKind::Range(_lhs, Some(rhs), _limits) => {
return matches!(rhs.kind, ExprKind::Block(..));
}

_ => return parser::contains_exterior_struct_lit(&inner),
}
}
}

fn emit_unused_delims_expr(
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/unused/issue-54538-unused-parens-lint.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ fn _no_lint_yeet() -> Result<(), ()> {
Ok(())
}

fn _no_lint_ops() {
#![allow(unreachable_code, irrefutable_let_patterns)]
if ((..{}) == ..{}) {}
if (!return) {}
loop { match (() = () = () = break {}) {} }
while let () = (*&mut false |= true && return) {}
}

// Don't lint in these cases (#64106).
fn or_patterns_no_lint() {
match Box::new(0) {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/unused/issue-54538-unused-parens-lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ fn _no_lint_yeet() -> Result<(), ()> {
Ok(())
}

fn _no_lint_ops() {
#![allow(unreachable_code, irrefutable_let_patterns)]
if ((..{}) == ..{}) {}
if (!return) {}
loop { match (() = () = () = break {}) {} }
while let () = (*&mut false |= true && return) {}
}

// Don't lint in these cases (#64106).
fn or_patterns_no_lint() {
match Box::new(0) {
Expand Down
36 changes: 18 additions & 18 deletions tests/ui/lint/unused/issue-54538-unused-parens-lint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ LL + let _ = |a: u8| 0;
|

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:104:10
--> $DIR/issue-54538-unused-parens-lint.rs:112:10
|
LL | &(_) => {}
| ^ ^
Expand Down