2 constants, snippet_opt, snippet_with_applicability, span_lint, span_lint_and_help, span_lint_and_sugg,
5 use if_chain::if_chain;
7 BindingMode, Block, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, Mutability,
8 NodeId, Pat, PatKind, StmtKind, UnOp,
10 use rustc_ast::visit::{walk_expr, FnKind, Visitor};
11 use rustc_data_structures::fx::FxHashMap;
12 use rustc_errors::Applicability;
13 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
14 use rustc_middle::lint::in_external_macro;
15 use rustc_session::{declare_lint_pass, declare_tool_lint};
16 use rustc_span::source_map::Span;
18 declare_clippy_lint! {
19 /// **What it does:** Checks for structure field patterns bound to wildcards.
21 /// **Why is this bad?** Using `..` instead is shorter and leaves the focus on
22 /// the fields that are actually bound.
24 /// **Known problems:** None.
33 /// let f = Foo { a: 0, b: 0, c: 0 };
37 /// Foo { a: _, b: 0, .. } => {},
38 /// Foo { a: _, b: _, c: _ } => {},
43 /// Foo { b: 0, .. } => {},
47 pub UNNEEDED_FIELD_PATTERN,
49 "struct fields bound to a wildcard instead of using `..`"
52 declare_clippy_lint! {
53 /// **What it does:** Checks for function arguments having the similar names
54 /// differing by an underscore.
56 /// **Why is this bad?** It affects code readability.
58 /// **Known problems:** None.
62 /// fn foo(a: i32, _a: i32) {}
64 pub DUPLICATE_UNDERSCORE_ARGUMENT,
66 "function arguments having names which only differ by an underscore"
69 declare_clippy_lint! {
70 /// **What it does:** Detects closures called in the same expression where they
73 /// **Why is this bad?** It is unnecessarily adding to the expression's
76 /// **Known problems:** None.
82 pub REDUNDANT_CLOSURE_CALL,
84 "throwaway closures called in the expression they are defined"
87 declare_clippy_lint! {
88 /// **What it does:** Detects expressions of the form `--x`.
90 /// **Why is this bad?** It can mislead C/C++ programmers to think `x` was
93 /// **Known problems:** None.
102 "`--x`, which is a double negation of `x` and not a pre-decrement as in C/C++"
105 declare_clippy_lint! {
106 /// **What it does:** Warns on hexadecimal literals with mixed-case letter
109 /// **Why is this bad?** It looks confusing.
111 /// **Known problems:** None.
115 /// let y = 0x1a9BAcD;
117 pub MIXED_CASE_HEX_LITERALS,
119 "hex literals whose letter digits are not consistently upper- or lowercased"
122 declare_clippy_lint! {
123 /// **What it does:** Warns if literal suffixes are not separated by an
126 /// **Why is this bad?** It is much less readable.
128 /// **Known problems:** None.
132 /// let y = 123832i32;
134 pub UNSEPARATED_LITERAL_SUFFIX,
136 "literals whose suffix is not separated by an underscore"
139 declare_clippy_lint! {
140 /// **What it does:** Warns if an integral constant literal starts with `0`.
142 /// **Why is this bad?** In some languages (including the infamous C language
144 /// family), this marks an octal constant. In Rust however, this is a decimal
145 /// constant. This could
146 /// be confusing for both the writer and a reader of the constant.
148 /// **Known problems:** None.
156 /// println!("{}", a);
160 /// prints `123`, while in C:
163 /// #include <stdio.h>
167 /// printf("%d\n", a);
171 /// prints `83` (as `83 == 0o123` while `123 == 0o173`).
172 pub ZERO_PREFIXED_LITERAL,
174 "integer literals starting with `0`"
177 declare_clippy_lint! {
178 /// **What it does:** Warns if a generic shadows a built-in type.
180 /// **Why is this bad?** This gives surprising type errors.
182 /// **Known problems:** None.
187 /// impl<u32> Foo<u32> {
188 /// fn impl_func(&self) -> u32 {
193 pub BUILTIN_TYPE_SHADOW,
195 "shadowing a builtin type"
198 declare_clippy_lint! {
199 /// **What it does:** Checks for patterns in the form `name @ _`.
201 /// **Why is this bad?** It's almost always more readable to just use direct
204 /// **Known problems:** None.
208 /// # let v = Some("abc");
212 /// y @ _ => (), // easier written as `y`,
215 pub REDUNDANT_PATTERN,
217 "using `name @ _` in a pattern"
220 declare_clippy_lint! {
221 /// **What it does:** Checks for tuple patterns with a wildcard
222 /// pattern (`_`) is next to a rest pattern (`..`).
224 /// _NOTE_: While `_, ..` means there is at least one element left, `..`
225 /// means there are 0 or more elements left. This can make a difference
226 /// when refactoring, but shouldn't result in errors in the refactored code,
227 /// since the wildcard pattern isn't used anyway.
228 /// **Why is this bad?** The wildcard pattern is unneeded as the rest pattern
229 /// can match that element as well.
231 /// **Known problems:** None.
235 /// # struct TupleStruct(u32, u32, u32);
236 /// # let t = TupleStruct(1, 2, 3);
239 /// TupleStruct(0, .., _) => (),
243 /// can be written as
245 /// # struct TupleStruct(u32, u32, u32);
246 /// # let t = TupleStruct(1, 2, 3);
249 /// TupleStruct(0, ..) => (),
253 pub UNNEEDED_WILDCARD_PATTERN,
255 "tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`)"
258 declare_lint_pass!(MiscEarlyLints => [
259 UNNEEDED_FIELD_PATTERN,
260 DUPLICATE_UNDERSCORE_ARGUMENT,
261 REDUNDANT_CLOSURE_CALL,
263 MIXED_CASE_HEX_LITERALS,
264 UNSEPARATED_LITERAL_SUFFIX,
265 ZERO_PREFIXED_LITERAL,
268 UNNEEDED_WILDCARD_PATTERN,
271 // Used to find `return` statements or equivalents e.g., `?`
272 struct ReturnVisitor {
279 Self { found_return: false }
283 impl<'ast> Visitor<'ast> for ReturnVisitor {
284 fn visit_expr(&mut self, ex: &'ast Expr) {
285 if let ExprKind::Ret(_) = ex.kind {
286 self.found_return = true;
287 } else if let ExprKind::Try(_) = ex.kind {
288 self.found_return = true;
295 impl EarlyLintPass for MiscEarlyLints {
296 fn check_generics(&mut self, cx: &EarlyContext<'_>, gen: &Generics) {
297 for param in &gen.params {
298 if let GenericParamKind::Type { .. } = param.kind {
299 let name = param.ident.as_str();
300 if constants::BUILTIN_TYPES.contains(&&*name) {
305 &format!("This generic shadows the built-in type `{}`", name),
312 fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
313 if let PatKind::Struct(ref npat, ref pfields, _) = pat.kind {
318 .expect("A path must have at least one segment")
322 for field in pfields {
323 if let PatKind::Wild = field.pat.kind {
327 if !pfields.is_empty() && wilds == pfields.len() {
330 UNNEEDED_FIELD_PATTERN,
332 "All the struct fields are matched to a wildcard pattern, consider using `..`.",
334 &format!("Try with `{} {{ .. }}` instead", type_name),
339 for field in pfields {
340 if let PatKind::Wild = field.pat.kind {
345 UNNEEDED_FIELD_PATTERN,
347 "You matched a field with a wildcard pattern. Consider using `..` instead",
350 let mut normal = vec![];
352 for field in pfields {
353 match field.pat.kind {
356 if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) {
365 UNNEEDED_FIELD_PATTERN,
367 "You matched a field with a wildcard pattern. Consider using `..` \
370 &format!("Try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")),
378 if let PatKind::Ident(left, ident, Some(ref right)) = pat.kind {
379 let left_binding = match left {
380 BindingMode::ByRef(Mutability::Mut) => "ref mut ",
381 BindingMode::ByRef(Mutability::Not) => "ref ",
382 BindingMode::ByValue(..) => "",
385 if let PatKind::Wild = right.kind {
391 "the `{} @ _` pattern can be written as just `{}`",
392 ident.name, ident.name,
395 format!("{}{}", left_binding, ident.name),
396 Applicability::MachineApplicable,
401 check_unneeded_wildcard_pattern(cx, pat);
404 fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: NodeId) {
405 let mut registered_names: FxHashMap<String, Span> = FxHashMap::default();
407 for arg in &fn_kind.decl().inputs {
408 if let PatKind::Ident(_, ident, None) = arg.pat.kind {
409 let arg_name = ident.to_string();
411 if arg_name.starts_with('_') {
412 if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
415 DUPLICATE_UNDERSCORE_ARGUMENT,
418 "`{}` already exists, having another argument having almost the same \
419 name makes code comprehension and documentation more difficult",
420 arg_name[1..].to_owned()
425 registered_names.insert(arg_name, arg.pat.span);
431 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
432 if in_external_macro(cx.sess(), expr.span) {
436 ExprKind::Call(ref paren, _) => {
437 if let ExprKind::Paren(ref closure) = paren.kind {
438 if let ExprKind::Closure(_, _, _, ref decl, ref block, _) = closure.kind {
439 let mut visitor = ReturnVisitor::new();
440 visitor.visit_expr(block);
441 if !visitor.found_return {
444 REDUNDANT_CLOSURE_CALL,
446 "Try not to call a closure in the expression where it is declared.",
448 if decl.inputs.is_empty() {
449 let mut app = Applicability::MachineApplicable;
451 snippet_with_applicability(cx, block.span, "..", &mut app).into_owned();
452 diag.span_suggestion(expr.span, "Try doing something like: ", hint, app);
460 ExprKind::Unary(UnOp::Neg, ref inner) => {
461 if let ExprKind::Unary(UnOp::Neg, _) = inner.kind {
466 "`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op",
470 ExprKind::Lit(ref lit) => Self::check_lit(cx, lit),
475 fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
476 for w in block.stmts.windows(2) {
478 if let StmtKind::Local(ref local) = w[0].kind;
479 if let Option::Some(ref t) = local.init;
480 if let ExprKind::Closure(..) = t.kind;
481 if let PatKind::Ident(_, ident, _) = local.pat.kind;
482 if let StmtKind::Semi(ref second) = w[1].kind;
483 if let ExprKind::Assign(_, ref call, _) = second.kind;
484 if let ExprKind::Call(ref closure, _) = call.kind;
485 if let ExprKind::Path(_, ref path) = closure.kind;
487 if ident == path.segments[0].ident {
490 REDUNDANT_CLOSURE_CALL,
492 "Closure called just once immediately after it was declared",
501 impl MiscEarlyLints {
502 fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
503 // We test if first character in snippet is a number, because the snippet could be an expansion
504 // from a built-in macro like `line!()` or a proc-macro like `#[wasm_bindgen]`.
505 // Note that this check also covers special case that `line!()` is eagerly expanded by compiler.
506 // See <https://github.com/rust-lang/rust-clippy/issues/4507> for a regression.
507 // FIXME: Find a better way to detect those cases.
508 let lit_snip = match snippet_opt(cx, lit.span) {
509 Some(snip) if snip.chars().next().map_or(false, |c| c.is_digit(10)) => snip,
513 if let LitKind::Int(value, lit_int_type) = lit.kind {
514 let suffix = match lit_int_type {
515 LitIntType::Signed(ty) => ty.name_str(),
516 LitIntType::Unsigned(ty) => ty.name_str(),
517 LitIntType::Unsuffixed => "",
520 let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
523 return; // It's useless so shouldn't lint.
525 // Do not lint when literal is unsuffixed.
526 if !suffix.is_empty() && lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
529 UNSEPARATED_LITERAL_SUFFIX,
531 "integer type suffix should be separated by an underscore",
533 format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix),
534 Applicability::MachineApplicable,
538 if lit_snip.starts_with("0x") {
539 if maybe_last_sep_idx <= 2 {
540 // It's meaningless or causes range error.
543 let mut seen = (false, false);
544 for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() {
546 b'a'..=b'f' => seen.0 = true,
547 b'A'..=b'F' => seen.1 = true,
550 if seen.0 && seen.1 {
553 MIXED_CASE_HEX_LITERALS,
555 "inconsistent casing in hexadecimal literal",
560 } else if lit_snip.starts_with("0b") || lit_snip.starts_with("0o") {
562 } else if value != 0 && lit_snip.starts_with('0') {
565 ZERO_PREFIXED_LITERAL,
567 "this is a decimal constant",
569 diag.span_suggestion(
571 "if you mean to use a decimal constant, remove the `0` to avoid confusion",
572 lit_snip.trim_start_matches(|c| c == '_' || c == '0').to_string(),
573 Applicability::MaybeIncorrect,
575 diag.span_suggestion(
577 "if you mean to use an octal constant, use `0o`",
578 format!("0o{}", lit_snip.trim_start_matches(|c| c == '_' || c == '0')),
579 Applicability::MaybeIncorrect,
584 } else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind {
585 let suffix = float_ty.name_str();
586 let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
589 return; // It's useless so shouldn't lint.
591 if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
594 UNSEPARATED_LITERAL_SUFFIX,
596 "float type suffix should be separated by an underscore",
598 format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix),
599 Applicability::MachineApplicable,
606 fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) {
607 if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.kind {
608 fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
611 UNNEEDED_WILDCARD_PATTERN,
614 "this pattern is unneeded as the `..` pattern can match that element"
616 "these patterns are unneeded as the `..` pattern can match those elements"
618 if only_one { "remove it" } else { "remove them" },
620 Applicability::MachineApplicable,
624 #[allow(clippy::trivially_copy_pass_by_ref)]
625 fn is_wild<P: std::ops::Deref<Target = Pat>>(pat: &&P) -> bool {
626 if let PatKind::Wild = pat.kind {
633 if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) {
634 if let Some((left_index, left_pat)) = patterns[..rest_index]
641 span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
644 if let Some((right_index, right_pat)) =
645 patterns[rest_index + 1..].iter().take_while(is_wild).enumerate().last()
649 patterns[rest_index].span.shrink_to_hi().to(right_pat.span),