mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 23:20:39 +00:00
Don't trigger lint if last method is to_lower/upper
This commit is contained in:
parent
0aa7d73df3
commit
0cee2c5095
4 changed files with 12 additions and 49 deletions
|
@ -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({})
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue