1
Fork 0

Fixed false positive for unused_parens lint

This commit is contained in:
aticu 2020-10-14 01:13:48 +02:00
parent adef9da30f
commit 39867f3c9f
4 changed files with 17 additions and 6 deletions

View file

@ -751,13 +751,20 @@ impl UnusedDelimLint for UnusedParens {
if !Self::is_expr_delims_necessary(inner, followed_by_block) if !Self::is_expr_delims_necessary(inner, followed_by_block)
&& value.attrs.is_empty() && value.attrs.is_empty()
&& !value.span.from_expansion() && !value.span.from_expansion()
&& (ctx != UnusedDelimsCtx::LetScrutineeExpr
|| match inner.kind {
ast::ExprKind::Binary(
rustc_span::source_map::Spanned { node, .. },
_,
_,
) if node.lazy() => false,
_ => true,
})
{ {
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos) self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos)
} }
} }
ast::ExprKind::Let(_, ref expr) => { ast::ExprKind::Let(_, ref expr) => {
// FIXME(#60336): Properly handle `let true = (false && true)`
// actually needing the parenthesis.
self.check_unused_delims_expr( self.check_unused_delims_expr(
cx, cx,
expr, expr,

View file

@ -60,6 +60,8 @@ fn main() {
if (v == X { y: true }) {} if (v == X { y: true }) {}
if (X { y: true } == v) {} if (X { y: true } == v) {}
if (X { y: false }.y) {} if (X { y: false }.y) {}
// this shouldn't warn, because the parens are necessary to disambiguate let chains
if let true = (true && false) {}
while (X { y: false }.foo(true)) {} while (X { y: false }.foo(true)) {}
while (true | X { y: false }.y) {} while (true | X { y: false }.y) {}

View file

@ -60,6 +60,8 @@ fn main() {
if (v == X { y: true }) {} if (v == X { y: true }) {}
if (X { y: true } == v) {} if (X { y: true } == v) {}
if (X { y: false }.y) {} if (X { y: false }.y) {}
// this shouldn't warn, because the parens are necessary to disambiguate let chains
if let true = (true && false) {}
while (X { y: false }.foo(true)) {} while (X { y: false }.foo(true)) {}
while (true | X { y: false }.y) {} while (true | X { y: false }.y) {}

View file

@ -83,25 +83,25 @@ LL | while let 1 = (2) {}
| ^^^ help: remove these parentheses | ^^^ help: remove these parentheses
error: unnecessary parentheses around method argument error: unnecessary parentheses around method argument
--> $DIR/lint-unnecessary-parens.rs:71:24 --> $DIR/lint-unnecessary-parens.rs:73:24
| |
LL | X { y: false }.foo((true)); LL | X { y: false }.foo((true));
| ^^^^^^ help: remove these parentheses | ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:73:18 --> $DIR/lint-unnecessary-parens.rs:75:18
| |
LL | let mut _a = (0); LL | let mut _a = (0);
| ^^^ help: remove these parentheses | ^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:74:10 --> $DIR/lint-unnecessary-parens.rs:76:10
| |
LL | _a = (0); LL | _a = (0);
| ^^^ help: remove these parentheses | ^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:75:11 --> $DIR/lint-unnecessary-parens.rs:77:11
| |
LL | _a += (1); LL | _a += (1);
| ^^^ help: remove these parentheses | ^^^ help: remove these parentheses