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;
9 use rustc_span::symbol::Symbol;
10 use rustc_span::{sym, Span};
12 use super::MATCH_STR_CASE_MISMATCH;
22 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, scrutinee: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>]) {
24 if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(scrutinee).kind();
25 if let ty::Str = ty.kind();
27 let mut visitor = MatchExprVisitor {
32 visitor.visit_expr(scrutinee);
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());
43 struct MatchExprVisitor<'a, 'tcx> {
44 cx: &'a LateContext<'tcx>,
45 case_method: Option<CaseMethod>,
48 impl<'a, 'tcx> Visitor<'tcx> for MatchExprVisitor<'a, 'tcx> {
49 fn visit_expr(&mut self, ex: &'tcx Expr<'_>) {
51 ExprKind::MethodCall(segment, receiver, [], _) if self.case_altered(segment.ident.as_str(), receiver) => {},
52 _ => walk_expr(self, ex),
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();
62 if is_type_diagnostic_item(self.cx, ty, sym::String) || ty.kind() == &ty::Str {
63 self.case_method = Some(case_method);
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),
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()) },
92 if let PatKind::Lit(Expr {
93 kind: ExprKind::Lit(lit),
96 if let LitKind::Str(symbol, _) = lit.node;
97 let input = symbol.as_str();
98 if !case_check(input);
100 return Some((lit.span, symbol));
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()),
118 MATCH_STR_CASE_MISMATCH,
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,