]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/misc_early.rs
Merge remote-tracking branch 'upstream/master' into luckily_literals_love_lints
[rust.git] / clippy_lints / src / misc_early.rs
1 use rustc::lint::*;
2 use std::collections::HashMap;
3 use std::char;
4 use syntax::ast::*;
5 use syntax::codemap::Span;
6 use syntax::visit::FnKind;
7 use utils::{constants, span_lint, span_help_and_lint, snippet, snippet_opt, span_lint_and_then, in_external_macro};
8
9 /// **What it does:** Checks for structure field patterns bound to wildcards.
10 ///
11 /// **Why is this bad?** Using `..` instead is shorter and leaves the focus on
12 /// the fields that are actually bound.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// let { a: _, b: ref b, c: _ } = ..
19 /// ```
20 declare_lint! {
21     pub UNNEEDED_FIELD_PATTERN,
22     Warn,
23     "struct fields bound to a wildcard instead of using `..`"
24 }
25
26 /// **What it does:** Checks for function arguments having the similar names
27 /// differing by an underscore.
28 ///
29 /// **Why is this bad?** It affects code readability.
30 ///
31 /// **Known problems:** None.
32 ///
33 /// **Example:**
34 /// ```rust
35 /// fn foo(a: i32, _a: i32) {}
36 /// ```
37 declare_lint! {
38     pub DUPLICATE_UNDERSCORE_ARGUMENT,
39     Warn,
40     "function arguments having names which only differ by an underscore"
41 }
42
43 /// **What it does:** Detects closures called in the same expression where they are defined.
44 ///
45 /// **Why is this bad?** It is unnecessarily adding to the expression's complexity.
46 ///
47 /// **Known problems:** None.
48 ///
49 /// **Example:**
50 /// ```rust
51 /// (|| 42)()
52 /// ```
53 declare_lint! {
54     pub REDUNDANT_CLOSURE_CALL,
55     Warn,
56     "throwaway closures called in the expression they are defined"
57 }
58
59 /// **What it does:** Detects expressions of the form `--x`.
60 ///
61 /// **Why is this bad?** It can mislead C/C++ programmers to think `x` was
62 /// decremented.
63 ///
64 /// **Known problems:** None.
65 ///
66 /// **Example:**
67 /// ```rust
68 /// --x;
69 /// ```
70 declare_lint! {
71     pub DOUBLE_NEG,
72     Warn,
73     "`--x`, which is a double negation of `x` and not a pre-decrement as in C/C++"
74 }
75
76 /// **What it does:** Warns on hexadecimal literals with mixed-case letter digits.
77 ///
78 /// **Why is this bad?** It looks confusing.
79 ///
80 /// **Known problems:** None.
81 ///
82 /// **Example:**
83 /// ```rust
84 /// let y = 0x1a9BAcD;
85 /// ```
86 declare_lint! {
87     pub MIXED_CASE_HEX_LITERALS,
88     Warn,
89     "hex literals whose letter digits are not consistently upper- or lowercased"
90 }
91
92 /// **What it does:** Warns if literal suffixes are not separated by an underscore.
93 ///
94 /// **Why is this bad?** It is much less readable.
95 ///
96 /// **Known problems:** None.
97 ///
98 /// **Example:**
99 /// ```rust
100 /// let y = 123832i32;
101 /// ```
102 declare_lint! {
103     pub UNSEPARATED_LITERAL_SUFFIX,
104     Allow,
105     "literals whose suffix is not separated by an underscore"
106 }
107
108 /// **What it does:** Warns if an integral constant literal starts with `0`.
109 ///
110 /// **Why is this bad?** In some languages (including the infamous C language and most of its
111 /// family), this marks an octal constant. In Rust however, this is a decimal constant. This could
112 /// be confusing for both the writer and a reader of the constant.
113 ///
114 /// **Known problems:** None.
115 ///
116 /// **Example:**
117 ///
118 /// In Rust:
119 /// ```rust
120 /// fn main() {
121 ///     let a = 0123;
122 ///     println!("{}", a);
123 /// }
124 /// ```
125 ///
126 /// prints `123`, while in C:
127 ///
128 /// ```c
129 /// #include <stdio.h>
130 ///
131 /// int main() {
132 ///     int a = 0123;
133 ///     printf("%d\n", a);
134 /// }
135 /// ```
136 ///
137 /// prints `83` (as `83 == 0o123` while `123 == 0o173`).
138 declare_lint! {
139     pub ZERO_PREFIXED_LITERAL,
140     Warn,
141     "integer literals starting with `0`"
142 }
143
144 /// **What it does:** Warns if a generic shadows a built-in type.
145 ///
146 /// **Why is this bad?** This gives surprising type errors.
147 ///
148 /// **Known problems:** None.
149 ///
150 /// **Example:**
151 ///
152 /// ```rust
153 /// impl<u32> Foo<u32> {
154 ///     fn impl_func(&self) -> u32 {
155 ///         42
156 ///     }
157 /// }
158 /// ```
159 declare_lint! {
160     pub BUILTIN_TYPE_SHADOW,
161     Warn,
162     "shadowing a builtin type"
163 }
164
165 #[derive(Copy, Clone)]
166 pub struct MiscEarly;
167
168 impl LintPass for MiscEarly {
169     fn get_lints(&self) -> LintArray {
170         lint_array!(UNNEEDED_FIELD_PATTERN,
171                     DUPLICATE_UNDERSCORE_ARGUMENT,
172                     REDUNDANT_CLOSURE_CALL,
173                     DOUBLE_NEG,
174                     MIXED_CASE_HEX_LITERALS,
175                     UNSEPARATED_LITERAL_SUFFIX,
176                     ZERO_PREFIXED_LITERAL,
177                     BUILTIN_TYPE_SHADOW)
178     }
179 }
180
181 impl EarlyLintPass for MiscEarly {
182     fn check_generics(&mut self, cx: &EarlyContext, gen: &Generics) {
183         for ty in &gen.ty_params {
184             let name = ty.ident.name.as_str();
185             if constants::BUILTIN_TYPES.contains(&&*name) {
186                 span_lint(cx,
187                           BUILTIN_TYPE_SHADOW,
188                           ty.span,
189                           &format!("This generic shadows the built-in type `{}`", name));
190             }
191         }
192     }
193
194     fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
195         if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
196             let mut wilds = 0;
197             let type_name = npat.segments.last().expect("A path must have at least one segment").identifier.name;
198
199             for field in pfields {
200                 if field.node.pat.node == PatKind::Wild {
201                     wilds += 1;
202                 }
203             }
204             if !pfields.is_empty() && wilds == pfields.len() {
205                 span_help_and_lint(cx,
206                                    UNNEEDED_FIELD_PATTERN,
207                                    pat.span,
208                                    "All the struct fields are matched to a wildcard pattern, consider using `..`.",
209                                    &format!("Try with `{} {{ .. }}` instead", type_name));
210                 return;
211             }
212             if wilds > 0 {
213                 let mut normal = vec![];
214
215                 for field in pfields {
216                     if field.node.pat.node != PatKind::Wild {
217                         if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
218                             normal.push(n);
219                         }
220                     }
221                 }
222                 for field in pfields {
223                     if field.node.pat.node == PatKind::Wild {
224                         wilds -= 1;
225                         if wilds > 0 {
226                             span_lint(cx,
227                                       UNNEEDED_FIELD_PATTERN,
228                                       field.span,
229                                       "You matched a field with a wildcard pattern. Consider using `..` instead");
230                         } else {
231                             span_help_and_lint(cx,
232                                                UNNEEDED_FIELD_PATTERN,
233                                                field.span,
234                                                "You matched a field with a wildcard pattern. Consider using `..` \
235                                                 instead",
236                                                &format!("Try with `{} {{ {}, .. }}`",
237                                                         type_name,
238                                                         normal[..].join(", ")));
239                         }
240                     }
241                 }
242             }
243         }
244     }
245
246     fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: Span, _: NodeId) {
247         let mut registered_names: HashMap<String, Span> = HashMap::new();
248
249         for arg in &decl.inputs {
250             if let PatKind::Ident(_, sp_ident, None) = arg.pat.node {
251                 let arg_name = sp_ident.node.to_string();
252
253                 if arg_name.starts_with('_') {
254                     if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
255                         span_lint(cx,
256                                   DUPLICATE_UNDERSCORE_ARGUMENT,
257                                   *correspondence,
258                                   &format!("`{}` already exists, having another argument having almost the same \
259                                             name makes code comprehension and documentation more difficult",
260                                            arg_name[1..].to_owned()));;
261                     }
262                 } else {
263                     registered_names.insert(arg_name, arg.pat.span);
264                 }
265             }
266         }
267     }
268
269     fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
270         if in_external_macro(cx, expr.span) {
271             return;
272         }
273         match expr.node {
274             ExprKind::Call(ref paren, _) => {
275                 if let ExprKind::Paren(ref closure) = paren.node {
276                     if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
277                         span_lint_and_then(cx,
278                                            REDUNDANT_CLOSURE_CALL,
279                                            expr.span,
280                                            "Try not to call a closure in the expression where it is declared.",
281                                            |db| if decl.inputs.is_empty() {
282                                                let hint = snippet(cx, block.span, "..").into_owned();
283                                                db.span_suggestion(expr.span, "Try doing something like: ", hint);
284                                            });
285                     }
286                 }
287             },
288             ExprKind::Unary(UnOp::Neg, ref inner) => {
289                 if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
290                     span_lint(cx,
291                               DOUBLE_NEG,
292                               expr.span,
293                               "`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
294                 }
295             },
296             ExprKind::Lit(ref lit) => self.check_lit(cx, lit),
297             _ => (),
298         }
299     }
300
301     fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
302         for w in block.stmts.windows(2) {
303             if_let_chain! {[
304                 let StmtKind::Local(ref local) = w[0].node,
305                 let Option::Some(ref t) = local.init,
306                 let ExprKind::Closure(_, _, _, _) = t.node,
307                 let PatKind::Ident(_, sp_ident, _) = local.pat.node,
308                 let StmtKind::Semi(ref second) = w[1].node,
309                 let ExprKind::Assign(_, ref call) = second.node,
310                 let ExprKind::Call(ref closure, _) = call.node,
311                 let ExprKind::Path(_, ref path) = closure.node
312             ], {
313                 if sp_ident.node == (&path.segments[0]).identifier {
314                     span_lint(
315                         cx,
316                         REDUNDANT_CLOSURE_CALL,
317                         second.span,
318                         "Closure called just once immediately after it was declared",
319                     );
320                 }
321             }}
322         }
323     }
324 }
325
326 impl MiscEarly {
327     fn check_lit(&self, cx: &EarlyContext, lit: &Lit) {
328         if_let_chain! {[
329             let LitKind::Int(value, ..) = lit.node,
330             let Some(src) = snippet_opt(cx, lit.span),
331             let Some(firstch) = src.chars().next(),
332             char::to_digit(firstch, 10).is_some()
333         ], {
334             let mut prev = '\0';
335             for ch in src.chars() {
336                 if ch == 'i' || ch == 'u' {
337                     if prev != '_' {
338                         span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
339                                     "integer type suffix should be separated by an underscore");
340                     }
341                     break;
342                 }
343                 prev = ch;
344             }
345             if src.starts_with("0x") {
346                 let mut seen = (false, false);
347                 for ch in src.chars() {
348                     match ch {
349                         'a' ... 'f' => seen.0 = true,
350                         'A' ... 'F' => seen.1 = true,
351                         'i' | 'u'   => break,   // start of suffix already
352                         _ => ()
353                     }
354                 }
355                 if seen.0 && seen.1 {
356                     span_lint(cx, MIXED_CASE_HEX_LITERALS, lit.span,
357                                 "inconsistent casing in hexadecimal literal");
358                 }
359             } else if src.starts_with("0b") || src.starts_with("0o") {
360                 /* nothing to do */
361             } else if value != 0 && src.starts_with('0') {
362                 span_lint_and_then(cx,
363                                     ZERO_PREFIXED_LITERAL,
364                                     lit.span,
365                                     "this is a decimal constant",
366                                     |db| {
367                     db.span_suggestion(
368                         lit.span,
369                         "if you mean to use a decimal constant, remove the `0` to remove confusion",
370                         src.trim_left_matches('0').to_string(),
371                     );
372                     db.span_suggestion(
373                         lit.span,
374                         "if you mean to use an octal constant, use `0o`",
375                         format!("0o{}", src.trim_left_matches('0')),
376                     );
377                 });
378             }
379         }}
380         if_let_chain! {[
381             let LitKind::Float(..) = lit.node,
382             let Some(src) = snippet_opt(cx, lit.span),
383             let Some(firstch) = src.chars().next(),
384             char::to_digit(firstch, 10).is_some()
385         ], {
386             let mut prev = '\0';
387             for ch in src.chars() {
388                 if ch == 'f' {
389                     if prev != '_' {
390                         span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
391                                     "float type suffix should be separated by an underscore");
392                     }
393                     break;
394                 }
395                 prev = ch;
396             }
397         }}
398     }
399 }