Simply unused_parens check and add tests
This commit is contained in:
parent
5217527a5b
commit
46b07d670a
4 changed files with 95 additions and 18 deletions
|
|
@ -274,7 +274,7 @@ impl UnusedParens {
|
|||
parser::contains_exterior_struct_lit(&inner);
|
||||
if !necessary {
|
||||
let pattern = pprust::expr_to_string(value);
|
||||
Self::remove_outer_parens(cx, value.span, &pattern, msg)
|
||||
Self::remove_outer_parens(cx, value.span, &pattern, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -282,13 +282,10 @@ impl UnusedParens {
|
|||
fn check_unused_parens_pat(&self,
|
||||
cx: &EarlyContext,
|
||||
value: &ast::Pat,
|
||||
msg: &str,
|
||||
struct_lit_needs_parens: bool) {
|
||||
msg: &str) {
|
||||
if let ast::PatKind::Paren(_) = value.node {
|
||||
if !struct_lit_needs_parens {
|
||||
let pattern = pprust::pat_to_string(value);
|
||||
Self::remove_outer_parens(cx, value.span, &pattern, msg)
|
||||
}
|
||||
let pattern = pprust::pat_to_string(value);
|
||||
Self::remove_outer_parens(cx, value.span, &pattern, msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -355,7 +352,9 @@ impl EarlyLintPass for UnusedParens {
|
|||
// first "argument" is self (which sometimes needs parens)
|
||||
MethodCall(_, ref args) => (&args[1..], "method"),
|
||||
// actual catch-all arm
|
||||
_ => { return; }
|
||||
_ => {
|
||||
return;
|
||||
}
|
||||
};
|
||||
// Don't lint if this is a nested macro expansion: otherwise, the lint could
|
||||
// trigger in situations that macro authors shouldn't have to care about, e.g.,
|
||||
|
|
@ -377,15 +376,9 @@ impl EarlyLintPass for UnusedParens {
|
|||
}
|
||||
|
||||
fn check_pat(&mut self, cx: &EarlyContext, p: &ast::Pat) {
|
||||
use ast::PatKind::*;
|
||||
let (value, msg, struct_lit_needs_parens) = match p.node {
|
||||
Ident(.., Some(ref pat)) => (pat, "optional subpattern", false),
|
||||
Ref(ref pat, _) => (pat, "reference pattern", false),
|
||||
Slice(_, Some(ref pat), _) => (pat, "optional position pattern", false),
|
||||
Paren(_) => (p, "pattern", false),
|
||||
_ => return,
|
||||
};
|
||||
self.check_unused_parens_pat(cx, &value, msg, struct_lit_needs_parens);
|
||||
if let ast::PatKind::Paren(_) = p.node {
|
||||
self.check_unused_parens_pat(cx, &p, "pattern");
|
||||
}
|
||||
}
|
||||
|
||||
fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,6 @@
|
|||
|
||||
fn main() {
|
||||
match 0 {
|
||||
(pat) => assert_eq!(pat, 0)
|
||||
pat => assert_eq!(pat, 0)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
42
src/test/ui/lint/issue-54538-unused-parens-lint.rs
Normal file
42
src/test/ui/lint/issue-54538-unused-parens-lint.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// compile-pass
|
||||
|
||||
#![allow(unreachable_patterns)]
|
||||
#![allow(unused_variables)]
|
||||
#![warn(unused_parens)]
|
||||
|
||||
struct A {
|
||||
field: Option<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = 3;
|
||||
match x {
|
||||
(_) => {} //~ WARNING: unnecessary parentheses around pattern
|
||||
(y) => {} //~ WARNING: unnecessary parentheses around pattern
|
||||
(ref r) => {} //~ WARNING: unnecessary parentheses around pattern
|
||||
e @ 1...2 | (e @ (3...4)) => {}
|
||||
//~^ WARNING: unnecessary parentheses around pattern (3 ... 4)
|
||||
//~^ WARNING: unnecessary parentheses around pattern (e @ _)
|
||||
}
|
||||
|
||||
let field = "foo".to_string();
|
||||
let x: Option<A> = Some(A { field: Some(field) });
|
||||
match x {
|
||||
Some(A {
|
||||
field: (ref a @ Some(_)),
|
||||
//~^ WARNING: unnecessary parentheses around pattern
|
||||
..
|
||||
}) => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
42
src/test/ui/lint/issue-54538-unused-parens-lint.stderr
Normal file
42
src/test/ui/lint/issue-54538-unused-parens-lint.stderr
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
warning: unnecessary parentheses around pattern
|
||||
--> $DIR/issue-54538-unused-parens-lint.rs:24:9
|
||||
|
|
||||
LL | (_) => {} //~ WARNING: unnecessary parentheses around pattern
|
||||
| ^^^ help: remove these parentheses
|
||||
|
|
||||
note: lint level defined here
|
||||
--> $DIR/issue-54538-unused-parens-lint.rs:15:9
|
||||
|
|
||||
LL | #![warn(unused_parens)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
warning: unnecessary parentheses around pattern
|
||||
--> $DIR/issue-54538-unused-parens-lint.rs:25:9
|
||||
|
|
||||
LL | (y) => {} //~ WARNING: unnecessary parentheses around pattern
|
||||
| ^^^ help: remove these parentheses
|
||||
|
||||
warning: unnecessary parentheses around pattern
|
||||
--> $DIR/issue-54538-unused-parens-lint.rs:26:9
|
||||
|
|
||||
LL | (ref r) => {} //~ WARNING: unnecessary parentheses around pattern
|
||||
| ^^^^^^^ help: remove these parentheses
|
||||
|
||||
warning: unnecessary parentheses around pattern
|
||||
--> $DIR/issue-54538-unused-parens-lint.rs:27:21
|
||||
|
|
||||
LL | e @ 1...2 | (e @ (3...4)) => {}
|
||||
| ^^^^^^^^^^^^^ help: remove these parentheses
|
||||
|
||||
warning: unnecessary parentheses around pattern
|
||||
--> $DIR/issue-54538-unused-parens-lint.rs:27:26
|
||||
|
|
||||
LL | e @ 1...2 | (e @ (3...4)) => {}
|
||||
| ^^^^^^^ help: remove these parentheses
|
||||
|
||||
warning: unnecessary parentheses around pattern
|
||||
--> $DIR/issue-54538-unused-parens-lint.rs:36:20
|
||||
|
|
||||
LL | field: (ref a @ Some(_)),
|
||||
| ^^^^^^^^^^^^^^^^^ help: remove these parentheses
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue