Skip to content

Commit edcbb2e

Browse files
committed
Add manual_ignore_cast_cmp lint
1 parent 2e5b680 commit edcbb2e

File tree

7 files changed

+446
-0
lines changed

7 files changed

+446
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5621,6 +5621,7 @@ Released 2018-09-13
56215621
[`manual_find_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_find_map
56225622
[`manual_flatten`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten
56235623
[`manual_hash_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_hash_one
5624+
[`manual_ignore_case_cmp`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_ignore_case_cmp
56245625
[`manual_inspect`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_inspect
56255626
[`manual_instant_elapsed`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_instant_elapsed
56265627
[`manual_is_ascii_check`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_is_ascii_check

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
305305
crate::manual_float_methods::MANUAL_IS_FINITE_INFO,
306306
crate::manual_float_methods::MANUAL_IS_INFINITE_INFO,
307307
crate::manual_hash_one::MANUAL_HASH_ONE_INFO,
308+
crate::manual_ignore_case_cmp::MANUAL_IGNORE_CASE_CMP_INFO,
308309
crate::manual_is_ascii_check::MANUAL_IS_ASCII_CHECK_INFO,
309310
crate::manual_is_power_of_two::MANUAL_IS_POWER_OF_TWO_INFO,
310311
crate::manual_let_else::MANUAL_LET_ELSE_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ mod manual_clamp;
206206
mod manual_div_ceil;
207207
mod manual_float_methods;
208208
mod manual_hash_one;
209+
mod manual_ignore_case_cmp;
209210
mod manual_is_ascii_check;
210211
mod manual_is_power_of_two;
211212
mod manual_let_else;
@@ -942,5 +943,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
942943
store.register_late_pass(move |_| Box::new(manual_div_ceil::ManualDivCeil::new(conf)));
943944
store.register_late_pass(|_| Box::new(manual_is_power_of_two::ManualIsPowerOfTwo));
944945
store.register_late_pass(|_| Box::new(non_zero_suggestions::NonZeroSuggestions));
946+
store.register_late_pass(|_| Box::new(manual_ignore_case_cmp::ManualIgnoreCaseCmp));
945947
// add lints here, do not remove this comment, it's used in `new_lint`
946948
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet;
3+
use clippy_utils::ty::{get_type_diagnostic_name, is_type_diagnostic_item, is_type_lang_item};
4+
use rustc_errors::Applicability;
5+
use rustc_hir::ExprKind::{Binary, MethodCall};
6+
use rustc_hir::{BinOpKind, Expr, LangItem};
7+
use rustc_lint::{LateContext, LateLintPass};
8+
use rustc_middle::ty;
9+
use rustc_middle::ty::{Ty, UintTy};
10+
use rustc_session::declare_lint_pass;
11+
use rustc_span::sym;
12+
13+
declare_clippy_lint! {
14+
/// ### What it does
15+
/// Checks for manual case-insensitive ASCII comparison.
16+
///
17+
/// ### Why is this bad?
18+
/// The `eq_ignore_ascii_case` method is faster because it does not allocate
19+
/// memory for the new strings, and it is more readable.
20+
///
21+
/// ### Example
22+
/// ```no_run
23+
/// fn compare(a: &str, b: &str) -> bool {
24+
/// a.to_ascii_lowercase() == b.to_ascii_lowercase() || a.to_ascii_lowercase() == "abc"
25+
/// }
26+
/// ```
27+
/// Use instead:
28+
/// ```no_run
29+
/// fn compare(a: &str, b: &str) -> bool {
30+
/// a.eq_ignore_ascii_case(b) || a.eq_ignore_ascii_case("abc")
31+
/// }
32+
/// ```
33+
#[clippy::version = "1.82.0"]
34+
pub MANUAL_IGNORE_CASE_CMP,
35+
perf,
36+
"manual case-insensitive ASCII comparison"
37+
}
38+
39+
declare_lint_pass!(ManualIgnoreCaseCmp => [MANUAL_IGNORE_CASE_CMP]);
40+
41+
fn get_ascii_type<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<Ty<'tcx>> {
42+
let ty_raw = cx.typeck_results().expr_ty(expr);
43+
let ty = ty_raw.peel_refs();
44+
if needs_ref_to_cmp(cx, ty)
45+
|| ty.is_str()
46+
|| ty.is_slice()
47+
|| matches!(get_type_diagnostic_name(cx, ty), Some(sym::OsStr | sym::OsString))
48+
{
49+
Some(ty_raw)
50+
} else {
51+
None
52+
}
53+
}
54+
55+
/// Returns true if the type needs to be dereferenced to be compared
56+
fn needs_ref_to_cmp(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
57+
ty.is_char()
58+
|| *ty.kind() == ty::Uint(UintTy::U8)
59+
|| is_type_diagnostic_item(cx, ty, sym::Vec)
60+
|| is_type_lang_item(cx, ty, LangItem::String)
61+
}
62+
63+
impl LateLintPass<'_> for ManualIgnoreCaseCmp {
64+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
65+
// check if expression represents a comparison of two strings
66+
// using .to_ascii_lowercase() or .to_ascii_uppercase() methods
67+
// Offer to replace it with .eq_ignore_ascii_case() method
68+
if let Binary(op, left, right) = &expr.kind
69+
&& (op.node == BinOpKind::Eq || op.node == BinOpKind::Ne)
70+
&& let MethodCall(left_path, left_val, _, _) = left.kind
71+
&& let MethodCall(right_path, right_val, _, _) = right.kind
72+
&& left_path.ident == right_path.ident
73+
&& matches!(
74+
left_path.ident.name.as_str(),
75+
"to_ascii_lowercase" | "to_ascii_uppercase"
76+
)
77+
&& get_ascii_type(cx, left_val).is_some()
78+
&& let Some(rtype) = get_ascii_type(cx, right_val)
79+
{
80+
// FIXME: there must be a better way to add dereference operator
81+
let deref = if needs_ref_to_cmp(cx, rtype) { "&" } else { "" };
82+
let neg = if op.node == BinOpKind::Ne { "!" } else { "" };
83+
span_lint_and_sugg(
84+
cx,
85+
MANUAL_IGNORE_CASE_CMP,
86+
expr.span,
87+
"manual case-insensitive ASCII comparison",
88+
"consider using `.eq_ignore_ascii_case()` instead",
89+
format!(
90+
"{neg}{}.eq_ignore_ascii_case({deref}{})",
91+
snippet(cx, left_val.span, "_"),
92+
snippet(cx, right_val.span, "_")
93+
),
94+
Applicability::MachineApplicable,
95+
);
96+
}
97+
}
98+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#![allow(clippy::all)]
2+
#![deny(clippy::manual_ignore_case_cmp)]
3+
4+
use std::ffi::{OsStr, OsString};
5+
6+
fn main() {}
7+
8+
fn variants(a: &str, b: &str) {
9+
if a.eq_ignore_ascii_case(b) {
10+
return;
11+
}
12+
if a.eq_ignore_ascii_case(b) {
13+
return;
14+
}
15+
let r = a.eq_ignore_ascii_case(b);
16+
let r = r || a.eq_ignore_ascii_case(b);
17+
r && a.eq_ignore_ascii_case(&b.to_uppercase());
18+
// !=
19+
if !a.eq_ignore_ascii_case(b) {
20+
return;
21+
}
22+
if !a.eq_ignore_ascii_case(b) {
23+
return;
24+
}
25+
let r = !a.eq_ignore_ascii_case(b);
26+
let r = r || !a.eq_ignore_ascii_case(b);
27+
r && !a.eq_ignore_ascii_case(&b.to_uppercase());
28+
}
29+
30+
fn unsupported(a: char, b: char) {
31+
// TODO:: these are rare, and might not be worth supporting
32+
a.to_ascii_lowercase() == char::to_ascii_lowercase(&b);
33+
char::to_ascii_lowercase(&a) == b.to_ascii_lowercase();
34+
char::to_ascii_lowercase(&a) == char::to_ascii_lowercase(&b);
35+
}
36+
37+
fn simple_char(a: char, b: char) {
38+
a.eq_ignore_ascii_case(&b);
39+
a.to_ascii_lowercase() == *&b.to_ascii_lowercase();
40+
*&a.to_ascii_lowercase() == b.to_ascii_lowercase();
41+
a.to_ascii_lowercase() == 'a';
42+
'a' == b.to_ascii_lowercase();
43+
}
44+
fn simple_u8(a: u8, b: u8) {
45+
a.eq_ignore_ascii_case(&b);
46+
a.to_ascii_lowercase() == b'a';
47+
b'a' == b.to_ascii_lowercase();
48+
}
49+
fn simple_str(a: &str, b: &str) {
50+
a.eq_ignore_ascii_case(b);
51+
a.to_uppercase().eq_ignore_ascii_case(b);
52+
a.to_ascii_lowercase() == "a";
53+
"a" == b.to_ascii_lowercase();
54+
}
55+
fn simple_string(a: String, b: String) {
56+
a.eq_ignore_ascii_case(&b);
57+
a.to_ascii_lowercase() == "a";
58+
"a" == b.to_ascii_lowercase();
59+
}
60+
fn simple_string2(a: String, b: &String) {
61+
a.eq_ignore_ascii_case(b);
62+
a.to_ascii_lowercase() == "a";
63+
"a" == b.to_ascii_lowercase();
64+
}
65+
fn simple_string3(a: &String, b: String) {
66+
a.eq_ignore_ascii_case(&b);
67+
a.to_ascii_lowercase() == "a";
68+
"a" == b.to_ascii_lowercase();
69+
}
70+
fn simple_u8slice(a: &[u8], b: &[u8]) {
71+
a.eq_ignore_ascii_case(b);
72+
}
73+
fn simple_u8vec(a: Vec<u8>, b: Vec<u8>) {
74+
a.eq_ignore_ascii_case(&b);
75+
}
76+
fn simple_u8vec2(a: Vec<u8>, b: &Vec<u8>) {
77+
a.eq_ignore_ascii_case(b);
78+
}
79+
fn simple_u8vec3(a: &Vec<u8>, b: Vec<u8>) {
80+
a.eq_ignore_ascii_case(&b);
81+
}
82+
fn simple_osstr(a: &OsStr, b: &OsStr) {
83+
a.eq_ignore_ascii_case(b);
84+
}
85+
fn simple_osstring(a: OsString, b: OsString) {
86+
a.eq_ignore_ascii_case(b);
87+
}
88+
fn simple_osstring2(a: OsString, b: &OsString) {
89+
a.eq_ignore_ascii_case(b);
90+
}
91+
fn simple_osstring3(a: &OsString, b: OsString) {
92+
a.eq_ignore_ascii_case(b);
93+
}

tests/ui/manual_ignore_case_cmp.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#![allow(clippy::all)]
2+
#![deny(clippy::manual_ignore_case_cmp)]
3+
4+
use std::ffi::{OsStr, OsString};
5+
6+
fn main() {}
7+
8+
fn variants(a: &str, b: &str) {
9+
if a.to_ascii_lowercase() == b.to_ascii_lowercase() {
10+
return;
11+
}
12+
if a.to_ascii_uppercase() == b.to_ascii_uppercase() {
13+
return;
14+
}
15+
let r = a.to_ascii_lowercase() == b.to_ascii_lowercase();
16+
let r = r || a.to_ascii_uppercase() == b.to_ascii_uppercase();
17+
r && a.to_ascii_lowercase() == b.to_uppercase().to_ascii_lowercase();
18+
// !=
19+
if a.to_ascii_lowercase() != b.to_ascii_lowercase() {
20+
return;
21+
}
22+
if a.to_ascii_uppercase() != b.to_ascii_uppercase() {
23+
return;
24+
}
25+
let r = a.to_ascii_lowercase() != b.to_ascii_lowercase();
26+
let r = r || a.to_ascii_uppercase() != b.to_ascii_uppercase();
27+
r && a.to_ascii_lowercase() != b.to_uppercase().to_ascii_lowercase();
28+
}
29+
30+
fn unsupported(a: char, b: char) {
31+
// TODO:: these are rare, and might not be worth supporting
32+
a.to_ascii_lowercase() == char::to_ascii_lowercase(&b);
33+
char::to_ascii_lowercase(&a) == b.to_ascii_lowercase();
34+
char::to_ascii_lowercase(&a) == char::to_ascii_lowercase(&b);
35+
}
36+
37+
fn simple_char(a: char, b: char) {
38+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
39+
a.to_ascii_lowercase() == *&b.to_ascii_lowercase();
40+
*&a.to_ascii_lowercase() == b.to_ascii_lowercase();
41+
a.to_ascii_lowercase() == 'a';
42+
'a' == b.to_ascii_lowercase();
43+
}
44+
fn simple_u8(a: u8, b: u8) {
45+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
46+
a.to_ascii_lowercase() == b'a';
47+
b'a' == b.to_ascii_lowercase();
48+
}
49+
fn simple_str(a: &str, b: &str) {
50+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
51+
a.to_uppercase().to_ascii_lowercase() == b.to_ascii_lowercase();
52+
a.to_ascii_lowercase() == "a";
53+
"a" == b.to_ascii_lowercase();
54+
}
55+
fn simple_string(a: String, b: String) {
56+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
57+
a.to_ascii_lowercase() == "a";
58+
"a" == b.to_ascii_lowercase();
59+
}
60+
fn simple_string2(a: String, b: &String) {
61+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
62+
a.to_ascii_lowercase() == "a";
63+
"a" == b.to_ascii_lowercase();
64+
}
65+
fn simple_string3(a: &String, b: String) {
66+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
67+
a.to_ascii_lowercase() == "a";
68+
"a" == b.to_ascii_lowercase();
69+
}
70+
fn simple_u8slice(a: &[u8], b: &[u8]) {
71+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
72+
}
73+
fn simple_u8vec(a: Vec<u8>, b: Vec<u8>) {
74+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
75+
}
76+
fn simple_u8vec2(a: Vec<u8>, b: &Vec<u8>) {
77+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
78+
}
79+
fn simple_u8vec3(a: &Vec<u8>, b: Vec<u8>) {
80+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
81+
}
82+
fn simple_osstr(a: &OsStr, b: &OsStr) {
83+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
84+
}
85+
fn simple_osstring(a: OsString, b: OsString) {
86+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
87+
}
88+
fn simple_osstring2(a: OsString, b: &OsString) {
89+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
90+
}
91+
fn simple_osstring3(a: &OsString, b: OsString) {
92+
a.to_ascii_lowercase() == b.to_ascii_lowercase();
93+
}

0 commit comments

Comments
 (0)