Skip to content

Commit 0cee2c5

Browse files
committed
Don't trigger lint if last method is to_lower/upper
1 parent 0aa7d73 commit 0cee2c5

File tree

4 files changed

+12
-49
lines changed

4 files changed

+12
-49
lines changed

clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ pub(super) fn check<'tcx>(
1818
recv: &'tcx Expr<'_>,
1919
arg: &'tcx Expr<'_>,
2020
) {
21+
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" {
24+
return;
25+
}
26+
}
27+
2128
if_chain! {
2229
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
2330
if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
@@ -44,16 +51,6 @@ pub(super) fn check<'tcx>(
4451
recv_source = format!("&{recv_source}");
4552
}
4653

47-
if recv_source.ends_with(".to_lowercase()") {
48-
diag.note("to_lowercase allocates memory, this can be avoided by using Path");
49-
recv_source = recv_source.strip_suffix(".to_lowercase()").unwrap().to_string();
50-
}
51-
52-
if recv_source.ends_with(".to_uppercase()") {
53-
diag.note("to_uppercase allocates memory, this can be avoided by using Path");
54-
recv_source = recv_source.strip_suffix(".to_uppercase()").unwrap().to_string();
55-
}
56-
5754
let suggestion_source = reindent_multiline(
5855
format!(
5956
"std::path::Path::new({})

tests/ui/case_sensitive_file_extension_comparisons.fixed

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ fn main() {
3636
.extension()
3737
.map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
3838

39-
// This should print a note about how to_lowercase and to_uppercase allocates
40-
let _ = std::path::Path::new(&String::new())
41-
.extension()
42-
.map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
43-
let _ = std::path::Path::new(&String::new())
44-
.extension()
45-
.map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
39+
// Should not trigger the lint failure because of the calls to to_lowercase and to_uppercase
40+
let _ = String::new().to_lowercase().ends_with(".EXT12");
41+
let _ = String::new().to_uppercase().ends_with(".EXT12");
4642

4743
// The test struct should not trigger the lint failure with .EXT12
4844
TestStruct {}.ends_with(".EXT12");

tests/ui/case_sensitive_file_extension_comparisons.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
let _ = String::new().ends_with(".EXT12");
2727
let _ = "str".ends_with(".EXT12");
2828

29-
// This should print a note about how to_lowercase and to_uppercase allocates
29+
// Should not trigger the lint failure because of the calls to to_lowercase and to_uppercase
3030
let _ = String::new().to_lowercase().ends_with(".EXT12");
3131
let _ = String::new().to_uppercase().ends_with(".EXT12");
3232

tests/ui/case_sensitive_file_extension_comparisons.stderr

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,5 @@ LL + .extension()
6969
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
7070
|
7171

72-
error: case-sensitive file extension comparison
73-
--> $DIR/case_sensitive_file_extension_comparisons.rs:30:13
74-
|
75-
LL | let _ = String::new().to_lowercase().ends_with(".EXT12");
76-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77-
|
78-
= help: consider using a case-insensitive comparison instead
79-
= note: to_lowercase allocates memory, this can be avoided by using Path
80-
help: use std::path::Path
81-
|
82-
LL ~ let _ = std::path::Path::new(&String::new())
83-
LL + .extension()
84-
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
85-
|
86-
87-
error: case-sensitive file extension comparison
88-
--> $DIR/case_sensitive_file_extension_comparisons.rs:31:13
89-
|
90-
LL | let _ = String::new().to_uppercase().ends_with(".EXT12");
91-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92-
|
93-
= help: consider using a case-insensitive comparison instead
94-
= note: to_uppercase allocates memory, this can be avoided by using Path
95-
help: use std::path::Path
96-
|
97-
LL ~ let _ = std::path::Path::new(&String::new())
98-
LL + .extension()
99-
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
100-
|
101-
102-
error: aborting due to 7 previous errors
72+
error: aborting due to 5 previous errors
10373

0 commit comments

Comments
 (0)