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