]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/misc_early.rs
Rustup to ea0dc9297283daff6486807f43e190b4eb561412
[rust.git] / clippy_lints / src / misc_early.rs
1 use rustc::lint::*;
2 use std::collections::HashMap;
3 use syntax::ast::*;
4 use syntax::codemap::Span;
5 use syntax::visit::FnKind;
6 use utils::{span_lint, span_help_and_lint, snippet, span_lint_and_then};
7 /// **What it does:** This lint checks for structure field patterns bound to wildcards.
8 ///
9 /// **Why is this bad?** Using `..` instead is shorter and leaves the focus on the fields that are actually bound.
10 ///
11 /// **Known problems:** None.
12 ///
13 /// **Example:** `let { a: _, b: ref b, c: _ } = ..`
14 declare_lint! {
15     pub UNNEEDED_FIELD_PATTERN, Warn,
16     "Struct fields are bound to a wildcard instead of using `..`"
17 }
18
19 /// **What it does:** This lint checks for function arguments having the similar names differing by an underscore
20 ///
21 /// **Why is this bad?** It affects code readability
22 ///
23 /// **Known problems:** None.
24 ///
25 /// **Example:** `fn foo(a: i32, _a: i32) {}`
26 declare_lint! {
27     pub DUPLICATE_UNDERSCORE_ARGUMENT, Warn,
28     "Function arguments having names which only differ by an underscore"
29 }
30
31 /// **What it does:** This lint detects closures called in the same expression where they are defined.
32 ///
33 /// **Why is this bad?** It is unnecessarily adding to the expression's complexity.
34 ///
35 /// **Known problems:** None.
36 ///
37 /// **Example:** `(|| 42)()`
38 declare_lint! {
39     pub REDUNDANT_CLOSURE_CALL, Warn,
40     "Closures should not be called in the expression they are defined"
41 }
42
43 /// **What it does:** This lint detects expressions of the form `--x`
44 ///
45 /// **Why is this bad?** It can mislead C/C++ programmers to think `x` was decremented.
46 ///
47 /// **Known problems:** None.
48 ///
49 /// **Example:** `--x;`
50 declare_lint! {
51     pub DOUBLE_NEG, Warn,
52     "`--x` is a double negation of `x` and not a pre-decrement as in C or C++"
53 }
54
55
56 #[derive(Copy, Clone)]
57 pub struct MiscEarly;
58
59 impl LintPass for MiscEarly {
60     fn get_lints(&self) -> LintArray {
61         lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL, DOUBLE_NEG)
62     }
63 }
64
65 impl EarlyLintPass for MiscEarly {
66     fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
67         if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
68             let mut wilds = 0;
69             let type_name = npat.segments.last().expect("A path must have at least one segment").identifier.name;
70
71             for field in pfields {
72                 if field.node.pat.node == PatKind::Wild {
73                     wilds += 1;
74                 }
75             }
76             if !pfields.is_empty() && wilds == pfields.len() {
77                 span_help_and_lint(cx,
78                                    UNNEEDED_FIELD_PATTERN,
79                                    pat.span,
80                                    "All the struct fields are matched to a wildcard pattern, consider using `..`.",
81                                    &format!("Try with `{} {{ .. }}` instead", type_name));
82                 return;
83             }
84             if wilds > 0 {
85                 let mut normal = vec![];
86
87                 for field in pfields {
88                     if field.node.pat.node != PatKind::Wild {
89                         if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
90                             normal.push(n);
91                         }
92                     }
93                 }
94                 for field in pfields {
95                     if field.node.pat.node == PatKind::Wild {
96                         wilds -= 1;
97                         if wilds > 0 {
98                             span_lint(cx,
99                                       UNNEEDED_FIELD_PATTERN,
100                                       field.span,
101                                       "You matched a field with a wildcard pattern. Consider using `..` instead");
102                         } else {
103                             span_help_and_lint(cx,
104                                                UNNEEDED_FIELD_PATTERN,
105                                                field.span,
106                                                "You matched a field with a wildcard pattern. Consider using `..` \
107                                                 instead",
108                                                &format!("Try with `{} {{ {}, .. }}`",
109                                                         type_name,
110                                                         normal[..].join(", ")));
111                         }
112                     }
113                 }
114             }
115         }
116     }
117
118     fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
119         let mut registered_names: HashMap<String, Span> = HashMap::new();
120
121         for ref arg in &decl.inputs {
122             if let PatKind::Ident(_, sp_ident, None) = arg.pat.node {
123                 let arg_name = sp_ident.node.to_string();
124
125                 if arg_name.starts_with('_') {
126                     if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
127                         span_lint(cx,
128                                   DUPLICATE_UNDERSCORE_ARGUMENT,
129                                   *correspondence,
130                                   &format!("`{}` already exists, having another argument having almost the same \
131                                             name makes code comprehension and documentation more difficult",
132                                            arg_name[1..].to_owned()));;
133                     }
134                 } else {
135                     registered_names.insert(arg_name, arg.pat.span);
136                 }
137             }
138         }
139     }
140
141     fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
142         match expr.node {
143             ExprKind::Call(ref paren, _) => {
144                 if let ExprKind::Paren(ref closure) = paren.node {
145                     if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
146                         span_lint_and_then(cx,
147                                            REDUNDANT_CLOSURE_CALL,
148                                            expr.span,
149                                            "Try not to call a closure in the expression where it is declared.",
150                                            |db| {
151                                                if decl.inputs.is_empty() {
152                                                    let hint = format!("{}", snippet(cx, block.span, ".."));
153                                                    db.span_suggestion(expr.span, "Try doing something like: ", hint);
154                                                }
155                                            });
156                     }
157                 }
158             }
159             ExprKind::Unary(UnOp::Neg, ref inner) => {
160                 if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
161                     span_lint(cx,
162                               DOUBLE_NEG,
163                               expr.span,
164                               "`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
165     }
166             }
167             _ => ()
168         }
169     }
170
171     fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
172         for w in block.stmts.windows(2) {
173             if_let_chain! {[
174                 let StmtKind::Local(ref local) = w[0].node,
175                 let Option::Some(ref t) = local.init,
176                 let ExprKind::Closure(_, _, _, _) = t.node,
177                 let PatKind::Ident(_, sp_ident, _) = local.pat.node,
178                 let StmtKind::Semi(ref second) = w[1].node,
179                 let ExprKind::Assign(_, ref call) = second.node,
180                 let ExprKind::Call(ref closure, _) = call.node,
181                 let ExprKind::Path(_, ref path) = closure.node
182             ], {
183                 if sp_ident.node == (&path.segments[0]).identifier {
184                     span_lint(cx, REDUNDANT_CLOSURE_CALL, second.span, "Closure called just once immediately after it was declared");
185                 }
186             }}
187         }
188     }
189 }