|
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; |
@@ -37,29 +38,36 @@ pub(super) fn check<'tcx>( |
37 | 38 | "case-sensitive file extension comparison", |
38 | 39 | |diag| { |
39 | 40 | diag.help("consider using a case-insensitive comparison instead"); |
40 | | - let mut recv_str = Sugg::hir(cx, recv, "").to_string(); |
| 41 | + let mut recv_source = Sugg::hir(cx, recv, "").to_string(); |
41 | 42 |
|
42 | 43 | if is_type_lang_item(cx, recv_ty, LangItem::String) { |
43 | | - recv_str = format!("&{recv_str}"); |
| 44 | + recv_source = format!("&{recv_source}"); |
44 | 45 | } |
45 | 46 |
|
46 | | - if recv_str.contains(".to_lowercase()") { |
| 47 | + if recv_source.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 | + recv_source = recv_source.strip_suffix(".to_lowercase()").unwrap().to_string(); |
49 | 50 | } |
50 | 51 |
|
51 | | - if recv_str.contains(".to_uppercase()") { |
| 52 | + if recv_source.ends_with(".to_uppercase()") { |
52 | 53 | diag.note("to_uppercase allocates memory, this can be avoided by using Path"); |
53 | | - recv_str = recv_str.replace(".to_uppercase()", ""); |
| 54 | + recv_source = recv_source.strip_suffix(".to_uppercase()").unwrap().to_string(); |
54 | 55 | } |
55 | 56 |
|
| 57 | + let suggestion_source = reindent_multiline( |
| 58 | + format!( |
| 59 | + "std::path::Path::new({}) |
| 60 | + .extension() |
| 61 | + .map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))", |
| 62 | + recv_source, ext_str.strip_prefix('.').unwrap()).into(), |
| 63 | + true, |
| 64 | + Some(indent_of(cx, call_span).unwrap_or(0) + 4) |
| 65 | + ); |
| 66 | + |
56 | 67 | diag.span_suggestion( |
57 | 68 | recv.span.to(call_span), |
58 | 69 | "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()), |
| 70 | + suggestion_source, |
63 | 71 | Applicability::MaybeIncorrect, |
64 | 72 | ); |
65 | 73 | } |
|
0 commit comments