Skip to content

Commit d0e637d

Browse files
committed
Auto merge of rust-lang#13281 - alex-semenyuk:string_slice_fix, r=y21
Trigger [`string_slice`] if expression is reference to `&str` Close rust-lang#12708 changelog: [`string_slice`]: trigger lint if expression is reference to `&str`
2 parents e7d9fcf + 7b7cf44 commit d0e637d

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

clippy_lints/src/strings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<'tcx> LateLintPass<'tcx> for StringAdd {
190190
}
191191
},
192192
ExprKind::Index(target, _idx, _) => {
193-
let e_ty = cx.typeck_results().expr_ty(target).peel_refs();
193+
let e_ty = cx.typeck_results().expr_ty_adjusted(target).peel_refs();
194194
if e_ty.is_str() || is_type_lang_item(cx, e_ty, LangItem::String) {
195195
span_lint(
196196
cx,

tests/ui/string_slice.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
#[warn(clippy::string_slice)]
24
#[allow(clippy::no_effect)]
35

@@ -11,4 +13,7 @@ fn main() {
1113
let s = String::from(m);
1214
&s[0..2];
1315
//~^ ERROR: indexing into a string may panic if the index is within a UTF-8 character
16+
let a = Cow::Borrowed("foo");
17+
&a[0..3];
18+
//~^ ERROR: indexing into a string may panic if the index is within a UTF-8 character
1419
}

tests/ui/string_slice.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: indexing into a string may panic if the index is within a UTF-8 character
2-
--> tests/ui/string_slice.rs:5:6
2+
--> tests/ui/string_slice.rs:7:6
33
|
44
LL | &"Ölkanne"[1..];
55
| ^^^^^^^^^^^^^^
@@ -8,16 +8,22 @@ LL | &"Ölkanne"[1..];
88
= help: to override `-D warnings` add `#[allow(clippy::string_slice)]`
99

1010
error: indexing into a string may panic if the index is within a UTF-8 character
11-
--> tests/ui/string_slice.rs:9:6
11+
--> tests/ui/string_slice.rs:11:6
1212
|
1313
LL | &m[2..5];
1414
| ^^^^^^^
1515

1616
error: indexing into a string may panic if the index is within a UTF-8 character
17-
--> tests/ui/string_slice.rs:12:6
17+
--> tests/ui/string_slice.rs:14:6
1818
|
1919
LL | &s[0..2];
2020
| ^^^^^^^
2121

22-
error: aborting due to 3 previous errors
22+
error: indexing into a string may panic if the index is within a UTF-8 character
23+
--> tests/ui/string_slice.rs:17:6
24+
|
25+
LL | &a[0..3];
26+
| ^^^^^^^
27+
28+
error: aborting due to 4 previous errors
2329

0 commit comments

Comments
 (0)