]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/misc_early.rs
Merge pull request #1146 from birkenfeld/housekeeping
[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::{span_lint, span_help_and_lint, snippet, snippet_opt, span_lint_and_then};
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
109 #[derive(Copy, Clone)]
110 pub struct MiscEarly;
111
112 impl LintPass for MiscEarly {
113     fn get_lints(&self) -> LintArray {
114         lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL,
115                     DOUBLE_NEG, MIXED_CASE_HEX_LITERALS, UNSEPARATED_LITERAL_SUFFIX)
116     }
117 }
118
119 impl EarlyLintPass for MiscEarly {
120     fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
121         if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
122             let mut wilds = 0;
123             let type_name = npat.segments.last().expect("A path must have at least one segment").identifier.name;
124
125             for field in pfields {
126                 if field.node.pat.node == PatKind::Wild {
127                     wilds += 1;
128                 }
129             }
130             if !pfields.is_empty() && wilds == pfields.len() {
131                 span_help_and_lint(cx,
132                                    UNNEEDED_FIELD_PATTERN,
133                                    pat.span,
134                                    "All the struct fields are matched to a wildcard pattern, consider using `..`.",
135                                    &format!("Try with `{} {{ .. }}` instead", type_name));
136                 return;
137             }
138             if wilds > 0 {
139                 let mut normal = vec![];
140
141                 for field in pfields {
142                     if field.node.pat.node != PatKind::Wild {
143                         if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
144                             normal.push(n);
145                         }
146                     }
147                 }
148                 for field in pfields {
149                     if field.node.pat.node == PatKind::Wild {
150                         wilds -= 1;
151                         if wilds > 0 {
152                             span_lint(cx,
153                                       UNNEEDED_FIELD_PATTERN,
154                                       field.span,
155                                       "You matched a field with a wildcard pattern. Consider using `..` instead");
156                         } else {
157                             span_help_and_lint(cx,
158                                                UNNEEDED_FIELD_PATTERN,
159                                                field.span,
160                                                "You matched a field with a wildcard pattern. Consider using `..` \
161                                                 instead",
162                                                &format!("Try with `{} {{ {}, .. }}`",
163                                                         type_name,
164                                                         normal[..].join(", ")));
165                         }
166                     }
167                 }
168             }
169         }
170     }
171
172     fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
173         let mut registered_names: HashMap<String, Span> = HashMap::new();
174
175         for arg in &decl.inputs {
176             if let PatKind::Ident(_, sp_ident, None) = arg.pat.node {
177                 let arg_name = sp_ident.node.to_string();
178
179                 if arg_name.starts_with('_') {
180                     if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
181                         span_lint(cx,
182                                   DUPLICATE_UNDERSCORE_ARGUMENT,
183                                   *correspondence,
184                                   &format!("`{}` already exists, having another argument having almost the same \
185                                             name makes code comprehension and documentation more difficult",
186                                            arg_name[1..].to_owned()));;
187                     }
188                 } else {
189                     registered_names.insert(arg_name, arg.pat.span);
190                 }
191             }
192         }
193     }
194
195     fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
196         match expr.node {
197             ExprKind::Call(ref paren, _) => {
198                 if let ExprKind::Paren(ref closure) = paren.node {
199                     if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
200                         span_lint_and_then(cx,
201                                            REDUNDANT_CLOSURE_CALL,
202                                            expr.span,
203                                            "Try not to call a closure in the expression where it is declared.",
204                                            |db| {
205                                                if decl.inputs.is_empty() {
206                                                    let hint = snippet(cx, block.span, "..").into_owned();
207                                                    db.span_suggestion(expr.span, "Try doing something like: ", hint);
208                                                }
209                                            });
210                     }
211                 }
212             }
213             ExprKind::Unary(UnOp::Neg, ref inner) => {
214                 if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
215                     span_lint(cx,
216                               DOUBLE_NEG,
217                               expr.span,
218                               "`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
219                 }
220             }
221             ExprKind::Lit(ref lit) => {
222                 if_let_chain! {[
223                     let LitKind::Int(..) = lit.node,
224                     let Some(src) = snippet_opt(cx, lit.span),
225                     let Some(firstch) = src.chars().next(),
226                     char::to_digit(firstch, 10).is_some()
227                 ], {
228                     let mut prev = '\0';
229                     for ch in src.chars() {
230                         if ch == 'i' || ch == 'u' {
231                             if prev != '_' {
232                                 span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
233                                           "integer type suffix should be separated by an underscore");
234                             }
235                             break;
236                         }
237                         prev = ch;
238                     }
239                     if src.starts_with("0x") {
240                         let mut seen = (false, false);
241                         for ch in src.chars() {
242                             match ch {
243                                 'a' ... 'f' => seen.0 = true,
244                                 'A' ... 'F' => seen.1 = true,
245                                 'i' | 'u'   => break,   // start of suffix already
246                                 _ => ()
247                             }
248                         }
249                         if seen.0 && seen.1 {
250                             span_lint(cx, MIXED_CASE_HEX_LITERALS, lit.span,
251                                       "inconsistent casing in hexadecimal literal");
252                         }
253                     }
254                 }}
255                 if_let_chain! {[
256                     let LitKind::Float(..) = lit.node,
257                     let Some(src) = snippet_opt(cx, lit.span),
258                     let Some(firstch) = src.chars().next(),
259                     char::to_digit(firstch, 10).is_some()
260                 ], {
261                     let mut prev = '\0';
262                     for ch in src.chars() {
263                         if ch == 'f' {
264                             if prev != '_' {
265                                 span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
266                                           "float type suffix should be separated by an underscore");
267                             }
268                             break;
269                         }
270                         prev = ch;
271                     }
272                 }}
273             }
274             _ => ()
275         }
276     }
277
278     fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
279         for w in block.stmts.windows(2) {
280             if_let_chain! {[
281                 let StmtKind::Local(ref local) = w[0].node,
282                 let Option::Some(ref t) = local.init,
283                 let ExprKind::Closure(_, _, _, _) = t.node,
284                 let PatKind::Ident(_, sp_ident, _) = local.pat.node,
285                 let StmtKind::Semi(ref second) = w[1].node,
286                 let ExprKind::Assign(_, ref call) = second.node,
287                 let ExprKind::Call(ref closure, _) = call.node,
288                 let ExprKind::Path(_, ref path) = closure.node
289             ], {
290                 if sp_ident.node == (&path.segments[0]).identifier {
291                     span_lint(cx, REDUNDANT_CLOSURE_CALL, second.span, "Closure called just once immediately after it was declared");
292                 }
293             }}
294         }
295     }
296 }