Skip to content

Commit ea6ff7e

Browse files
committed
Apply changes suggested in review
1 parent 0cee2c5 commit ea6ff7e

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::source::snippet_opt;
23
use clippy_utils::source::{indent_of, reindent_multiline};
3-
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::is_type_lang_item;
55
use if_chain::if_chain;
66
use rustc_ast::ast::LitKind;
@@ -19,8 +19,10 @@ pub(super) fn check<'tcx>(
1919
arg: &'tcx Expr<'_>,
2020
) {
2121
if let ExprKind::MethodCall(path_segment, ..) = recv.kind {
22-
let method_name = path_segment.ident.name.as_str();
23-
if method_name == "to_lowercase" || method_name == "to_uppercase" {
22+
if matches!(
23+
path_segment.ident.name.as_str(),
24+
"to_lowercase" | "to_uppercase" | "to_ascii_lowercase" | "to_ascii_uppercase"
25+
) {
2426
return;
2527
}
2628
}
@@ -45,28 +47,29 @@ pub(super) fn check<'tcx>(
4547
"case-sensitive file extension comparison",
4648
|diag| {
4749
diag.help("consider using a case-insensitive comparison instead");
48-
let mut recv_source = Sugg::hir(cx, recv, "").to_string();
50+
if let Some(mut recv_source) = snippet_opt(cx, recv.span) {
4951

50-
if is_type_lang_item(cx, recv_ty, LangItem::String) {
51-
recv_source = format!("&{recv_source}");
52-
}
52+
if !cx.typeck_results().expr_ty(recv).is_ref() {
53+
recv_source = format!("&{recv_source}");
54+
}
5355

54-
let suggestion_source = reindent_multiline(
55-
format!(
56-
"std::path::Path::new({})
57-
.extension()
58-
.map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))",
59-
recv_source, ext_str.strip_prefix('.').unwrap()).into(),
60-
true,
61-
Some(indent_of(cx, call_span).unwrap_or(0) + 4)
62-
);
56+
let suggestion_source = reindent_multiline(
57+
format!(
58+
"std::path::Path::new({})
59+
.extension()
60+
.map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))",
61+
recv_source, ext_str.strip_prefix('.').unwrap()).into(),
62+
true,
63+
Some(indent_of(cx, call_span).unwrap_or(0) + 4)
64+
);
6365

64-
diag.span_suggestion(
65-
recv.span.to(call_span),
66-
"use std::path::Path",
67-
suggestion_source,
68-
Applicability::MaybeIncorrect,
69-
);
66+
diag.span_suggestion(
67+
recv.span.to(call_span),
68+
"use std::path::Path",
69+
suggestion_source,
70+
Applicability::MaybeIncorrect,
71+
);
72+
}
7073
}
7174
);
7275
}

tests/ui/case_sensitive_file_extension_comparisons.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ fn main() {
2525
.extension()
2626
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
2727

28+
// The fixup should preserve the indentation level
29+
{
30+
let _ = std::path::Path::new("str")
31+
.extension()
32+
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
33+
}
34+
2835
// The test struct should not trigger the lint failure with .ext12
2936
TestStruct {}.ends_with(".ext12");
3037

tests/ui/case_sensitive_file_extension_comparisons.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ fn main() {
1919
let _ = String::new().ends_with(".ext12");
2020
let _ = "str".ends_with(".ext12");
2121

22+
// The fixup should preserve the indentation level
23+
{
24+
let _ = "str".ends_with(".ext12");
25+
}
26+
2227
// The test struct should not trigger the lint failure with .ext12
2328
TestStruct {}.ends_with(".ext12");
2429

tests/ui/case_sensitive_file_extension_comparisons.stderr

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,21 @@ LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
4242
|
4343

4444
error: case-sensitive file extension comparison
45-
--> $DIR/case_sensitive_file_extension_comparisons.rs:26:13
45+
--> $DIR/case_sensitive_file_extension_comparisons.rs:24:17
46+
|
47+
LL | let _ = "str".ends_with(".ext12");
48+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
49+
|
50+
= help: consider using a case-insensitive comparison instead
51+
help: use std::path::Path
52+
|
53+
LL ~ let _ = std::path::Path::new("str")
54+
LL + .extension()
55+
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
56+
|
57+
58+
error: case-sensitive file extension comparison
59+
--> $DIR/case_sensitive_file_extension_comparisons.rs:31:13
4660
|
4761
LL | let _ = String::new().ends_with(".EXT12");
4862
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -56,7 +70,7 @@ LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
5670
|
5771

5872
error: case-sensitive file extension comparison
59-
--> $DIR/case_sensitive_file_extension_comparisons.rs:27:13
73+
--> $DIR/case_sensitive_file_extension_comparisons.rs:32:13
6074
|
6175
LL | let _ = "str".ends_with(".EXT12");
6276
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -69,5 +83,5 @@ LL + .extension()
6983
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
7084
|
7185

72-
error: aborting due to 5 previous errors
86+
error: aborting due to 6 previous errors
7387

0 commit comments

Comments
 (0)