]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/double_parens.rs
Merge pull request #3269 from rust-lang-nursery/relicense
[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
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};
15
16
17 /// **What it does:** Checks for unnecessary double parentheses.
18 ///
19 /// **Why is this bad?** This makes code harder to read and might indicate a
20 /// mistake.
21 ///
22 /// **Known problems:** None.
23 ///
24 /// **Example:**
25 /// ```rust
26 /// ((0))
27 /// foo((0))
28 /// ((1, 2))
29 /// ```
30 declare_clippy_lint! {
31     pub DOUBLE_PARENS,
32     complexity,
33     "Warn on unnecessary double parentheses"
34 }
35
36 #[derive(Copy, Clone)]
37 pub struct DoubleParens;
38
39 impl LintPass for DoubleParens {
40     fn get_lints(&self) -> LintArray {
41         lint_array!(DOUBLE_PARENS)
42     }
43 }
44
45 impl EarlyLintPass for DoubleParens {
46     fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
47         if in_macro(expr.span) {
48             return;
49         }
50
51         match expr.node {
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");
55                 },
56                 _ => {},
57             },
58             ExprKind::Call(_, ref params) => if params.len() == 1 {
59                 let param = &params[0];
60                 if let ExprKind::Paren(_) = param.node {
61                     span_lint(cx, DOUBLE_PARENS, param.span, "Consider removing unnecessary double parentheses");
62                 }
63             },
64             ExprKind::MethodCall(_, ref params) => if params.len() == 2 {
65                 let param = &params[1];
66                 if let ExprKind::Paren(_) = param.node {
67                     span_lint(cx, DOUBLE_PARENS, param.span, "Consider removing unnecessary double parentheses");
68                 }
69             },
70             _ => {},
71         }
72     }
73 }