about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlapla-cogito <me@lapla.dev>2025-03-06 13:44:23 +0900
committerlapla-cogito <me@lapla.dev>2025-03-25 21:26:35 +0900
commite78216c4a14c4590a0efa3deba7f9ed08e1d0b31 (patch)
tree942ab84dcdf0dec403baf733f8e7829d7433235e
parent81643e297cf44ce3c7648b8443fc4d6592fa81eb (diff)
downloadrust-e78216c4a14c4590a0efa3deba7f9ed08e1d0b31.tar.gz
rust-e78216c4a14c4590a0efa3deba7f9ed08e1d0b31.zip
suggest `is_some_and` instead of `map_or` in `case_sensitive_file_extension_comparions`
-rw-r--r--clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs14
-rw-r--r--clippy_lints/src/methods/mod.rs2
-rw-r--r--tests/ui/case_sensitive_file_extension_comparisons.fixed21
-rw-r--r--tests/ui/case_sensitive_file_extension_comparisons.rs7
-rw-r--r--tests/ui/case_sensitive_file_extension_comparisons.stderr40
5 files changed, 58 insertions, 26 deletions
diff --git a/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs b/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs
index 18568e3661f..1c745b7376e 100644
--- a/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs
+++ b/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs
@@ -1,4 +1,5 @@
 use clippy_utils::diagnostics::span_lint_and_then;
+use clippy_utils::msrvs::{self, Msrv};
 use clippy_utils::source::{SpanRangeExt, indent_of, reindent_multiline};
 use clippy_utils::ty::is_type_lang_item;
 use rustc_ast::ast::LitKind;
@@ -16,6 +17,7 @@ pub(super) fn check<'tcx>(
     call_span: Span,
     recv: &'tcx Expr<'_>,
     arg: &'tcx Expr<'_>,
