Don't trigger lint if last method is to_lower/upper

This commit is contained in:
Tyler Weaver 2023-01-02 16:42:56 -07:00
parent 0aa7d73df3
commit 0cee2c5095
No known key found for this signature in database
4 changed files with 12 additions and 49 deletions

View File

@ -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({})

View File

@ -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");

View File

@ -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");

View File

@ -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