Skip to content

Commit 7ba24e8

Browse files
authored
Rollup merge of rust-lang#54820 - kleimkuhler:issue-54538-unused_patterns-lint, r=nikomatsakis
Closes rust-lang#54538: `unused_patterns` lint Closes rust-lang#54538 r? @nikomatsakis
2 parents 4f9b581 + 0e411c2 commit 7ba24e8

File tree

4 files changed

+150
-42
lines changed

4 files changed

+150
-42
lines changed

src/librustc_lint/unused.rs

+69-42
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ declare_lint! {
272272
pub struct UnusedParens;
273273

274274
impl UnusedParens {
275-
fn check_unused_parens_core(&self,
275+
fn check_unused_parens_expr(&self,
276276
cx: &EarlyContext,
277277
value: &ast::Expr,
278278
msg: &str,
@@ -281,46 +281,57 @@ impl UnusedParens {
281281
let necessary = struct_lit_needs_parens &&
282282
parser::contains_exterior_struct_lit(&inner);
283283
if !necessary {
284-
let span_msg = format!("unnecessary parentheses around {}", msg);
285-
let mut err = cx.struct_span_lint(UNUSED_PARENS,
286-
value.span,
287-
&span_msg);
288-
// Remove exactly one pair of parentheses (rather than naïvely
289-
// stripping all paren characters)
290-
let mut ate_left_paren = false;
291-
let mut ate_right_paren = false;
292-
let parens_removed = pprust::expr_to_string(value)
293-
.trim_matches(|c| {
294-
match c {
295-
'(' => {
296-
if ate_left_paren {
297-
false
298-
} else {
299-
ate_left_paren = true;
300-
true
301-
}
302-
},
303-
')' => {
304-
if ate_right_paren {
305-
false
306-
} else {
307-
ate_right_paren = true;
308-
true
309-
}
310-
},
311-
_ => false,
312-
}
313-
}).to_owned();
314-
err.span_suggestion_short_with_applicability(
315-
value.span,
316-
"remove these parentheses",
317-
parens_removed,
318-
Applicability::MachineApplicable
319-
);
320-
err.emit();
284+
let pattern = pprust::expr_to_string(value);
285+
Self::remove_outer_parens(cx, value.span, &pattern, msg);
321286
}
322287
}
323288
}
289+
290+
fn check_unused_parens_pat(&self,
291+
cx: &EarlyContext,
292+
value: &ast::Pat,
293+
msg: &str) {
294+
if let ast::PatKind::Paren(_) = value.node {
295+
let pattern = pprust::pat_to_string(value);
296+
Self::remove_outer_parens(cx, value.span, &pattern, msg);
297+
}
298+
}
299+
300+
fn remove_outer_parens(cx: &EarlyContext, span: Span, pattern: &str, msg: &str) {
301+
let span_msg = format!("unnecessary parentheses around {}", msg);
302+
let mut err = cx.struct_span_lint(UNUSED_PARENS, span, &span_msg);
303+
let mut ate_left_paren = false;
304+
let mut ate_right_paren = false;
305+
let parens_removed = pattern
306+
.trim_matches(|c| {
307+
match c {
308+
'(' => {
309+
if ate_left_paren {
310+
false
311+
} else {
312+
ate_left_paren = true;
313+
true
314+
}
315+
},
316+
')' => {
317+
if ate_right_paren {
318+
false
319+
} else {
320+
ate_right_paren = true;
321+
true
322+
}
323+
},
324+
_ => false,
325+
}
326+
}).to_owned();
327+
err.span_suggestion_short_with_applicability(
328+
span,
329+
"remove these parentheses",
330+
parens_removed,
331+
Applicability::MachineApplicable
332+
);
333+
err.emit();
334+
}
324335
}
325336

326337
impl LintPass for UnusedParens {
@@ -349,7 +360,9 @@ impl EarlyLintPass for UnusedParens {
349360
// first "argument" is self (which sometimes needs parens)
350361
MethodCall(_, ref args) => (&args[1..], "method"),
351362
// actual catch-all arm
352-
_ => { return; }
363+
_ => {
364+
return;
365+
}
353366
};
354367
// Don't lint if this is a nested macro expansion: otherwise, the lint could
355368
// trigger in situations that macro authors shouldn't have to care about, e.g.,
@@ -362,18 +375,32 @@ impl EarlyLintPass for UnusedParens {
362375
}
363376
let msg = format!("{} argument", call_kind);
364377
for arg in args_to_check {
365-
self.check_unused_parens_core(cx, arg, &msg, false);
378+
self.check_unused_parens_expr(cx, arg, &msg, false);
366379
}
367380
return;
368381
}
369382
};
370-
self.check_unused_parens_core(cx, &value, msg, struct_lit_needs_parens);
383+
self.check_unused_parens_expr(cx, &value, msg, struct_lit_needs_parens);
384+
}
385+
386+
fn check_pat(&mut self, cx: &EarlyContext, p: &ast::Pat) {
387+
use ast::PatKind::{Paren, Range};
388+
// The lint visitor will visit each subpattern of `p`. We do not want to lint any range
389+
// pattern no matter where it occurs in the pattern. For something like `&(a..=b)`, there
390+
// is a recursive `check_pat` on `a` and `b`, but we will assume that if there are
391+
// unnecessry parens they serve a purpose of readability.
392+
if let Paren(ref pat) = p.node {
393+
match pat.node {
394+
Range(..) => {}
395+
_ => self.check_unused_parens_pat(cx, &p, "pattern")
396+
}
397+
}
371398
}
372399

