]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/misc_early.rs
2d6b0a357da55b64a48bc6ddb182b521f74952ae
[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, snippet_opt, 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:**
14 /// ```rust
15 /// let { a: _, b: ref b, c: _ } = ..
16 /// ```
17 declare_lint! {
18     pub UNNEEDED_FIELD_PATTERN, Warn,
19     "Struct fields are bound to a wildcard instead of using `..`"
20 }
21
22 /// **What it does:** This lint checks for function arguments having the similar names differing by an underscore
23 ///
24 /// **Why is this bad?** It affects code readability
25 ///
26 /// **Known problems:** None.
27 ///
28 /// **Example:**
29 /// ```rust
30 /// fn foo(a: i32, _a: i32) {}
31 /// ```
32 declare_lint! {
33     pub DUPLICATE_UNDERSCORE_ARGUMENT, Warn,
34     "Function arguments having names which only differ by an underscore"
35 }
36
37 /// **What it does:** This lint detects closures called in the same expression where they are defined.
38 ///
39 /// **Why is this bad?** It is unnecessarily adding to the expression's complexity.
40 ///
41 /// **Known problems:** None.
42 ///
43 /// **Example:**
44 /// ```rust
45 /// (|| 42)()
46 /// ```
47 declare_lint! {
48     pub REDUNDANT_CLOSURE_CALL, Warn,
49     "Closures should not be called in the expression they are defined"
50 }
51
52 /// **What it does:** This lint detects expressions of the form `--x`
53 ///
54 /// **Why is this bad?** It can mislead C/C++ programmers to think `x` was decremented.
55 ///
56 /// **Known problems:** None.
57 ///
58 /// **Example:**
59 /// ```rust
60 /// --x;
61 /// ```
62 declare_lint! {
63     pub DOUBLE_NEG, Warn,
64     "`--x` is a double negation of `x` and not a pre-decrement as in C or C++"
65 }
66
67 /// **What it does:** Warns on hexadecimal literals with mixed-case letter digits.
68 ///
69 /// **Why is this bad?** It looks confusing.
70 ///
71 /// **Known problems:** None.
72 ///
73 /// **Example:**
74 /// ```rust
75 /// let y = 0x1a9BAcD;
76 /// ```
77 declare_lint! {
78     pub MIXED_CASE_HEX_LITERALS, Warn,
79     "letter digits in hex literals should be either completely upper- or lowercased"
80 }
81
82
83 #[derive(Copy, Clone)]
84 pub struct MiscEarly;
85
86 impl LintPass for MiscEarly {
87     fn get_lints(&self) -> LintArray {
88         lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL,
89                     DOUBLE_NEG, MIXED_CASE_HEX_LITERALS)
90     }
91 }
92
93 impl EarlyLintPass for MiscEarly {
94     fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
95         if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
96             let mut wilds = 0;
97             let type_name = npat.segments.last().expect("A path must have at least one segment").identifier.name;
98
99             for field in pfields {
100                 if field.node.pat.node == PatKind::Wild {
101                     wilds += 1;
102                 }
103             }
104             if !pfields.is_empty() && wilds == pfields.len() {
105                 span_help_and_lint(cx,
106                                    UNNEEDED_FIELD_PATTERN,
107                                    pat.span,
108                                    "All the struct fields are matched to a wildcard pattern, consider using `..`.",
109                                    &format!("Try with `{} {{ .. }}` instead", type_name));
110                 return;
111             }
112             if wilds > 0 {
113                 let mut normal = vec![];
114
115                 for field in pfields {
116                     if field.node.pat.node != PatKind::Wild {
117                         if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
118                             normal.push(n);
119                         }
120                     }
121                 }
122                 for field in pfields {
123                     if field.node.pat.node == PatKind::Wild {
124                         wilds -= 1;
125                         if wilds > 0 {
126                             span_lint(cx,
127                                       UNNEEDED_FIELD_PATTERN,
128                                       field.span,
129                                       "You matched a field with a wildcard pattern. Consider using `..` instead");
130                         } else {
131                             span_help_and_lint(cx,
132                                                UNNEEDED_FIELD_PATTERN,
133                                                field.span,
134                                                "You matched a field with a wildcard pattern. Consider using `..` \
135                                                 instead",
136                                                &format!("Try with `{} {{ {}, .. }}`",
137                                                         type_name,
138                                                         normal[..].join(", ")));
139                         }
140                     }
141                 }
142             }
143         }
144     }
145
146     fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
147         let mut registered_names: HashMap<String, Span> = HashMap::new();
148
149         for arg in &decl.inputs {
150             if let PatKind::Ident(_, sp_ident, None) = arg.pat.node {
151                 let arg_name = sp_ident.node.to_string();
152
153                 if arg_name.starts_with('_') {
154                     if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
155                         span_lint(cx,
156                                   DUPLICATE_UNDERSCORE_ARGUMENT,
157                                   *correspondence,
158                                   &format!("`{}` already exists, having another argument having almost the same \
159                                             name makes code comprehension and documentation more difficult",
160                                            arg_name[1..].to_owned()));;
161                     }
162                 } else {
163                     registered_names.insert(arg_name, arg.pat.span);
164                 }
165             }
166         }
167     }
168
169     fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
170         match expr.node {
171             ExprKind::Call(ref paren, _) => {
172                 if let ExprKind::Paren(ref closure) = paren.node {
173                     if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
174                         span_lint_and_then(cx,
175                                            REDUNDANT_CLOSURE_CALL,
176                                            expr.span,
177                                            "Try not to call a closure in the expression where it is declared.",
178                                            |db| {
179                                                if decl.inputs.is_empty() {
180                                                    let hint = snippet(cx, block.span, "..").into_owned();
181                                                    db.span_suggestion(expr.span, "Try doing something like: ", hint);
182                                                }
183                                            });
184                     }
185                 }
186             }
187             ExprKind::Unary(UnOp::Neg, ref inner) => {
188                 if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
189                     span_lint(cx,
190                               DOUBLE_NEG,
191                               expr.span,
192                               "`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
193                 }
194             }
195             ExprKind::Lit(ref lit) => {
196                 if_let_chain! {[
197                     let LitKind::Int(..) = lit.node,
198                     let Some(src) = snippet_opt(cx, lit.span),
199                     src.starts_with("0x")
200                 ], {
201                     let mut seen = (false, false);
202                     for ch in src.chars() {
203                         match ch {
204                             'a' ... 'f' => seen.0 = true,
205                             'A' ... 'F' => seen.1 = true,
206                             'i' | 'u'   => break,   // start of suffix already
207                             _ => ()
208                         }
209                     }
210                     if seen.0 && seen.1 {
211                         span_lint(cx, MIXED_CASE_HEX_LITERALS, lit.span,
212                                   "inconsistent casing in hexadecimal literal");
213                     }
214                 }}
215             }
216             _ => ()
217         }
218     }
219
220     fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
221         for w in block.stmts.windows(2) {
222             if_let_chain! {[
223                 let StmtKind::Local(ref local) = w[0].node,
224                 let Option::Some(ref t) = local.init,
225                 let ExprKind::Closure(_, _, _, _) = t.node,
226                 let PatKind::Ident(_, sp_ident, _) = local.pat.node,
227                 let StmtKind::Semi(ref second) = w[1].node,
228                 let ExprKind::Assign(_, ref call) = second.node,
229                 let ExprKind::Call(ref closure, _) = call.node,
230                 let ExprKind::Path(_, ref path) = closure.node
231             ], {
232                 if sp_ident.node == (&path.segments[0]).identifier {
233                     span_lint(cx, REDUNDANT_CLOSURE_CALL, second.span, "Closure called just once immediately after it was declared");
234                 }
235             }}
236         }
237     }
238 }