Skip to content

Commit 46b07d6

Browse files
committed
Simply unused_parens check and add tests
1 parent 5217527 commit 46b07d6

File tree

4 files changed

+95
-18
lines changed

4 files changed

+95
-18
lines changed

src/librustc_lint/unused.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -274,21 +274,18 @@ impl UnusedParens {
274274
parser::contains_exterior_struct_lit(&inner);
275275
if !necessary {
276276
let pattern = pprust::expr_to_string(value);
277-
Self::remove_outer_parens(cx, value.span, &pattern, msg)
277+
Self::remove_outer_parens(cx, value.span, &pattern, msg);
278278
}
279279
}
280280
}
281281

282282
fn check_unused_parens_pat(&self,
283283
cx: &EarlyContext,
284284
value: &ast::Pat,
285-
msg: &str,
286-
struct_lit_needs_parens: bool) {
285+
msg: &str) {
287286
if let ast::PatKind::Paren(_) = value.node {
288-
if !struct_lit_needs_parens {
289-
let pattern = pprust::pat_to_string(value);
290-
Self::remove_outer_parens(cx, value.span, &pattern, msg)
291-
}
287+
let pattern = pprust::pat_to_string(value);
288+
Self::remove_outer_parens(cx, value.span, &pattern, msg);
292289
}
293290
}
294291

@@ -355,7 +352,9 @@ impl EarlyLintPass for UnusedParens {
355352
// first "argument" is self (which sometimes needs parens)
356353
MethodCall(_, ref args) => (&args[1..], "method"),
357354
// actual catch-all arm
358-
_ => { return; }
355+
_ => {
356+
return;
357+
}
359358
};
360359
// Don't lint if this is a nested macro expansion: otherwise, the lint could
361360
// trigger in situations that macro authors shouldn't have to care about, e.g.,
@@ -377,15 +376,9 @@ impl EarlyLintPass for UnusedParens {
377376
}
378377

379378
fn check_pat(&mut self, cx: &EarlyContext, p: &ast::Pat) {
380-
use ast::PatKind::*;
381-
let (value, msg, struct_lit_needs_parens) = match p.node {
382-
Ident(.., Some(ref pat)) => (pat, "optional subpattern", false),
383-
Ref(ref pat, _) => (pat, "reference pattern", false),
384-
Slice(_, Some(ref pat), _) => (pat, "optional position pattern", false),
385-
Paren(_) => (p, "pattern", false),
386-
_ => return,
387-
};
388-
self.check_unused_parens_pat(cx, &value, msg, struct_lit_needs_parens);
379+
if let ast::PatKind::Paren(_) = p.node {
380+
self.check_unused_parens_pat(cx, &p, "pattern");
381+
}
389382
}
390383

391384
fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {

src/test/run-pass/binding/pat-tuple-7.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
fn main() {
1414
match 0 {
15-
(pat) => assert_eq!(pat, 0)
15+
pat => assert_eq!(pat, 0)
1616
}
1717
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-pass
12+
13+
#![allow(unreachable_patterns)]
14+
#![allow(unused_variables)]
15+
#![warn(unused_parens)]
16+
17+
struct A {
18+
field: Option<String>,
19+
}
20+
21+
fn main() {
22+
let x = 3;
23+
match x {
24+
(_) => {} //~ WARNING: unnecessary parentheses around pattern
25+
(y) => {} //~ WARNING: unnecessary parentheses around pattern
26+
(ref r) => {} //~ WARNING: unnecessary parentheses around pattern
27+
e @ 1...2 | (e @ (3...4)) => {}
28+
//~^ WARNING: unnecessary parentheses around pattern (3 ... 4)
29+
//~^ WARNING: unnecessary parentheses around pattern (e @ _)
30+
}
31+
32+
let field = "foo".to_string();
33+
let x: Option<A> = Some(A { field: Some(field) });
34+
match x {
35+
Some(A {
36+
field: (ref a @ Some(_)),
37+
//~^ WARNING: unnecessary parentheses around pattern
38+
..
39+
}) => {}
40+
_ => {}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
warning: unnecessary parentheses around pattern
2+
--> $DIR/issue-54538-unused-parens-lint.rs:24:9
3+
|
4+
LL | (_) => {} //~ WARNING: unnecessary parentheses around pattern
5+
| ^^^ help: remove these parentheses
6+
|
7+
note: lint level defined here
8+
--> $DIR/issue-54538-unused-parens-lint.rs:15:9
9+
|
10+
LL | #![warn(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
13+
warning: unnecessary parentheses around pattern
14+
--> $DIR/issue-54538-unused-parens-lint.rs:25:9
15+
|
16+
LL | (y) => {} //~ WARNING: unnecessary parentheses around pattern
17+
| ^^^ help: remove these parentheses
18+
19+
warning: unnecessary parentheses around pattern
20+
--> $DIR/issue-54538-unused-parens-lint.rs:26:9
21+
|
22+
LL | (ref r) => {} //~ WARNING: unnecessary parentheses around pattern
23+
| ^^^^^^^ help: remove these parentheses
24+
25+
warning: unnecessary parentheses around pattern
26+
--> $DIR/issue-54538-unused-parens-lint.rs:27:21
27+
|
28+
LL | e @ 1...2 | (e @ (3...4)) => {}
29+
| ^^^^^^^^^^^^^ help: remove these parentheses
30+
31+
warning: unnecessary parentheses around pattern
32+
--> $DIR/issue-54538-unused-parens-lint.rs:27:26
33+
|
34+
LL | e @ 1...2 | (e @ (3...4)) => {}
35+
| ^^^^^^^ help: remove these parentheses
36+
37+
warning: unnecessary parentheses around pattern
38+
--> $DIR/issue-54538-unused-parens-lint.rs:36:20
39+
|
40+
LL | field: (ref a @ Some(_)),
41+
| ^^^^^^^^^^^^^^^^^ help: remove these parentheses
42+

0 commit comments

Comments
 (0)