Rollup merge of #66983 - weiznich:bugfix/issue_66295, r=estebank

Fix `unused_parens` triggers on macro by example code

Fix #66295

Unfortunately this does also break [an existing test](4787e97475/src/test/ui/lint/issue-47775-nested-macro-unnecessary-parens-arg.rs (L22)). I'm not sure how to handle that, because that seems to be quite similar to the allowed cases

If this gets accepted it would be great to backport this fix to beta.
This commit is contained in:
Yuki Okushi 2019-12-12 10:09:19 +09:00 committed by GitHub
commit f642dc4124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 30 deletions

View File

@ -355,7 +355,9 @@ impl UnusedParens {
match value.kind {
ast::ExprKind::Paren(ref inner) => {
if !Self::is_expr_parens_necessary(inner, followed_by_block) &&
value.attrs.is_empty() {
value.attrs.is_empty() &&
!value.span.from_expansion()
{
let expr_text = if let Ok(snippet) = cx.sess().source_map()
.span_to_snippet(value.span) {
snippet

View File

@ -17,10 +17,7 @@ macro_rules! the_worship_the_heart_lifts_above {
macro_rules! and_the_heavens_reject_not {
() => {
// ↓ But let's test that we still lint for unused parens around
// function args inside of simple, one-deep macros.
#[allow(dead_code)] fn the_night_for_the_morrow() -> Option<isize> { Some((2)) }
//~^ WARN unnecessary parentheses around function argument
}
}

View File

@ -1,15 +0,0 @@
warning: unnecessary parentheses around function argument
--> $DIR/issue-47775-nested-macro-unnecessary-parens-arg.rs:22:83
|
LL | #[allow(dead_code)] fn the_night_for_the_morrow() -> Option<isize> { Some((2)) }
| ^^^ help: remove these parentheses
...
LL | and_the_heavens_reject_not!();
| ------------------------------ in this macro invocation
|
note: lint level defined here
--> $DIR/issue-47775-nested-macro-unnecessary-parens-arg.rs:3:9
|
LL | #![warn(unused_parens)]
| ^^^^^^^^^^^^^

View File

@ -25,6 +25,12 @@ fn passes_unused_parens_lint() -> &'static (dyn Trait) {
panic!()
}
macro_rules! baz {
($($foo:expr),+) => {
($($foo),*)
}
}
fn main() {
foo();
bar((true)); //~ ERROR unnecessary parentheses around function argument
@ -55,4 +61,7 @@ fn main() {
let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value
_a = (0); //~ ERROR unnecessary parentheses around assigned value
_a += (1); //~ ERROR unnecessary parentheses around assigned value
let _a = baz!(3, 4);
let _b = baz!(3);
}

View File

@ -23,25 +23,25 @@ LL | fn unused_parens_around_return_type() -> (u32) {
| ^^^^^ help: remove these parentheses
error: unnecessary parentheses around function argument
--> $DIR/lint-unnecessary-parens.rs:30:9
--> $DIR/lint-unnecessary-parens.rs:36:9
|
LL | bar((true));
| ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around `if` condition
--> $DIR/lint-unnecessary-parens.rs:32:8
--> $DIR/lint-unnecessary-parens.rs:38:8
|
LL | if (true) {}
| ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around `while` condition
--> $DIR/lint-unnecessary-parens.rs:33:11
--> $DIR/lint-unnecessary-parens.rs:39:11
|
LL | while (true) {}
| ^^^^^^ help: remove these parentheses
warning: denote infinite loops with `loop { ... }`
--> $DIR/lint-unnecessary-parens.rs:33:5
--> $DIR/lint-unnecessary-parens.rs:39:5
|
LL | while (true) {}
| ^^^^^^^^^^^^ help: use `loop`
@ -49,43 +49,43 @@ LL | while (true) {}
= note: `#[warn(while_true)]` on by default
error: unnecessary parentheses around `match` head expression
--> $DIR/lint-unnecessary-parens.rs:35:11
--> $DIR/lint-unnecessary-parens.rs:41:11
|
LL | match (true) {
| ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around `let` head expression
--> $DIR/lint-unnecessary-parens.rs:38:16
--> $DIR/lint-unnecessary-parens.rs:44:16
|
LL | if let 1 = (1) {}
| ^^^ help: remove these parentheses
error: unnecessary parentheses around `let` head expression
--> $DIR/lint-unnecessary-parens.rs:39:19
--> $DIR/lint-unnecessary-parens.rs:45:19
|
LL | while let 1 = (2) {}
| ^^^ help: remove these parentheses
error: unnecessary parentheses around method argument
--> $DIR/lint-unnecessary-parens.rs:53:24
--> $DIR/lint-unnecessary-parens.rs:59:24
|
LL | X { y: false }.foo((true));
| ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:55:18
--> $DIR/lint-unnecessary-parens.rs:61:18
|
LL | let mut _a = (0);
| ^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:56:10
--> $DIR/lint-unnecessary-parens.rs:62:10
|
LL | _a = (0);
| ^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:57:11
--> $DIR/lint-unnecessary-parens.rs:63:11
|
LL | _a += (1);
| ^^^ help: remove these parentheses