2 use std::collections::HashMap;
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.
9 /// **Why is this bad?** Using `..` instead is shorter and leaves the focus on the fields that are actually bound.
11 /// **Known problems:** None.
15 /// let { a: _, b: ref b, c: _ } = ..
18 pub UNNEEDED_FIELD_PATTERN, Warn,
19 "Struct fields are bound to a wildcard instead of using `..`"
22 /// **What it does:** This lint checks for function arguments having the similar names differing by an underscore
24 /// **Why is this bad?** It affects code readability
26 /// **Known problems:** None.
30 /// fn foo(a: i32, _a: i32) {}
33 pub DUPLICATE_UNDERSCORE_ARGUMENT, Warn,
34 "Function arguments having names which only differ by an underscore"
37 /// **What it does:** This lint detects closures called in the same expression where they are defined.
39 /// **Why is this bad?** It is unnecessarily adding to the expression's complexity.
41 /// **Known problems:** None.
48 pub REDUNDANT_CLOSURE_CALL, Warn,
49 "Closures should not be called in the expression they are defined"
52 /// **What it does:** This lint detects expressions of the form `--x`
54 /// **Why is this bad?** It can mislead C/C++ programmers to think `x` was decremented.
56 /// **Known problems:** None.
64 "`--x` is a double negation of `x` and not a pre-decrement as in C or C++"
67 /// **What it does:** Warns on hexadecimal literals with mixed-case letter digits.
69 /// **Why is this bad?** It looks confusing.
71 /// **Known problems:** None.
75 /// let y = 0x1a9BAcD;
78 pub MIXED_CASE_HEX_LITERALS, Warn,
79 "letter digits in hex literals should be either completely upper- or lowercased"
83 #[derive(Copy, Clone)]
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)
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 {
97 let type_name = npat.segments.last().expect("A path must have at least one segment").identifier.name;
99 for field in pfields {
100 if field.node.pat.node == PatKind::Wild {
104 if !pfields.is_empty() && wilds == pfields.len() {
105 span_help_and_lint(cx,
106 UNNEEDED_FIELD_PATTERN,
108 "All the struct fields are matched to a wildcard pattern, consider using `..`.",
109 &format!("Try with `{} {{ .. }}` instead", type_name));
113 let mut normal = vec![];
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) {
122 for field in pfields {
123 if field.node.pat.node == PatKind::Wild {
127 UNNEEDED_FIELD_PATTERN,
129 "You matched a field with a wildcard pattern. Consider using `..` instead");
131 span_help_and_lint(cx,
132 UNNEEDED_FIELD_PATTERN,
134 "You matched a field with a wildcard pattern. Consider using `..` \
136 &format!("Try with `{} {{ {}, .. }}`",
138 normal[..].join(", ")));
146 fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
147 let mut registered_names: HashMap<String, Span> = HashMap::new();
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();
153 if arg_name.starts_with('_') {
154 if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
156 DUPLICATE_UNDERSCORE_ARGUMENT,
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()));;
163 registered_names.insert(arg_name, arg.pat.span);
169 fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
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,
177 "Try not to call a closure in the expression where it is declared.",
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);
187 ExprKind::Unary(UnOp::Neg, ref inner) => {
188 if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
192 "`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
195 ExprKind::Lit(ref lit) => {
197 let LitKind::Int(..) = lit.node,
198 let Some(src) = snippet_opt(cx, lit.span),
199 src.starts_with("0x")
201 let mut seen = (false, false);
202 for ch in src.chars() {
204 'a' ... 'f' => seen.0 = true,
205 'A' ... 'F' => seen.1 = true,
206 'i' | 'u' => break, // start of suffix already
210 if seen.0 && seen.1 {
211 span_lint(cx, MIXED_CASE_HEX_LITERALS, lit.span,
212 "inconsistent casing in hexadecimal literal");
220 fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
221 for w in block.stmts.windows(2) {
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
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");