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, 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.
13 /// **Example:** `let { a: _, b: ref b, c: _ } = ..`
15 pub UNNEEDED_FIELD_PATTERN, Warn,
16 "Struct fields are bound to a wildcard instead of using `..`"
19 /// **What it does:** This lint checks for function arguments having the similar names differing by an underscore
21 /// **Why is this bad?** It affects code readability
23 /// **Known problems:** None.
25 /// **Example:** `fn foo(a: i32, _a: i32) {}`
27 pub DUPLICATE_UNDERSCORE_ARGUMENT, Warn,
28 "Function arguments having names which only differ by an underscore"
31 /// **What it does:** This lint detects closures called in the same expression where they are defined.
33 /// **Why is this bad?** It is unnecessarily adding to the expression's complexity.
35 /// **Known problems:** None.
37 /// **Example:** `(|| 42)()`
39 pub REDUNDANT_CLOSURE_CALL, Warn,
40 "Closures should not be called in the expression they are defined"
43 /// **What it does:** This lint detects expressions of the form `--x`
45 /// **Why is this bad?** It can mislead C/C++ programmers to think `x` was decremented.
47 /// **Known problems:** None.
49 /// **Example:** `--x;`
52 "`--x` is a double negation of `x` and not a pre-decrement as in C or C++"
56 #[derive(Copy, Clone)]
59 impl LintPass for MiscEarly {
60 fn get_lints(&self) -> LintArray {
61 lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL, DOUBLE_NEG)
65 impl EarlyLintPass for MiscEarly {
66 fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
67 if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
69 let type_name = npat.segments.last().expect("A path must have at least one segment").identifier.name;
71 for field in pfields {
72 if field.node.pat.node == PatKind::Wild {
76 if !pfields.is_empty() && wilds == pfields.len() {
77 span_help_and_lint(cx,
78 UNNEEDED_FIELD_PATTERN,
80 "All the struct fields are matched to a wildcard pattern, consider using `..`.",
81 &format!("Try with `{} {{ .. }}` instead", type_name));
85 let mut normal = vec![];
87 for field in pfields {
88 if field.node.pat.node != PatKind::Wild {
89 if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
94 for field in pfields {
95 if field.node.pat.node == PatKind::Wild {
99 UNNEEDED_FIELD_PATTERN,
101 "You matched a field with a wildcard pattern. Consider using `..` instead");
103 span_help_and_lint(cx,
104 UNNEEDED_FIELD_PATTERN,
106 "You matched a field with a wildcard pattern. Consider using `..` \
108 &format!("Try with `{} {{ {}, .. }}`",
110 normal[..].join(", ")));
118 fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
119 let mut registered_names: HashMap<String, Span> = HashMap::new();
121 for ref arg in &decl.inputs {
122 if let PatKind::Ident(_, sp_ident, None) = arg.pat.node {
123 let arg_name = sp_ident.node.to_string();
125 if arg_name.starts_with('_') {
126 if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
128 DUPLICATE_UNDERSCORE_ARGUMENT,
130 &format!("`{}` already exists, having another argument having almost the same \
131 name makes code comprehension and documentation more difficult",
132 arg_name[1..].to_owned()));;
135 registered_names.insert(arg_name, arg.pat.span);
141 fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
143 ExprKind::Call(ref paren, _) => {
144 if let ExprKind::Paren(ref closure) = paren.node {
145 if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
146 span_lint_and_then(cx,
147 REDUNDANT_CLOSURE_CALL,
149 "Try not to call a closure in the expression where it is declared.",
151 if decl.inputs.is_empty() {
152 let hint = snippet(cx, block.span, "..").into_owned();
153 db.span_suggestion(expr.span, "Try doing something like: ", hint);
159 ExprKind::Unary(UnOp::Neg, ref inner) => {
160 if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
164 "`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
171 fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
172 for w in block.stmts.windows(2) {
174 let StmtKind::Local(ref local) = w[0].node,
175 let Option::Some(ref t) = local.init,
176 let ExprKind::Closure(_, _, _, _) = t.node,
177 let PatKind::Ident(_, sp_ident, _) = local.pat.node,
178 let StmtKind::Semi(ref second) = w[1].node,
179 let ExprKind::Assign(_, ref call) = second.node,
180 let ExprKind::Call(ref closure, _) = call.node,
181 let ExprKind::Path(_, ref path) = closure.node
183 if sp_ident.node == (&path.segments[0]).identifier {
184 span_lint(cx, REDUNDANT_CLOSURE_CALL, second.span, "Closure called just once immediately after it was declared");