2 use std::collections::HashMap;
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.
10 /// **Why is this bad?** Using `..` instead is shorter and leaves the focus on the fields that are actually bound.
12 /// **Known problems:** None.
16 /// let { a: _, b: ref b, c: _ } = ..
19 pub UNNEEDED_FIELD_PATTERN, Warn,
20 "Struct fields are bound to a wildcard instead of using `..`"
23 /// **What it does:** This lint checks for function arguments having the similar names differing by an underscore
25 /// **Why is this bad?** It affects code readability
27 /// **Known problems:** None.
31 /// fn foo(a: i32, _a: i32) {}
34 pub DUPLICATE_UNDERSCORE_ARGUMENT, Warn,
35 "Function arguments having names which only differ by an underscore"
38 /// **What it does:** This lint detects closures called in the same expression where they are defined.
40 /// **Why is this bad?** It is unnecessarily adding to the expression's complexity.
42 /// **Known problems:** None.
49 pub REDUNDANT_CLOSURE_CALL, Warn,
50 "Closures should not be called in the expression they are defined"
53 /// **What it does:** This lint detects expressions of the form `--x`
55 /// **Why is this bad?** It can mislead C/C++ programmers to think `x` was decremented.
57 /// **Known problems:** None.
65 "`--x` is a double negation of `x` and not a pre-decrement as in C or C++"
68 /// **What it does:** Warns on hexadecimal literals with mixed-case letter digits.
70 /// **Why is this bad?** It looks confusing.
72 /// **Known problems:** None.
76 /// let y = 0x1a9BAcD;
79 pub MIXED_CASE_HEX_LITERALS, Warn,
80 "letter digits in hex literals should be either completely upper- or lowercased"
83 /// **What it does:** Warns if literal suffixes are not separated by an underscore.
85 /// **Why is this bad?** It is much less readable.
87 /// **Known problems:** None.
91 /// let y = 123832i32;
94 pub UNSEPARATED_LITERAL_SUFFIX, Allow,
95 "literal suffixes should be separated with an underscore"
99 #[derive(Copy, Clone)]
100 pub struct MiscEarly;
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)
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 {
113 let type_name = npat.segments.last().expect("A path must have at least one segment").identifier.name;
115 for field in pfields {
116 if field.node.pat.node == PatKind::Wild {
120 if !pfields.is_empty() && wilds == pfields.len() {
121 span_help_and_lint(cx,
122 UNNEEDED_FIELD_PATTERN,
124 "All the struct fields are matched to a wildcard pattern, consider using `..`.",
125 &format!("Try with `{} {{ .. }}` instead", type_name));
129 let mut normal = vec![];
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) {
138 for field in pfields {
139 if field.node.pat.node == PatKind::Wild {
143 UNNEEDED_FIELD_PATTERN,
145 "You matched a field with a wildcard pattern. Consider using `..` instead");
147 span_help_and_lint(cx,
148 UNNEEDED_FIELD_PATTERN,
150 "You matched a field with a wildcard pattern. Consider using `..` \
152 &format!("Try with `{} {{ {}, .. }}`",
154 normal[..].join(", ")));
162 fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: &Block, _: Span, _: NodeId) {
163 let mut registered_names: HashMap<String, Span> = HashMap::new();
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();
169 if arg_name.starts_with('_') {
170 if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
172 DUPLICATE_UNDERSCORE_ARGUMENT,
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()));;
179 registered_names.insert(arg_name, arg.pat.span);
185 fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
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,
193 "Try not to call a closure in the expression where it is declared.",
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);
203 ExprKind::Unary(UnOp::Neg, ref inner) => {
204 if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
208 "`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
211 ExprKind::Lit(ref lit) => {
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()
219 for ch in src.chars() {
220 if ch == 'i' || ch == 'u' {
222 span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
223 "integer type suffix should be separated by an underscore");
229 if src.starts_with("0x") {
230 let mut seen = (false, false);
231 for ch in src.chars() {
233 'a' ... 'f' => seen.0 = true,
234 'A' ... 'F' => seen.1 = true,
235 'i' | 'u' => break, // start of suffix already
239 if seen.0 && seen.1 {
240 span_lint(cx, MIXED_CASE_HEX_LITERALS, lit.span,
241 "inconsistent casing in hexadecimal literal");
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()
252 for ch in src.chars() {
255 span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
256 "float type suffix should be separated by an underscore");
268 fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
269 for w in block.stmts.windows(2) {
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
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");