Skip to content

Commit 9d78426

Browse files
suggest is_some_and instead of map_or in case_sensitive_file_extension_comparions (rust-lang#14358)
close rust-lang#14357 changelog: [`case_sensitive_file_extension_comparisons`]: suggest `is_some_and` to suppress other lint warnings
2 parents f5d81a3 + e78216c commit 9d78426

5 files changed

+58
-26
lines changed

clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs

+10-4
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::msrvs::{self, Msrv};
23
use clippy_utils::source::{SpanRangeExt, indent_of, reindent_multiline};
34
use clippy_utils::ty::is_type_lang_item;
45
use rustc_ast::ast::LitKind;
@@ -16,6 +17,7 @@ pub(super) fn check<'tcx>(
1617
call_span: Span,
1718
recv: &'tcx Expr<'_>,
1819
arg: &'tcx Expr<'_>,
20+
msrv: Msrv,
1921
) {
2022
if let ExprKind::MethodCall(path_segment, ..) = recv.kind {
2123
if matches!(
@@ -58,11 +60,15 @@ pub(super) fn check<'tcx>(
5860

5961
let suggestion_source = reindent_multiline(
6062
&format!(
61-
"std::path::Path::new({})
63+
"std::path::Path::new({recv_source})
6264
.extension()
63-
.map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))",
64-
recv_source,
65-
ext_str.strip_prefix('.').unwrap()
65+
.{}|ext| ext.eq_ignore_ascii_case(\"{}\"))",
66+
if msrv.meets(cx, msrvs::OPTION_RESULT_IS_VARIANT_AND) {
67+
"is_some_and("
68+
} else {
69+
"map_or(false, "
70+
},
71+
ext_str.strip_prefix('.').unwrap(),
6672
),
6773
true,
6874
Some(indent_of(cx, call_span).unwrap_or(0) + 4),

clippy_lints/src/methods/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4992,7 +4992,7 @@ impl Methods {
49924992
},
49934993
("ends_with", [arg]) => {
49944994
if let ExprKind::MethodCall(.., span) = expr.kind {
4995-
case_sensitive_file_extension_comparisons::check(cx, expr, span, recv, arg);
4995+
case_sensitive_file_extension_comparisons::check(cx, expr, span, recv, arg, self.msrv);
49964996
}
49974997
path_ends_with_ext::check(cx, recv, arg, expr, self.msrv, &self.allowed_dotfiles);
49984998
},

tests/ui/case_sensitive_file_extension_comparisons.fixed

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![warn(clippy::case_sensitive_file_extension_comparisons)]
2-
#![allow(clippy::unnecessary_map_or)]
32

43
use std::string::String;
54

@@ -13,26 +12,26 @@ impl TestStruct {
1312
fn is_rust_file(filename: &str) -> bool {
1413
std::path::Path::new(filename)
1514
.extension()
16-
.map_or(false, |ext| ext.eq_ignore_ascii_case("rs"))
15+
.is_some_and(|ext| ext.eq_ignore_ascii_case("rs"))
1716
//~^ case_sensitive_file_extension_comparisons
1817
}
1918

2019
fn main() {
2120
// std::string::String and &str should trigger the lint failure with .ext12
2221
let _ = std::path::Path::new(&String::new())
2322
.extension()
24-
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
23+
.is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
2524
//~^ case_sensitive_file_extension_comparisons
2625
let _ = std::path::Path::new("str")
2726
.extension()
28-
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
27+
.is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
2928
//~^ case_sensitive_file_extension_comparisons
3029

3130
// The fixup should preserve the indentation level
3231
{
3332
let _ = std::path::Path::new("str")
3433
.extension()
35-
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
34+
.is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
3635
//~^ case_sensitive_file_extension_comparisons
3736
}
3837

@@ -42,11 +41,11 @@ fn main() {
4241
// std::string::String and &str should trigger the lint failure with .EXT12
4342
let _ = std::path::Path::new(&String::new())
4443
.extension()
45-
.map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
44+
.is_some_and(|ext| ext.eq_ignore_ascii_case("EXT12"));
4645
//~^ case_sensitive_file_extension_comparisons
4746
let _ = std::path::Path::new("str")
4847
.extension()
49-
.map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
48+
.is_some_and(|ext| ext.eq_ignore_ascii_case("EXT12"));
5049
//~^ case_sensitive_file_extension_comparisons
5150

5251
// Should not trigger the lint failure because of the calls to to_lowercase and to_uppercase
@@ -76,3 +75,11 @@ fn main() {
7675
let _ = "str".ends_with(".123");
7776
TestStruct {}.ends_with(".123");
7877
}
78+
79+
#[clippy::msrv = "1.69"]
80+
fn msrv_check() {
81+
let _ = std::path::Path::new(&String::new())
82+
.extension()
83+
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
84+
//~^ case_sensitive_file_extension_comparisons
85+
}

tests/ui/case_sensitive_file_extension_comparisons.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![warn(clippy::case_sensitive_file_extension_comparisons)]
2-
#![allow(clippy::unnecessary_map_or)]
32

43
use std::string::String;
54

@@ -64,3 +63,9 @@ fn main() {
6463
let _ = "str".ends_with(".123");
6564
TestStruct {}.ends_with(".123");
6665
}
66+
67+
#[clippy::msrv = "1.69"]
68+
fn msrv_check() {
69+
let _ = String::new().ends_with(".ext12");
70+
//~^ case_sensitive_file_extension_comparisons
71+
}

tests/ui/case_sensitive_file_extension_comparisons.stderr

+27-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: case-sensitive file extension comparison
2-
--> tests/ui/case_sensitive_file_extension_comparisons.rs:14:5
2+
--> tests/ui/case_sensitive_file_extension_comparisons.rs:13:5
33
|
44
LL | filename.ends_with(".rs")
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -11,11 +11,11 @@ help: use std::path::Path
1111
|
1212
LL ~ std::path::Path::new(filename)
1313
LL + .extension()
14-
LL + .map_or(false, |ext| ext.eq_ignore_ascii_case("rs"))
14+
LL + .is_some_and(|ext| ext.eq_ignore_ascii_case("rs"))
1515
|
1616

1717
error: case-sensitive file extension comparison
18-
--> tests/ui/case_sensitive_file_extension_comparisons.rs:20:13
18+
--> tests/ui/case_sensitive_file_extension_comparisons.rs:19:13
1919
|
2020
LL | let _ = String::new().ends_with(".ext12");
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -25,11 +25,11 @@ help: use std::path::Path
2525
|
2626
LL ~ let _ = std::path::Path::new(&String::new())
2727
LL + .extension()
28-
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
28+
LL ~ .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
2929
|
3030

3131
error: case-sensitive file extension comparison
32-
--> tests/ui/case_sensitive_file_extension_comparisons.rs:22:13
32+
--> tests/ui/case_sensitive_file_extension_comparisons.rs:21:13
3333
|
3434
LL | let _ = "str".ends_with(".ext12");
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -39,11 +39,11 @@ help: use std::path::Path
3939
|
4040
LL ~ let _ = std::path::Path::new("str")
4141
LL + .extension()
42-
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
42+
LL ~ .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
4343
|
4444

4545
error: case-sensitive file extension comparison
46-
--> tests/ui/case_sensitive_file_extension_comparisons.rs:27:17
46+
--> tests/ui/case_sensitive_file_extension_comparisons.rs:26:17
4747
|
4848
LL | let _ = "str".ends_with(".ext12");
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -53,11 +53,11 @@ help: use std::path::Path
5353
|
5454
LL ~ let _ = std::path::Path::new("str")
5555
LL + .extension()
56-
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
56+
LL ~ .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
5757
|
5858

5959
error: case-sensitive file extension comparison
60-
--> tests/ui/case_sensitive_file_extension_comparisons.rs:35:13
60+
--> tests/ui/case_sensitive_file_extension_comparisons.rs:34:13
6161
|
6262
LL | let _ = String::new().ends_with(".EXT12");
6363
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -67,11 +67,11 @@ help: use std::path::Path
6767
|
6868
LL ~ let _ = std::path::Path::new(&String::new())
6969
LL + .extension()
70-
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
70+
LL ~ .is_some_and(|ext| ext.eq_ignore_ascii_case("EXT12"));
7171
|
7272

7373
error: case-sensitive file extension comparison
74-
--> tests/ui/case_sensitive_file_extension_comparisons.rs:37:13
74+
--> tests/ui/case_sensitive_file_extension_comparisons.rs:36:13
7575
|
7676
LL | let _ = "str".ends_with(".EXT12");
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -81,8 +81,22 @@ help: use std::path::Path
8181
|
8282
LL ~ let _ = std::path::Path::new("str")
8383
LL + .extension()
84-
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
84+
LL ~ .is_some_and(|ext| ext.eq_ignore_ascii_case("EXT12"));
85+
|
86+
87+
error: case-sensitive file extension comparison
88+
--> tests/ui/case_sensitive_file_extension_comparisons.rs:69:13
89+
|
90+
LL | let _ = String::new().ends_with(".ext12");
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92+
|
93+
= help: consider using a case-insensitive comparison instead
94+
help: use std::path::Path
95+
|
96+
LL ~ let _ = std::path::Path::new(&String::new())
97+
LL + .extension()
98+
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
8599
|
86100

87-
error: aborting due to 6 previous errors
101+
error: aborting due to 7 previous errors
88102

0 commit comments

Comments
 (0)