|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_then;
|
| 2 | +use clippy_utils::source::{indent_of, reindent_multiline}; |
2 | 3 | use clippy_utils::sugg::Sugg;
|
3 | 4 | use clippy_utils::ty::is_type_lang_item;
|
4 | 5 | use if_chain::if_chain;
|
@@ -43,23 +44,32 @@ pub(super) fn check<'tcx>(
|
43 | 44 | recv_str = format!("&{recv_str}");
|
44 | 45 | }
|
45 | 46 |
|
46 |
| - if recv_str.contains(".to_lowercase()") { |
| 47 | + if recv_str.ends_with(".to_lowercase()") { |
47 | 48 | diag.note("to_lowercase allocates memory, this can be avoided by using Path");
|
48 |
| - recv_str = recv_str.replace(".to_lowercase()", ""); |
| 49 | + let (prefix, _) = recv_str.split_at(recv_str.len() - ".to_lowercase()".len()); |
| 50 | + recv_str = prefix.to_string(); |
49 | 51 | }
|
50 | 52 |
|
51 |
| - if recv_str.contains(".to_uppercase()") { |
| 53 | + if recv_str.ends_with(".to_uppercase()") { |
52 | 54 | diag.note("to_uppercase allocates memory, this can be avoided by using Path");
|
53 |
| - recv_str = recv_str.replace(".to_uppercase()", ""); |
| 55 | + let (prefix, _) = recv_str.split_at(recv_str.len() - ".to_uppercase()".len()); |
| 56 | + recv_str = prefix.to_string(); |
54 | 57 | }
|
55 | 58 |
|
| 59 | + let suggestion_source: String = reindent_multiline( |
| 60 | + format!( |
| 61 | + "std::path::Path::new({}) |
| 62 | + .extension() |
| 63 | + .map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))", |
| 64 | + recv_str, ext_str.strip_prefix('.').unwrap()).into(), |
| 65 | + true, |
| 66 | + Some(indent_of(cx, call_span).unwrap_or(0) + 4) |
| 67 | + ).to_string(); |
| 68 | + |
56 | 69 | diag.span_suggestion(
|
57 | 70 | recv.span.to(call_span),
|
58 | 71 | "use std::path::Path",
|
59 |
| - format!("std::path::Path::new({}) |
60 |
| - .extension() |
61 |
| - .map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))", |
62 |
| - recv_str, ext_str.strip_prefix('.').unwrap()), |
| 72 | + suggestion_source, |
63 | 73 | Applicability::MaybeIncorrect,
|
64 | 74 | );
|
65 | 75 | }
|
|
0 commit comments