Skip to content

Commit e87eda0

Browse files
committed
Only remove method from end of recv, indent suggestion source
Signed-off-by: Tyler Weaver <[email protected]>
1 parent 909379d commit e87eda0

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::source::{indent_of, reindent_multiline};
23
use clippy_utils::sugg::Sugg;
34
use clippy_utils::ty::is_type_lang_item;
45
use if_chain::if_chain;
@@ -43,23 +44,30 @@ pub(super) fn check<'tcx>(
4344
recv_str = format!("&{recv_str}");
4445
}
4546

46-
if recv_str.contains(".to_lowercase()") {
47+
if recv_str.ends_with(".to_lowercase()") {
4748
diag.note("to_lowercase allocates memory, this can be avoided by using Path");
48-
recv_str = recv_str.replace(".to_lowercase()", "");
49+
recv_str = recv_str.strip_suffix(".to_lowercase()").unwrap().to_string();
4950
}
5051

51-
if recv_str.contains(".to_uppercase()") {
52+
if recv_str.ends_with(".to_uppercase()") {
5253
diag.note("to_uppercase allocates memory, this can be avoided by using Path");
53-
recv_str = recv_str.replace(".to_uppercase()", "");
54+
recv_str = recv_str.strip_suffix(".to_uppercase()").unwrap().to_string();
5455
}
5556

57+
let suggestion_source: String = reindent_multiline(
58+
format!(
59+
"std::path::Path::new({})
60+
.extension()
61+
.map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))",
62+
recv_str, ext_str.strip_prefix('.').unwrap()).into(),
63+
true,
64+
Some(indent_of(cx, call_span).unwrap_or(0) + 4)
65+
).to_string();
66+
5667
diag.span_suggestion(
5768
recv.span.to(call_span),
5869
"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,
6371
Applicability::MaybeIncorrect,
6472
);
6573
}

0 commit comments

Comments
 (0)