]> git.lizzy.rs Git - rust.git/commitdiff
Apply changes suggested in review
authorTyler Weaver <maybe@tylerjw.dev>
Wed, 4 Jan 2023 14:06:07 +0000 (07:06 -0700)
committerTyler Weaver <maybe@tylerjw.dev>
Wed, 4 Jan 2023 14:06:07 +0000 (07:06 -0700)
clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs
tests/ui/case_sensitive_file_extension_comparisons.fixed
tests/ui/case_sensitive_file_extension_comparisons.rs
tests/ui/case_sensitive_file_extension_comparisons.stderr

index b6ec0fba6cfbb0c3a18b3187919df643acc739fc..0b3bf22743fae1b0c446c72112b6be8536a1ef4c 100644 (file)
@@ -1,6 +1,6 @@
 use clippy_utils::diagnostics::span_lint_and_then;
+use clippy_utils::source::snippet_opt;
 use clippy_utils::source::{indent_of, reindent_multiline};
-use clippy_utils::sugg::Sugg;
 use clippy_utils::ty::is_type_lang_item;
 use if_chain::if_chain;
 use rustc_ast::ast::LitKind;
@@ -19,8 +19,10 @@ pub(super) fn check<'tcx>(
     arg: &'tcx Expr<'_>,
 ) {
     if let ExprKind::MethodCall(path_segment, ..) = recv.kind {
-        let method_name = path_segment.ident.name.as_str();
-        if method_name == "to_lowercase" || method_name == "to_uppercase" {
+        if matches!(
+            path_segment.ident.name.as_str(),
+            "to_lowercase" | "to_uppercase" | "to_ascii_lowercase" | "to_ascii_uppercase"
+        ) {
             return;
         }
     }
@@ -45,28 +47,29 @@ pub(super) fn check<'tcx>(
                 "case-sensitive file extension comparison",
                 |diag| {
                     diag.help("consider using a case-insensitive comparison instead");
-                    let mut recv_source = Sugg::hir(cx, recv, "").to_string();
+                    if let Some(mut recv_source) = snippet_opt(cx, recv.span) {
 
-                    if is_type_lang_item(cx, recv_ty, LangItem::String) {
-                        recv_source = format!("&{recv_source}");
-                    }
+                        if !cx.typeck_results().expr_ty(recv).is_ref() {
+                            recv_source = format!("&{recv_source}");
+                        }
 
-                    let suggestion_source = reindent_multiline(
-                        format!(
-                            "std::path::Path::new({})
-                                .extension()
-                                .map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))",
-                            recv_source, ext_str.strip_prefix('.').unwrap()).into(),
-                        true,
-                        Some(indent_of(cx, call_span).unwrap_or(0) + 4)
-                    );
+                        let suggestion_source = reindent_multiline(
+                            format!(
+                                "std::path::Path::new({})
+                                    .extension()
+                                    .map_or(false, |ext| ext.eq_ignore_ascii_case(\"{}\"))",
+                                recv_source, ext_str.strip_prefix('.').unwrap()).into(),
+                            true,
+                            Some(indent_of(cx, call_span).unwrap_or(0) + 4)
+                        );
 
-                    diag.span_suggestion(
-                        recv.span.to(call_span),
-                        "use std::path::Path",
-                        suggestion_source,
-                        Applicability::MaybeIncorrect,
-                    );
+                        diag.span_suggestion(
+                            recv.span.to(call_span),
+                            "use std::path::Path",
+                            suggestion_source,
+                            Applicability::MaybeIncorrect,
+                        );
+                    }
                 }
             );
         }
index a19ed1ddcd54af7f27e0163247f9a11aef06f552..5fbaa64db39edb8406855012a3c1ef35e83cce28 100644 (file)
@@ -25,6 +25,13 @@ fn main() {
         .extension()
         .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
 
+    // The fixup should preserve the indentation level
+    {
+        let _ = std::path::Path::new("str")
+            .extension()
+            .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
+    }
+
     // The test struct should not trigger the lint failure with .ext12
     TestStruct {}.ends_with(".ext12");
 
index ad56b7296f75f550b5e3ed7f70f048bf9354cb85..3c0d4821f9f3a8ee9ff18b66e51e855cadc3655b 100644 (file)
@@ -19,6 +19,11 @@ fn main() {
     let _ = String::new().ends_with(".ext12");
     let _ = "str".ends_with(".ext12");
 
+    // The fixup should preserve the indentation level
+    {
+        let _ = "str".ends_with(".ext12");
+    }
+
     // The test struct should not trigger the lint failure with .ext12
     TestStruct {}.ends_with(".ext12");
 
index b5c8e4b4fe6832cee783b2497b0078eb657916cf..44c8e3fdf7403d05c6eba7101d231cc6c89d7e14 100644 (file)
@@ -42,7 +42,21 @@ LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12"));
    |
 
 error: case-sensitive file extension comparison
-  --> $DIR/case_sensitive_file_extension_comparisons.rs:26:13
+  --> $DIR/case_sensitive_file_extension_comparisons.rs:24:17
+   |
+LL |         let _ = "str".ends_with(".ext12");
+   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using a case-insensitive comparison instead
+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"));
+   |
+
+error: case-sensitive file extension comparison
+  --> $DIR/case_sensitive_file_extension_comparisons.rs:31:13
    |
 LL |     let _ = String::new().ends_with(".EXT12");
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -56,7 +70,7 @@ LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
    |
 
 error: case-sensitive file extension comparison
-  --> $DIR/case_sensitive_file_extension_comparisons.rs:27:13
+  --> $DIR/case_sensitive_file_extension_comparisons.rs:32:13
    |
 LL |     let _ = "str".ends_with(".EXT12");
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -69,5 +83,5 @@ LL +         .extension()
 LL ~         .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12"));
    |
 
-error: aborting due to 5 previous errors
+error: aborting due to 6 previous errors