From 46b07d670a168e832391bba35d58f823cd00b76d Mon Sep 17 00:00:00 2001 From: Kevin Leimkuhler Date: Fri, 5 Oct 2018 19:09:14 -0700 Subject: [PATCH] Simply unused_parens check and add tests --- src/librustc_lint/unused.rs | 27 +++++------- src/test/run-pass/binding/pat-tuple-7.rs | 2 +- .../ui/lint/issue-54538-unused-parens-lint.rs | 42 +++++++++++++++++++ .../issue-54538-unused-parens-lint.stderr | 42 +++++++++++++++++++ 4 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 src/test/ui/lint/issue-54538-unused-parens-lint.rs create mode 100644 src/test/ui/lint/issue-54538-unused-parens-lint.stderr diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index a176bd830260..0ef46e77280d 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -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) { diff --git a/src/test/run-pass/binding/pat-tuple-7.rs b/src/test/run-pass/binding/pat-tuple-7.rs index c32b52eac33d..f02e3198984f 100644 --- a/src/test/run-pass/binding/pat-tuple-7.rs +++ b/src/test/run-pass/binding/pat-tuple-7.rs @@ -12,6 +12,6 @@ fn main() { match 0 { - (pat) => assert_eq!(pat, 0) + pat => assert_eq!(pat, 0) } } diff --git a/src/test/ui/lint/issue-54538-unused-parens-lint.rs b/src/test/ui/lint/issue-54538-unused-parens-lint.rs new file mode 100644 index 000000000000..5702a8941d46 --- /dev/null +++ b/src/test/ui/lint/issue-54538-unused-parens-lint.rs @@ -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 or the MIT license +// , 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, +} + +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 = Some(A { field: Some(field) }); + match x { + Some(A { + field: (ref a @ Some(_)), + //~^ WARNING: unnecessary parentheses around pattern + .. + }) => {} + _ => {} + } +} diff --git a/src/test/ui/lint/issue-54538-unused-parens-lint.stderr b/src/test/ui/lint/issue-54538-unused-parens-lint.stderr new file mode 100644 index 000000000000..7d9eb697fd7e --- /dev/null +++ b/src/test/ui/lint/issue-54538-unused-parens-lint.stderr @@ -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 +