]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/matches/match_str_case_mismatch.rs
separate the receiver from arguments in HIR under /clippy
[rust.git] / clippy_lints / src / matches / match_str_case_mismatch.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use rustc_ast::ast::LitKind;
4 use rustc_errors::Applicability;
5 use rustc_hir::intravisit::{walk_expr, Visitor};
6 use rustc_hir::{Arm, Expr, ExprKind, PatKind};
7 use rustc_lint::LateContext;
8 use rustc_middle::ty;
9 use rustc_span::symbol::Symbol;
10 use rustc_span::{sym, Span};
11
12 use super::MATCH_STR_CASE_MISMATCH;
13
14 #[derive(Debug)]
15 enum CaseMethod {
16     LowerCase,
17     AsciiLowerCase,
18     UpperCase,
19     AsciiUppercase,
20 }
21
22 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, scrutinee: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>]) {
23     if_chain! {
24         if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(scrutinee).kind();
25         if let ty::Str = ty.kind();
26         then {
27             let mut visitor = MatchExprVisitor {
28                 cx,
29                 case_method: None,
30             };
31
32             visitor.visit_expr(scrutinee);
33
34             if let Some(case_method) = visitor.case_method {
35                 if let Some((bad_case_span, bad_case_sym)) = verify_case(&case_method, arms) {
36                     lint(cx, &case_method, bad_case_span, bad_case_sym.as_str());
37                 }
38             }
39         }
40     }
41 }
42
43 struct MatchExprVisitor<'a, 'tcx> {
44     cx: &'a LateContext<'tcx>,
45     case_method: Option<CaseMethod>,
46 }
47
48 impl<'a, 'tcx> Visitor<'tcx> for MatchExprVisitor<'a, 'tcx> {
49     fn visit_expr(&mut self, ex: &'tcx Expr<'_>) {
50         match ex.kind {
51             ExprKind::MethodCall(segment, receiver, [], _) if self.case_altered(segment.ident.as_str(), receiver) => {},
52             _ => walk_expr(self, ex),
53         }
54     }
55 }
56
57 impl<'a, 'tcx> MatchExprVisitor<'a, 'tcx> {
58     fn case_altered(&mut self, segment_ident: &str, receiver: &Expr<'_>) -> bool {
59         if let Some(case_method) = get_case_method(segment_ident) {
60             let ty = self.cx.typeck_results().expr_ty(receiver).peel_refs();
61
62             if is_type_diagnostic_item(self.cx, ty, sym::String) || ty.kind() == &ty::Str {
63                 self.case_method = Some(case_method);
64                 return true;
65             }
66         }
67
68         false
69     }
70 }
71
72 fn get_case_method(segment_ident_str: &str) -> Option<CaseMethod> {
73     match segment_ident_str {
74         "to_lowercase" => Some(CaseMethod::LowerCase),
75         "to_ascii_lowercase" => Some(CaseMethod::AsciiLowerCase),
76         "to_uppercase" => Some(CaseMethod::UpperCase),
77         "to_ascii_uppercase" => Some(CaseMethod::AsciiUppercase),
78         _ => None,
79     }
80 }
81
82 fn verify_case<'a>(case_method: &'a CaseMethod, arms: &'a [Arm<'_>]) -> Option<(Span, Symbol)> {
83     let case_check = match case_method {
84         CaseMethod::LowerCase => |input: &str| -> bool { input.chars().all(|c| c.to_lowercase().next() == Some(c)) },
85         CaseMethod::AsciiLowerCase => |input: &str| -> bool { !input.chars().any(|c| c.is_ascii_uppercase()) },
86         CaseMethod::UpperCase => |input: &str| -> bool { input.chars().all(|c| c.to_uppercase().next() == Some(c)) },
87         CaseMethod::AsciiUppercase => |input: &str| -> bool { !input.chars().any(|c| c.is_ascii_lowercase()) },
88     };
89
90     for arm in arms {
91         if_chain! {
92             if let PatKind::Lit(Expr {
93                                 kind: ExprKind::Lit(lit),
94                                 ..
95                             }) = arm.pat.kind;
96             if let LitKind::Str(symbol, _) = lit.node;
97             let input = symbol.as_str();
98             if !case_check(input);
99             then {
100                 return Some((lit.span, symbol));
101             }
102         }
103     }
104
105     None
106 }
107
108 fn lint(cx: &LateContext<'_>, case_method: &CaseMethod, bad_case_span: Span, bad_case_str: &str) {
109     let (method_str, suggestion) = match case_method {
110         CaseMethod::LowerCase => ("to_lowercase", bad_case_str.to_lowercase()),
111         CaseMethod::AsciiLowerCase => ("to_ascii_lowercase", bad_case_str.to_ascii_lowercase()),
112         CaseMethod::UpperCase => ("to_uppercase", bad_case_str.to_uppercase()),
113         CaseMethod::AsciiUppercase => ("to_ascii_uppercase", bad_case_str.to_ascii_uppercase()),
114     };
115
116     span_lint_and_sugg(
117         cx,
118         MATCH_STR_CASE_MISMATCH,
119         bad_case_span,
120         "this `match` arm has a differing case than its expression",
121         &format!("consider changing the case of this arm to respect `{}`", method_str),
122         format!("\"{}\"", suggestion),
123         Applicability::MachineApplicable,
124     );
125 }