Skip to content

Commit

Permalink
Rollup merge of rust-lang#64472 - Mark-Simulacrum:unused-parens-attr,…
Browse files Browse the repository at this point in the history
… r=varkor

Don't mark expression with attributes as not needing parentheses

This is not perfectly correct as `#[attr] (5)` will still not lint, but it does seem good enough, in particular as the parentheses in that case are not unambiguously incorrect; I might personally prefer to see them for clarity.

Fixes rust-lang#43279.
  • Loading branch information
Centril authored Sep 15, 2019
2 parents 2e11e81 + 1d33f06 commit 0592d13
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ impl UnusedParens {
right_pos: Option<BytePos>) {
match value.node {
ast::ExprKind::Paren(ref inner) => {
if !Self::is_expr_parens_necessary(inner, followed_by_block) {
if !Self::is_expr_parens_necessary(inner, followed_by_block) &&
value.attrs.is_empty() {
let expr_text = if let Ok(snippet) = cx.sess().source_map()
.span_to_snippet(value.span) {
snippet
Expand Down
6 changes: 5 additions & 1 deletion src/test/ui/lint/issue-54538-unused-parens-lint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(box_patterns)]
#![feature(box_patterns, stmt_expr_attributes)]

#![feature(or_patterns)]
//~^ WARN the feature `or_patterns` is incomplete
Expand All @@ -17,6 +17,10 @@ fn lint_on_top_level() {
let _ = |(a): u8| 0; //~ ERROR unnecessary parentheses around pattern
}

fn _no_lint_attr() {
let _x = #[allow(dead_code)] (1 + 2);
}

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

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:41:12
--> $DIR/issue-54538-unused-parens-lint.rs:45:12
|
LL | if let (0 | 1) = 0 {}
| ^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:42:13
--> $DIR/issue-54538-unused-parens-lint.rs:46:13
|
LL | if let ((0 | 1),) = (0,) {}
| ^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:43:13
--> $DIR/issue-54538-unused-parens-lint.rs:47:13
|
LL | if let [(0 | 1)] = [0] {}
| ^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:44:16
--> $DIR/issue-54538-unused-parens-lint.rs:48:16
|
LL | if let 0 | (1 | 2) = 0 {}
| ^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:46:15
--> $DIR/issue-54538-unused-parens-lint.rs:50:15
|
LL | if let TS((0 | 1)) = TS(0) {}
| ^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:48:20
--> $DIR/issue-54538-unused-parens-lint.rs:52:20
|
LL | if let NS { f: (0 | 1) } = (NS { f: 0 }) {}
| ^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:58:9
--> $DIR/issue-54538-unused-parens-lint.rs:62:9
|
LL | (_) => {}
| ^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:59:9
--> $DIR/issue-54538-unused-parens-lint.rs:63:9
|
LL | (y) => {}
| ^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:60:9
--> $DIR/issue-54538-unused-parens-lint.rs:64:9
|
LL | (ref r) => {}
| ^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:61:9
--> $DIR/issue-54538-unused-parens-lint.rs:65:9
|
LL | (e @ 1...2) => {}
| ^^^^^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:67:9
--> $DIR/issue-54538-unused-parens-lint.rs:71:9
|
LL | (e @ &(1...2)) => {}
| ^^^^^^^^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:68:10
--> $DIR/issue-54538-unused-parens-lint.rs:72:10
|
LL | &(_) => {}
| ^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:79:9
--> $DIR/issue-54538-unused-parens-lint.rs:83:9
|
LL | (_) => {}
| ^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:80:9
--> $DIR/issue-54538-unused-parens-lint.rs:84:9
|
LL | (y) => {}
| ^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:81:9
--> $DIR/issue-54538-unused-parens-lint.rs:85:9
|
LL | (ref r) => {}
| ^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:82:9
--> $DIR/issue-54538-unused-parens-lint.rs:86:9
|
LL | (e @ 1..=2) => {}
| ^^^^^^^^^^^ help: remove these parentheses

error: unnecessary parentheses around pattern
--> $DIR/issue-54538-unused-parens-lint.rs:88:9
--> $DIR/issue-54538-unused-parens-lint.rs:92:9
|
LL | (e @ &(1..=2)) => {}
| ^^^^^^^^^^^^^^ help: remove these parentheses

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

0 comments on commit 0592d13

Please sign in to comment.