1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 use crate::syntax::ast::*;
12 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::utils::{in_macro, span_lint};
17 /// **What it does:** Checks for unnecessary double parentheses.
19 /// **Why is this bad?** This makes code harder to read and might indicate a
22 /// **Known problems:** None.
30 declare_clippy_lint! {
33 "Warn on unnecessary double parentheses"
36 #[derive(Copy, Clone)]
37 pub struct DoubleParens;
39 impl LintPass for DoubleParens {
40 fn get_lints(&self) -> LintArray {
41 lint_array!(DOUBLE_PARENS)
45 impl EarlyLintPass for DoubleParens {
46 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
47 if in_macro(expr.span) {
52 ExprKind::Paren(ref in_paren) => match in_paren.node {
53 ExprKind::Paren(_) | ExprKind::Tup(_) => {
54 span_lint(cx, DOUBLE_PARENS, expr.span, "Consider removing unnecessary double parentheses");
58 ExprKind::Call(_, ref params) => if params.len() == 1 {
59 let param = ¶ms[0];
60 if let ExprKind::Paren(_) = param.node {
61 span_lint(cx, DOUBLE_PARENS, param.span, "Consider removing unnecessary double parentheses");
64 ExprKind::MethodCall(_, ref params) => if params.len() == 2 {
65 let param = ¶ms[1];
66 if let ExprKind::Paren(_) = param.node {
67 span_lint(cx, DOUBLE_PARENS, param.span, "Consider removing unnecessary double parentheses");