|
| 1 | +use crate::manual_ignore_case_cmp::MatchType::{Literal, ToAscii}; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use clippy_utils::source::snippet_with_applicability; |
| 4 | +use clippy_utils::ty::{get_type_diagnostic_name, is_type_diagnostic_item, is_type_lang_item}; |
| 5 | +use rustc_ast::LitKind; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::ExprKind::{Binary, Lit, MethodCall}; |
| 8 | +use rustc_hir::{BinOpKind, Expr, LangItem}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_middle::ty; |
| 11 | +use rustc_middle::ty::{Ty, UintTy}; |
| 12 | +use rustc_session::declare_lint_pass; |
| 13 | +use rustc_span::{Span, sym}; |
| 14 | + |
| 15 | +declare_clippy_lint! { |
| 16 | + /// ### What it does |
| 17 | + /// Checks for manual case-insensitive ASCII comparison. |
| 18 | + /// |
| 19 | + /// ### Why is this bad? |
| 20 | + /// The `eq_ignore_ascii_case` method is faster because it does not allocate |
| 21 | + /// memory for the new strings, and it is more readable. |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```no_run |
| 25 | + /// fn compare(a: &str, b: &str) -> bool { |
| 26 | + /// a.to_ascii_lowercase() == b.to_ascii_lowercase() || a.to_ascii_lowercase() == "abc" |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// Use instead: |
| 30 | + /// ```no_run |
| 31 | + /// fn compare(a: &str, b: &str) -> bool { |
| 32 | + /// a.eq_ignore_ascii_case(b) || a.eq_ignore_ascii_case("abc") |
| 33 | + /// } |
| 34 | + /// ``` |
| 35 | + #[clippy::version = "1.82.0"] |
| 36 | + pub MANUAL_IGNORE_CASE_CMP, |
| 37 | + perf, |
| 38 | + "manual case-insensitive ASCII comparison" |
| 39 | +} |
| 40 | + |
| 41 | +declare_lint_pass!(ManualIgnoreCaseCmp => [MANUAL_IGNORE_CASE_CMP]); |
| 42 | + |
| 43 | +enum MatchType<'a, 'b> { |
| 44 | + ToAscii(bool, Ty<'a>), |
| 45 | + Literal(&'b LitKind), |
| 46 | +} |
| 47 | + |
| 48 | +fn get_ascii_type<'a, 'b>(cx: &LateContext<'a>, kind: rustc_hir::ExprKind<'b>) -> Option<(Span, MatchType<'a, 'b>)> { |
| 49 | + if let MethodCall(path, expr, _, _) = kind { |
| 50 | + let is_lower = match path.ident.name.as_str() { |
| 51 | + "to_ascii_lowercase" => true, |
| 52 | + "to_ascii_uppercase" => false, |
| 53 | + _ => return None, |
| 54 | + }; |
| 55 | + let ty_raw = cx.typeck_results().expr_ty(expr); |
| 56 | + let ty = ty_raw.peel_refs(); |
| 57 | + if needs_ref_to_cmp(cx, ty) |
| 58 | + || ty.is_str() |
| 59 | + || ty.is_slice() |
| 60 | + || matches!(get_type_diagnostic_name(cx, ty), Some(sym::OsStr | sym::OsString)) |
| 61 | + { |
| 62 | + return Some((expr.span, ToAscii(is_lower, ty_raw))); |
| 63 | + } |
| 64 | + } else if let Lit(expr) = kind { |
| 65 | + return Some((expr.span, Literal(&expr.node))); |
| 66 | + } |
| 67 | + None |
| 68 | +} |
| 69 | + |
| 70 | +/// Returns true if the type needs to be dereferenced to be compared |
| 71 | +fn needs_ref_to_cmp(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { |
| 72 | + ty.is_char() |
| 73 | + || *ty.kind() == ty::Uint(UintTy::U8) |
| 74 | + || is_type_diagnostic_item(cx, ty, sym::Vec) |
| 75 | + || is_type_lang_item(cx, ty, LangItem::String) |
| 76 | +} |
| 77 | + |
| 78 | +impl LateLintPass<'_> for ManualIgnoreCaseCmp { |
| 79 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) { |
| 80 | + // check if expression represents a comparison of two strings |
| 81 | + // using .to_ascii_lowercase() or .to_ascii_uppercase() methods, |
| 82 | + // or one of the sides is a literal |
| 83 | + // Offer to replace it with .eq_ignore_ascii_case() method |
| 84 | + if let Binary(op, left, right) = &expr.kind |
| 85 | + && (op.node == BinOpKind::Eq || op.node == BinOpKind::Ne) |
| 86 | + && let Some((left_span, left_val)) = get_ascii_type(cx, left.kind) |
| 87 | + && let Some((right_span, right_val)) = get_ascii_type(cx, right.kind) |
| 88 | + && match (&left_val, &right_val) { |
| 89 | + (ToAscii(l_lower, ..), ToAscii(r_lower, ..)) if l_lower == r_lower => true, |
| 90 | + (ToAscii(..), Literal(..)) | (Literal(..), ToAscii(..)) => true, |
| 91 | + _ => false, |
| 92 | + } |
| 93 | + { |
| 94 | + let deref = match right_val { |
| 95 | + ToAscii(_, ty) if needs_ref_to_cmp(cx, ty) => "&", |
| 96 | + ToAscii(..) => "", |
| 97 | + Literal(ty) => { |
| 98 | + if let LitKind::Char(_) | LitKind::Byte(_) = ty { |
| 99 | + "&" |
| 100 | + } else { |
| 101 | + "" |
| 102 | + } |
| 103 | + }, |
| 104 | + }; |
| 105 | + let neg = if op.node == BinOpKind::Ne { "!" } else { "" }; |
| 106 | + span_lint_and_then( |
| 107 | + cx, |
| 108 | + MANUAL_IGNORE_CASE_CMP, |
| 109 | + expr.span, |
| 110 | + "manual case-insensitive ASCII comparison", |
| 111 | + |diag| { |
| 112 | + let mut app = Applicability::MachineApplicable; |
| 113 | + diag.span_suggestion_verbose( |
| 114 | + expr.span, |
| 115 | + "consider using `.eq_ignore_ascii_case()` instead", |
| 116 | + format!( |
| 117 | + "{neg}{}.eq_ignore_ascii_case({deref}{})", |
| 118 | + snippet_with_applicability(cx, left_span, "_", &mut app), |
| 119 | + snippet_with_applicability(cx, right_span, "_", &mut app) |
| 120 | + ), |
| 121 | + app, |
| 122 | + ); |
| 123 | + }, |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments