Skip to content

Commit b604d69

Browse files
committed
Sugggest using Path for comparing extensions
Signed-off-by: Tyler Weaver <[email protected]>
1 parent 4a09068 commit b604d69

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::sugg::Sugg;
23
use clippy_utils::ty::is_type_lang_item;
34
use if_chain::if_chain;
45
use rustc_ast::ast::LitKind;
6+
use rustc_errors::Applicability;
57
use rustc_hir::{Expr, ExprKind, LangItem};
68
use rustc_lint::LateContext;
79
use rustc_span::{source_map::Spanned, Span};
@@ -28,13 +30,23 @@ pub(super) fn check<'tcx>(
2830
let recv_ty = cx.typeck_results().expr_ty(recv).peel_refs();
2931
if recv_ty.is_str() || is_type_lang_item(cx, recv_ty, LangItem::String);
3032
then {
31-
span_lint_and_help(
33+
span_lint_and_then(
3234
cx,
3335
CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
34-
call_span,
36+
recv.span.to(call_span),
3537
"case-sensitive file extension comparison",
36-
None,
37-
"consider using a case-insensitive comparison instead",
38+
|diag| {
39+
diag.help("consider using a case-insensitive comparison instead");
40+
diag.span_suggestion(
41+
recv.span.to(call_span),
42+
"use std::path::Path",
43+
format!("std::path::Path::new({})
44+
.extension()
45+
.map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))",
46+
Sugg::hir(cx, recv, ""), ext_str.strip_prefix('.').unwrap()),
47+
Applicability::MaybeIncorrect,
48+
);
49+
}
3850
);
3951
}
4052
}
Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,73 @@
11
error: case-sensitive file extension comparison
2-
--> $DIR/case_sensitive_file_extension_comparisons.rs:12:14
2+
--> $DIR/case_sensitive_file_extension_comparisons.rs:12:5
33
|
44
LL | filename.ends_with(".rs")
5-
| ^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: consider using a case-insensitive comparison instead
88
= note: `-D clippy::case-sensitive-file-extension-comparisons` implied by `-D warnings`
9+
help: use std::path::Path
10+
|
11+
LL ~ std::path::Path::new(filename)
12+
LL + .extension()
13+
LL + .map_or(false, |ext| ext.eq_ignore_ascii_case("rs"))
14+
|
915

1016
error: case-sensitive file extension comparison
11-
--> $DIR/case_sensitive_file_extension_comparisons.rs:17:27
17+
--> $DIR/case_sensitive_file_extension_comparisons.rs:17:13
1218
|
1319
LL | let _ = String::new().ends_with(".ext12");
14-
| ^^^^^^^^^^^^^^^^^^^
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1521
|
1622
= help: consider using a case-insensitive comparison instead
23+
help: use std::path::Path
24+
|
25+
LL ~ let _ = std::path::Path::new(String::new())
26+
LL + .extension()
27+
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
28+
|
1729

1830
error: case-sensitive file extension comparison
19-
--> $DIR/case_sensitive_file_extension_comparisons.rs:18:19
31+
--> $DIR/case_sensitive_file_extension_comparisons.rs:18:13
2032
|
2133
LL | let _ = "str".ends_with(".ext12");
22-
| ^^^^^^^^^^^^^^^^^^^
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2335
|
2436
= help: consider using a case-insensitive comparison instead
37+
help: use std::path::Path
38+
|
39+
LL ~ let _ = std::path::Path::new("str")
40+
LL + .extension()
41+
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
42+
|
2543

2644
error: case-sensitive file extension comparison
27-
--> $DIR/case_sensitive_file_extension_comparisons.rs:24:27
45+
--> $DIR/case_sensitive_file_extension_comparisons.rs:24:13
2846
|
2947
LL | let _ = String::new().ends_with(".EXT12");
30-
| ^^^^^^^^^^^^^^^^^^^
48+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3149
|
3250
= help: consider using a case-insensitive comparison instead
51+
help: use std::path::Path
52+
|
53+
LL ~ let _ = std::path::Path::new(String::new())
54+
LL + .extension()
55+
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
56+
|
3357

3458
error: case-sensitive file extension comparison
35-
--> $DIR/case_sensitive_file_extension_comparisons.rs:25:19
59+
--> $DIR/case_sensitive_file_extension_comparisons.rs:25:13
3660
|
3761
LL | let _ = "str".ends_with(".EXT12");
38-
| ^^^^^^^^^^^^^^^^^^^
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
3963
|
4064
= help: consider using a case-insensitive comparison instead
65+
help: use std::path::Path
66+
|
67+
LL ~ let _ = std::path::Path::new("str")
68+
LL + .extension()
69+
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
70+
|
4171

4272
error: aborting due to 5 previous errors
4373

0 commit comments

Comments
 (0)