373400
fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {
374401
if let ast::StmtKind::Local(ref local) = s.node {
375402
if let Some(ref value) = local.init {
376-
self.check_unused_parens_core(cx, &value, "assigned value", false);
403+
self.check_unused_parens_expr(cx, &value, "assigned value", false);
377404
}
378405
}
379406
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// run-pass
1212

1313
fn main() {
14+
#[allow(unused_parens)]
1415
match 0 {
1516
(pat) => assert_eq!(pat, 0)
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
fn main() {
18+
match 1 {
19+
(_) => {} //~ WARNING: unnecessary parentheses around pattern
20+
(y) => {} //~ WARNING: unnecessary parentheses around pattern
21+
(ref r) => {} //~ WARNING: unnecessary parentheses around pattern
22+
(e @ 1..=2) => {} //~ WARNING: unnecessary parentheses around outer pattern
23+
(1..=2) => {} // Non ambiguous range pattern should not warn
24+
e @ (3..=4) => {} // Non ambiguous range pattern should not warn
25+
}
26+
27+
match &1 {
28+
(e @ &(1...2)) => {} //~ WARNING: unnecessary parentheses around outer pattern
29+
&(_) => {} //~ WARNING: unnecessary parentheses around pattern
30+
e @ &(1...2) => {} // Ambiguous range pattern should not warn
31+
&(1..=2) => {} // Ambiguous range pattern should not warn
32+
}
33+
34+
match &1 {
35+
e @ &(1...2) | e @ &(3..=4) => {} // Complex ambiguous pattern should not warn
36+
&_ => {}
37+
}
38+
}
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:19: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:20: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:21: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:22:9
27+
|
28+
LL | (e @ 1..=2) => {} //~ WARNING: unnecessary parentheses around outer pattern
29+
| ^^^^^^^^^^^ help: remove these parentheses
30+
31+
warning: unnecessary parentheses around pattern
32+
--> $DIR/issue-54538-unused-parens-lint.rs:28:9
33+
|
34+
LL | (e @ &(1...2)) => {} //~ WARNING: unnecessary parentheses around outer pattern
35+
| ^^^^^^^^^^^^^^ help: remove these parentheses
36+
37+
warning: unnecessary parentheses around pattern
38+
--> $DIR/issue-54538-unused-parens-lint.rs:29:10
39+
|
40+
LL | &(_) => {} //~ WARNING: unnecessary parentheses around pattern
41+
| ^^^ help: remove these parentheses
42+

0 commit comments

Comments
 (0)