]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/double_parens.rs
Merge pull request #3494 from daxpedda/master
[rust.git] / clippy_lints / src / double_parens.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
11 use crate::rustc::{declare_tool_lint, lint_array};
12 use crate::syntax::ast::*;
13 use crate::utils::{in_macro, span_lint};
14
15 /// **What it does:** Checks for unnecessary double parentheses.
16 ///
17 /// **Why is this bad?** This makes code harder to read and might indicate a
18 /// mistake.
19 ///
20 /// **Known problems:** None.
21 ///
22 /// **Example:**
23 /// ```rust
24 /// ((0))
25 /// foo((0))
26 /// ((1, 2))
27 /// ```
28 declare_clippy_lint! {
29     pub DOUBLE_PARENS,
30     complexity,
31     "Warn on unnecessary double parentheses"
32 }
33
34 #[derive(Copy, Clone)]
35 pub struct DoubleParens;
36
37 impl LintPass for DoubleParens {
38     fn get_lints(&self) -> LintArray {
39         lint_array!(DOUBLE_PARENS)
40     }
41 }
42
43 impl EarlyLintPass for DoubleParens {
44     fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
45         if in_macro(expr.span) {
46             return;
47         }
48
49         match expr.node {
50             ExprKind::Paren(ref in_paren) => match in_paren.node {
51                 ExprKind::Paren(_) | ExprKind::Tup(_) => {
52                     span_lint(
53                         cx,
54                         DOUBLE_PARENS,
55                         expr.span,
56                         "Consider removing unnecessary double parentheses",
57                     );
58                 },
59                 _ => {},
60             },
61             ExprKind::Call(_, ref params) => {
62                 if params.len() == 1 {
63                     let param = &params[0];
64                     if let ExprKind::Paren(_) = param.node {
65                         span_lint(
66                             cx,
67                             DOUBLE_PARENS,
68                             param.span,
69                             "Consider removing unnecessary double parentheses",
70                         );
71                     }
72                 }
73             },
74             ExprKind::MethodCall(_, ref params) => {
75                 if params.len() == 2 {
76                     let param = &params[1];
77                     if let ExprKind::Paren(_) = param.node {
78                         span_lint(
79                             cx,
80                             DOUBLE_PARENS,
81                             param.span,
82                             "Consider removing unnecessary double parentheses",
83                         );
84                     }
85                 }
86             },
87             _ => {},
88         }
89     }
90 }