about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlexey Semenyuk <alexsemenyuk88@gmail.com>2024-08-18 17:50:29 +0500
committerAlexey Semenyuk <alexsemenyuk88@gmail.com>2024-08-19 23:25:45 +0500
commit7b7cf440cb5bc8733de38aeca8d42f3747bd45e1 (patch)
tree421c6acc38fb331441595bfa800773155d2a3742
parent5da97d006e33a4458f467e41ca43085f5e5b20ba (diff)
downloadrust-7b7cf440cb5bc8733de38aeca8d42f3747bd45e1.tar.gz
rust-7b7cf440cb5bc8733de38aeca8d42f3747bd45e1.zip
string_slice should detect on Cow
-rw-r--r--clippy_lints/src/strings.rs2
-rw-r--r--tests/ui/string_slice.rs5
-rw-r--r--tests/ui/string_slice.stderr14
3 files changed, 16 insertions, 5 deletions
diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs
index cfc387886dc..6ca4ca000e9 100644
--- a/clippy_lints/src/strings.rs
+++ b/clippy_lints/src/strings.rs
@@ -190,7 +190,7 @@ impl<'tcx> LateLintPass<'tcx> for StringAdd {
                 }
             },
             ExprKind::Index(target, _idx, _) => {
-                let e_ty = cx.typeck_results().expr_ty(target).peel_refs();
+                let e_ty = cx.typeck_results().expr_ty_adjusted(target).peel_refs();
                 if e_ty.is_str() || is_type_lang_item(cx, e_ty, LangItem::String) {
                     span_lint(
                         cx,
diff --git a/tests/ui/string_slice.rs b/tests/ui/string_slice.rs
index 440a86b104a..1d1911aaa1d 100644
--- a/tests/ui/string_slice.rs
+++ b/tests/ui/string_slice.rs
@@ -1,3 +1,5 @@
+use std::borrow::Cow;
+
 #[warn(clippy::string_slice)]
 #[allow(clippy::no_effect)]
 
@@ -11,4 +13,7 @@ fn main() {
     let s = String::from(m);
     &s[0..2];
     //~^ ERROR: indexing into a string may panic if the index is within a UTF-8 character
+    let a = Cow::Borrowed("foo");
+    &a[0..3];
+    //~^ ERROR: indexing into a string may panic if the index is within a UTF-8 character
 }
diff --git a/tests/ui/string_slice.stderr b/tests/ui/string_slice.stderr
index 7a4596b5f2d..bc0fcde34b8 100644
--- a/tests/ui/string_slice.stderr
+++ b/tests/ui/string_slice.stderr
@@ -1,5 +1,5 @@
 error: indexing into a string may panic if the index is within a UTF-8 character
-  --> tests/ui/string_slice.rs:5:6
+  --> tests/ui/string_slice.rs:7:6
    |
 LL |     &"Ölkanne"[1..];
    |      ^^^^^^^^^^^^^^
@@ -8,16 +8,22 @@ LL |     &"Ölkanne"[1..];
    = help: to override `-D warnings` add `#[allow(clippy::string_slice)]`
 
 error: indexing into a string may panic if the index is within a UTF-8 character
-  --> tests/ui/string_slice.rs:9:6
+  --> tests/ui/string_slice.rs:11:6
    |
 LL |     &m[2..5];
    |      ^^^^^^^
 
 error: indexing into a string may panic if the index is within a UTF-8 character
-  --> tests/ui/string_slice.rs:12:6
+  --> tests/ui/string_slice.rs:14:6
    |
 LL |     &s[0..2];
    |      ^^^^^^^
 
-error: aborting due to 3 previous errors
+error: indexing into a string may panic if the index is within a UTF-8 character
+  --> tests/ui/string_slice.rs:17:6
+   |
+LL |     &a[0..3];
+   |      ^^^^^^^
+
+error: aborting due to 4 previous errors