|
| 1 | +usecrate::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 | +pubMANUAL_IGNORE_CASE_CMP, |
| 37 | + perf, |
| 38 | +"manual case-insensitive ASCII comparison" |
| 39 | +} |
| 40 | + |
| 41 | +declare_lint_pass!(ManualIgnoreCaseCmp => [MANUAL_IGNORE_CASE_CMP]); |
| 42 | + |
| 43 | +enumMatchType<'a,'b>{ |
| 44 | +ToAscii(bool,Ty<'a>), |
| 45 | +Literal(&'bLitKind), |
| 46 | +} |
| 47 | + |
| 48 | +fnget_ascii_type<'a,'b>(cx:&LateContext<'a>,kind: rustc_hir::ExprKind<'b>) -> Option<(Span,MatchType<'a,'b>)>{ |
| 49 | +ifletMethodCall(path, expr, _, _) = kind { |
| 50 | +let is_lower = match path.ident.name.as_str(){ |
| 51 | +"to_ascii_lowercase" => true, |
| 52 | +"to_ascii_uppercase" => false, |
| 53 | + _ => returnNone, |
| 54 | +}; |
| 55 | +let ty_raw = cx.typeck_results().expr_ty(expr); |
| 56 | +let ty = ty_raw.peel_refs(); |
| 57 | +ifneeds_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 | +returnSome((expr.span,ToAscii(is_lower, ty_raw))); |
| 63 | +} |
| 64 | +}elseifletLit(expr) = kind { |
| 65 | +returnSome((expr.span,Literal(&expr.node))); |
| 66 | +} |
| 67 | +None |
| 68 | +} |
| 69 | + |
| 70 | +/// Returns true if the type needs to be dereferenced to be compared |
| 71 | +fnneeds_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 | +implLateLintPass<'_>forManualIgnoreCaseCmp{ |
| 79 | +fncheck_expr(&mutself,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 | +ifletBinary(op, left, right) = &expr.kind |
| 85 | + && (op.node == BinOpKind::Eq || op.node == BinOpKind::Ne) |
| 86 | + && letSome((left_span, left_val)) = get_ascii_type(cx, left.kind) |
| 87 | + && letSome((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)ifneeds_ref_to_cmp(cx, ty) => "&", |
| 96 | +ToAscii(..) => "", |
| 97 | +Literal(ty) => { |
| 98 | +ifletLitKind::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 | +letmut 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