+    msrv: Msrv,
 ) {
     if let ExprKind::MethodCall(path_segment, ..) = recv.kind {
         if matches!(
@@ -58,11 +60,15 @@ pub(super) fn check<'tcx>(
 
                     let suggestion_source = reindent_multiline(
                         &format!(
-                            "std::path::Path::new({})
+                            "std::path::Path::new({recv_source})
                                 .extension()
-                                .map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))",
-                            recv_source,
-                            ext_str.strip_prefix('.').unwrap()
+                                .{}|ext| ext.eq_ignore_ascii_case(\"{}\"))",
+                            if msrv.meets(cx, msrvs::OPTION_RESULT_IS_VARIANT_AND) {
+                                "is_some_and("
+                            } else {
+                                "map_or(false, "
+                            },
+                            ext_str.strip_prefix('.').unwrap(),
                         ),
                         true,
                         Some(indent_of(cx, call_span).unwrap_or(0) + 4),
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 94d3657d9f1..f17bf6d6366 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -4992,7 +4992,7 @@ impl Methods {
                 },
                 ("ends_with", [arg]) => {
                     if let ExprKind::MethodCall(.., span) = expr.kind {
-                        case_sensitive_file_extension_comparisons::check(cx, expr, span, recv, arg);
+                        case_sensitive_file_extension_comparisons::check(cx, expr, span, recv, arg, self.msrv);
                     }
                     path_ends_with_ext::check(cx, recv, arg, expr, self.msrv, &self.allowed_dotfiles);
                 },
diff --git a/tests/ui/case_sensitive_file_extension_comparisons.fixed b/tests/ui/case_sensitive_file_extension_comparisons.fixed
index bf7635fdf09..0c9d2124354 100644
--- a/tests/ui/case_sensitive_file_extension_comparisons.fixed
+++ b/tests/ui/case_sensitive_file_extension_comparisons.fixed
@@ -1,5 +1,4 @@
 #![warn(clippy::case_sensitive_file_extension_comparisons)]
-#![allow(clippy::unnecessary_map_or)]
 
 use std::string::String;
 
@@ -13,7 +12,7 @@ impl TestStruct {
 fn is_rust_file(filename: &str) -> bool {
     std::path::Path::new(filename)
         .extension()
-        .map_or(false, |ext| ext.eq_ignore_ascii_case("rs"))
+        .is_some_and(|ext| ext.eq_ignore_ascii_case("rs"))
     //~^ case_sensitive_file_extension_comparisons
 }
 
@@ -21,18 +20,18 @@ fn main() {
     // std::string::String and &str should trigger the lint failure with .ext12
     let _ = std::path::Path::new(&String::new())
         .extension()
-        .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
+        .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
     //~^ case_sensitive_file_extension_comparisons
     let _ = std::path::Path::new("str")
         .extension()
-        .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
+        .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
     //~^ case_sensitive_file_extension_comparisons
 
     // The fixup should preserve the indentation level
     {
         let _ = std::path::Path::new("str")
             .extension()
-            .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
+            .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
         //~^ case_sensitive_file_extension_comparisons
     }
 
@@ -42,11 +41,11 @@ fn main() {
     // std::string::String and &str should trigger the lint failure with .EXT12
     let _ = std::path::Path::new(&String::new())
         .extension()
-        .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
+        .is_some_and(|ext| ext.eq_ignore_ascii_case("EXT12"));
     //~^ case_sensitive_file_extension_comparisons
     let _ = std::path::Path::new("str")
         .extension()
-        .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
+        .is_some_and(|ext| ext.eq_ignore_ascii_case("EXT12"));
     //~^ case_sensitive_file_extension_comparisons
 
     // Should not trigger the lint failure because of the calls to to_lowercase and to_uppercase
@@ -76,3 +75,11 @@ fn main() {
     let _ = "str".ends_with(".123");
     TestStruct {}.ends_with(".123");
 }
+
+#[clippy::msrv = "1.69"]
+fn msrv_check() {
+    let _ = std::path::Path::new(&String::new())
+        .extension()
+        .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
+    //~^ case_sensitive_file_extension_comparisons
+}
diff --git a/tests/ui/case_sensitive_file_extension_comparisons.rs b/tests/ui/case_sensitive_file_extension_comparisons.rs
index 0c4070a42d4..f8a947aa827 100644
--- a/tests/ui/case_sensitive_file_extension_comparisons.rs
+++ b/tests/ui/case_sensitive_file_extension_comparisons.rs
@@ -1,5 +1,4 @@
 #![warn(clippy::case_sensitive_file_extension_comparisons)]
-#![allow(clippy::unnecessary_map_or)]
 
 use std::string::String;
 
@@ -64,3 +63,9 @@ fn main() {
     let _ = "str".ends_with(".123");
     TestStruct {}.ends_with(".123");
 }
+
+#[clippy::msrv = "1.69"]
+fn msrv_check() {
+    let _ = String::new().ends_with(".ext12");
+    //~^ case_sensitive_file_extension_comparisons
+}
diff --git a/tests/ui/case_sensitive_file_extension_comparisons.stderr b/tests/ui/case_sensitive_file_extension_comparisons.stderr
index e035534d269..93bee8e7667 100644
--- a/tests/ui/case_sensitive_file_extension_comparisons.stderr
+++ b/tests/ui/case_sensitive_file_extension_comparisons.stderr
@@ -1,5 +1,5 @@
 error: case-sensitive file extension comparison
-  --> tests/ui/case_sensitive_file_extension_comparisons.rs:14:5
+  --> tests/ui/case_sensitive_file_extension_comparisons.rs:13:5
    |
 LL |     filename.ends_with(".rs")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -11,11 +11,11 @@ help: use std::path::Path
    |
 LL ~     std::path::Path::new(filename)
 LL +         .extension()
-LL +         .map_or(false, |ext| ext.eq_ignore_ascii_case("rs"))
+LL +         .is_some_and(|ext| ext.eq_ignore_ascii_case("rs"))
    |
 
 error: case-sensitive file extension comparison
-  --> tests/ui/case_sensitive_file_extension_comparisons.rs:20:13
+  --> tests/ui/case_sensitive_file_extension_comparisons.rs:19:13
    |
 LL |     let _ = String::new().ends_with(".ext12");
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -25,11 +25,11 @@ help: use std::path::Path
    |
 LL ~     let _ = std::path::Path::new(&String::new())
 LL +         .extension()
-LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
+LL ~         .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
    |
 
 error: case-sensitive file extension comparison
-  --> tests/ui/case_sensitive_file_extension_comparisons.rs:22:13
+  --> tests/ui/case_sensitive_file_extension_comparisons.rs:21:13
    |
 LL |     let _ = "str".ends_with(".ext12");
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -39,11 +39,11 @@ help: use std::path::Path
    |
 LL ~     let _ = std::path::Path::new("str")
 LL +         .extension()
-LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
+LL ~         .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
    |
 
 error: case-sensitive file extension comparison
-  --> tests/ui/case_sensitive_file_extension_comparisons.rs:27:17
+  --> tests/ui/case_sensitive_file_extension_comparisons.rs:26:17
    |
 LL |         let _ = "str".ends_with(".ext12");
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -53,11 +53,11 @@ help: use std::path::Path
    |
 LL ~         let _ = std::path::Path::new("str")
 LL +             .extension()
-LL ~             .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
+LL ~             .is_some_and(|ext| ext.eq_ignore_ascii_case("ext12"));
    |
 
 error: case-sensitive file extension comparison
-  --> tests/ui/case_sensitive_file_extension_comparisons.rs:35:13
+  --> tests/ui/case_sensitive_file_extension_comparisons.rs:34:13
    |
 LL |     let _ = String::new().ends_with(".EXT12");
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -67,11 +67,11 @@ help: use std::path::Path
    |
 LL ~     let _ = std::path::Path::new(&String::new())
 LL +         .extension()
-LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
+LL ~         .is_some_and(|ext| ext.eq_ignore_ascii_case("EXT12"));
    |
 
 error: case-sensitive file extension comparison
-  --> tests/ui/case_sensitive_file_extension_comparisons.rs:37:13
+  --> tests/ui/case_sensitive_file_extension_comparisons.rs:36:13
    |
 LL |     let _ = "str".ends_with(".EXT12");
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -81,8 +81,22 @@ help: use std::path::Path
    |
 LL ~     let _ = std::path::Path::new("str")
 LL +         .extension()
-LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
+LL ~         .is_some_and(|ext| ext.eq_ignore_ascii_case("EXT12"));
+   |
+
+error: case-sensitive file extension comparison
+  --> tests/ui/case_sensitive_file_extension_comparisons.rs:69:13
+   |
+LL |     let _ = String::new().ends_with(".ext12");
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using a case-insensitive comparison instead
+help: use std::path::Path
+   |
+LL ~     let _ = std::path::Path::new(&String::new())
+LL +         .extension()
+LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
    |
 
-error: aborting due to 6 previous errors
+error: aborting due to 7 previous errors