]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/misc_early/redundant_pattern.rs
Rollup merge of #99742 - sigaloid:master, r=thomcc
[rust.git] / src / tools / clippy / clippy_lints / src / misc_early / redundant_pattern.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use rustc_ast::ast::{BindingMode, Mutability, Pat, PatKind};
3 use rustc_errors::Applicability;
4 use rustc_lint::EarlyContext;
5
6 use super::REDUNDANT_PATTERN;
7
8 pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
9     if let PatKind::Ident(left, ident, Some(ref right)) = pat.kind {
10         let left_binding = match left {
11             BindingMode::ByRef(Mutability::Mut) => "ref mut ",
12             BindingMode::ByRef(Mutability::Not) => "ref ",
13             BindingMode::ByValue(..) => "",
14         };
15
16         if let PatKind::Wild = right.kind {
17             span_lint_and_sugg(
18                 cx,
19                 REDUNDANT_PATTERN,
20                 pat.span,
21                 &format!(
22                     "the `{} @ _` pattern can be written as just `{}`",
23                     ident.name, ident.name,
24                 ),
25                 "try",
26                 format!("{}{}", left_binding, ident.name),
27                 Applicability::MachineApplicable,
28             );
29         }
30     }
31 }