about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs17
-rw-r--r--tests/ui/case_sensitive_file_extension_comparisons.fixed10
-rw-r--r--tests/ui/case_sensitive_file_extension_comparisons.rs2
-rw-r--r--tests/ui/case_sensitive_file_extension_comparisons.stderr32
4 files changed, 12 insertions, 49 deletions
diff --git a/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs b/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs
index e34c377f5fc..b6ec0fba6cf 100644
--- a/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs
+++ b/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs
@@ -18,6 +18,13 @@ pub(super) fn check<'tcx>(
     recv: &'tcx Expr<'_>,
     arg: &'tcx Expr<'_>,
 ) {
+    if let ExprKind::MethodCall(path_segment, ..) = recv.kind {
+        let method_name = path_segment.ident.name.as_str();
+        if method_name == "to_lowercase" || method_name == "to_uppercase" {
+            return;
+        }
+    }
+
     if_chain! {
         if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
         if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
@@ -44,16 +51,6 @@ pub(super) fn check<'tcx>(
                         recv_source = format!("&{recv_source}");
                     }
 
-                    if recv_source.ends_with(".to_lowercase()") {
-                        diag.note("to_lowercase allocates memory, this can be avoided by using Path");
-                        recv_source = recv_source.strip_suffix(".to_lowercase()").unwrap().to_string();
-                    }
-
-                    if recv_source.ends_with(".to_uppercase()") {
-                        diag.note("to_uppercase allocates memory, this can be avoided by using Path");
-                        recv_source = recv_source.strip_suffix(".to_uppercase()").unwrap().to_string();
-                    }
-
                     let suggestion_source = reindent_multiline(
                         format!(
                             "std::path::Path::new({})
diff --git a/tests/ui/case_sensitive_file_extension_comparisons.fixed b/tests/ui/case_sensitive_file_extension_comparisons.fixed
index a8b5950f208..a19ed1ddcd5 100644
--- a/tests/ui/case_sensitive_file_extension_comparisons.fixed
+++ b/tests/ui/case_sensitive_file_extension_comparisons.fixed
@@ -36,13 +36,9 @@ fn main() {
         .extension()
         .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
 
-    // This should print a note about how to_lowercase and to_uppercase allocates
-    let _ = std::path::Path::new(&String::new())
-        .extension()
-        .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
-    let _ = std::path::Path::new(&String::new())
-        .extension()
-        .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
+    // Should not trigger the lint failure because of the calls to to_lowercase and to_uppercase
+    let _ = String::new().to_lowercase().ends_with(".EXT12");
+    let _ = String::new().to_uppercase().ends_with(".EXT12");
 
     // The test struct should not trigger the lint failure with .EXT12
     TestStruct {}.ends_with(".EXT12");
diff --git a/tests/ui/case_sensitive_file_extension_comparisons.rs b/tests/ui/case_sensitive_file_extension_comparisons.rs
index ee9bcd73d52..ad56b7296f7 100644
--- a/tests/ui/case_sensitive_file_extension_comparisons.rs
+++ b/tests/ui/case_sensitive_file_extension_comparisons.rs
@@ -26,7 +26,7 @@ fn main() {
     let _ = String::new().ends_with(".EXT12");
     let _ = "str".ends_with(".EXT12");
 
-    // This should print a note about how to_lowercase and to_uppercase allocates
+    // Should not trigger the lint failure because of the calls to to_lowercase and to_uppercase
     let _ = String::new().to_lowercase().ends_with(".EXT12");
     let _ = String::new().to_uppercase().ends_with(".EXT12");
 
diff --git a/tests/ui/case_sensitive_file_extension_comparisons.stderr b/tests/ui/case_sensitive_file_extension_comparisons.stderr
index 33435f086d4..b5c8e4b4fe6 100644
--- a/tests/ui/case_sensitive_file_extension_comparisons.stderr
+++ b/tests/ui/case_sensitive_file_extension_comparisons.stderr
@@ -69,35 +69,5 @@ LL +         .extension()
 LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
    |
 
-error: case-sensitive file extension comparison
-  --> $DIR/case_sensitive_file_extension_comparisons.rs:30:13
-   |
-LL |     let _ = String::new().to_lowercase().ends_with(".EXT12");
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |
-   = help: consider using a case-insensitive comparison instead
-   = note: to_lowercase allocates memory, this can be avoided by using Path
-help: use std::path::Path
-   |
-LL ~     let _ = std::path::Path::new(&String::new())
-LL +         .extension()
-LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
-   |
-
-error: case-sensitive file extension comparison
-  --> $DIR/case_sensitive_file_extension_comparisons.rs:31:13
-   |
-LL |     let _ = String::new().to_uppercase().ends_with(".EXT12");
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |
-   = help: consider using a case-insensitive comparison instead
-   = note: to_uppercase allocates memory, this can be avoided by using Path
-help: use std::path::Path
-   |
-LL ~     let _ = std::path::Path::new(&String::new())
-LL +         .extension()
-LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
-   |
-
-error: aborting due to 7 previous errors
+error: aborting due to 5 previous errors