From 0cee2c50952a11099692652af367b4fea3cb09a6 Mon Sep 17 00:00:00 2001 From: Tyler Weaver Date: Mon, 2 Jan 2023 16:42:56 -0700 Subject: [PATCH] Don't trigger lint if last method is to_lower/upper --- ...se_sensitive_file_extension_comparisons.rs | 17 ++++------ ...sensitive_file_extension_comparisons.fixed | 10 ++---- ...se_sensitive_file_extension_comparisons.rs | 2 +- ...ensitive_file_extension_comparisons.stderr | 32 +------------------ 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 e34c377f5..b6ec0fba6 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 a8b5950f2..a19ed1ddc 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 ee9bcd73d..ad56b7296 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 33435f086..b5c8e4b4f 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