2021-10-21 06:11:36 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2022-07-05 11:56:16 -05:00
|
|
|
use clippy_utils::ty::is_type_lang_item;
|
2021-10-21 06:11:36 -05:00
|
|
|
use rustc_ast::ast::LitKind;
|
|
|
|
use rustc_errors::Applicability;
|
2022-01-15 16:07:52 -06:00
|
|
|
use rustc_hir::intravisit::{walk_expr, Visitor};
|
2022-07-05 11:56:16 -05:00
|
|
|
use rustc_hir::{Arm, Expr, ExprKind, LangItem, PatKind};
|
2022-06-04 06:34:07 -05:00
|
|
|
use rustc_lint::LateContext;
|
2021-10-21 06:11:36 -05:00
|
|
|
use rustc_middle::ty;
|
2021-12-14 15:32:21 -06:00
|
|
|
use rustc_span::symbol::Symbol;
|
2022-07-05 11:56:16 -05:00
|
|
|
use rustc_span::Span;
|
2021-10-21 06:11:36 -05:00
|
|
|
|
2022-06-04 06:34:07 -05:00
|
|
|
use super::MATCH_STR_CASE_MISMATCH;
|
2021-10-21 06:11:36 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum CaseMethod {
|
|
|
|
LowerCase,
|
|
|
|
AsciiLowerCase,
|
|
|
|
UpperCase,
|
|
|
|
AsciiUppercase,
|
|
|
|
}
|
|
|
|
|
2022-06-04 06:34:07 -05:00
|
|
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, scrutinee: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>]) {
|
2023-11-16 12:13:24 -06:00
|
|
|
if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(scrutinee).kind()
|
|
|
|
&& let ty::Str = ty.kind()
|
|
|
|
{
|
|
|
|
let mut visitor = MatchExprVisitor { cx, case_method: None };
|
|
|
|
|
|
|
|
visitor.visit_expr(scrutinee);
|
|
|
|
|
|
|
|
if let Some(case_method) = visitor.case_method {
|
|
|
|
if let Some((bad_case_span, bad_case_sym)) = verify_case(&case_method, arms) {
|
|
|
|
lint(cx, &case_method, bad_case_span, bad_case_sym.as_str());
|
2021-10-21 06:11:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MatchExprVisitor<'a, 'tcx> {
|
|
|
|
cx: &'a LateContext<'tcx>,
|
|
|
|
case_method: Option<CaseMethod>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for MatchExprVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, ex: &'tcx Expr<'_>) {
|
|
|
|
match ex.kind {
|
2022-09-01 04:43:35 -05:00
|
|
|
ExprKind::MethodCall(segment, receiver, [], _) if self.case_altered(segment.ident.as_str(), receiver) => {},
|
2021-10-21 06:11:36 -05:00
|
|
|
_ => walk_expr(self, ex),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> MatchExprVisitor<'a, 'tcx> {
|
|
|
|
fn case_altered(&mut self, segment_ident: &str, receiver: &Expr<'_>) -> bool {
|
|
|
|
if let Some(case_method) = get_case_method(segment_ident) {
|
|
|
|
let ty = self.cx.typeck_results().expr_ty(receiver).peel_refs();
|
|
|
|
|
2022-07-05 11:56:16 -05:00
|
|
|
if is_type_lang_item(self.cx, ty, LangItem::String) || ty.kind() == &ty::Str {
|
2021-10-21 06:11:36 -05:00
|
|
|
self.case_method = Some(case_method);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_case_method(segment_ident_str: &str) -> Option<CaseMethod> {
|
|
|
|
match segment_ident_str {
|
|
|
|
"to_lowercase" => Some(CaseMethod::LowerCase),
|
|
|
|
"to_ascii_lowercase" => Some(CaseMethod::AsciiLowerCase),
|
|
|
|
"to_uppercase" => Some(CaseMethod::UpperCase),
|
|
|
|
"to_ascii_uppercase" => Some(CaseMethod::AsciiUppercase),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 15:32:21 -06:00
|
|
|
fn verify_case<'a>(case_method: &'a CaseMethod, arms: &'a [Arm<'_>]) -> Option<(Span, Symbol)> {
|
2021-10-21 06:11:36 -05:00
|
|
|
let case_check = match case_method {
|
2021-11-04 07:52:36 -05:00
|
|
|
CaseMethod::LowerCase => |input: &str| -> bool { input.chars().all(|c| c.to_lowercase().next() == Some(c)) },
|
|
|
|
CaseMethod::AsciiLowerCase => |input: &str| -> bool { !input.chars().any(|c| c.is_ascii_uppercase()) },
|
|
|
|
CaseMethod::UpperCase => |input: &str| -> bool { input.chars().all(|c| c.to_uppercase().next() == Some(c)) },
|
|
|
|
CaseMethod::AsciiUppercase => |input: &str| -> bool { !input.chars().any(|c| c.is_ascii_lowercase()) },
|
2021-10-21 06:11:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
for arm in arms {
|
2023-11-16 12:13:24 -06:00
|
|
|
if let PatKind::Lit(Expr {
|
|
|
|
kind: ExprKind::Lit(lit),
|
|
|
|
..
|
|
|
|
}) = arm.pat.kind
|
|
|
|
&& let LitKind::Str(symbol, _) = lit.node
|
|
|
|
&& let input = symbol.as_str()
|
|
|
|
&& !case_check(input)
|
|
|
|
{
|
|
|
|
return Some((lit.span, symbol));
|
2021-10-21 06:11:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint(cx: &LateContext<'_>, case_method: &CaseMethod, bad_case_span: Span, bad_case_str: &str) {
|
|
|
|
let (method_str, suggestion) = match case_method {
|
2021-11-04 07:52:36 -05:00
|
|
|
CaseMethod::LowerCase => ("to_lowercase", bad_case_str.to_lowercase()),
|
2021-10-21 06:11:36 -05:00
|
|
|
CaseMethod::AsciiLowerCase => ("to_ascii_lowercase", bad_case_str.to_ascii_lowercase()),
|
|
|
|
CaseMethod::UpperCase => ("to_uppercase", bad_case_str.to_uppercase()),
|
|
|
|
CaseMethod::AsciiUppercase => ("to_ascii_uppercase", bad_case_str.to_ascii_uppercase()),
|
|
|
|
};
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MATCH_STR_CASE_MISMATCH,
|
|
|
|
bad_case_span,
|
|
|
|
"this `match` arm has a differing case than its expression",
|
2024-04-04 12:52:55 -05:00
|
|
|
format!("consider changing the case of this arm to respect `{method_str}`"),
|
2022-10-06 02:44:38 -05:00
|
|
|
format!("\"{suggestion}\""),
|
2021-10-21 06:11:36 -05:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|