Skip to content

Commit 71ddf81

Browse files
committed
Auto merge of rust-lang#9741 - llogiq:fix-string-extend-chars-slice-ref, r=Manishearth
fix the `string-extend-chars` suggestion on slice This adds the missing `&` to the suggestion if the target is a `str` slice (e.g. extending with `"foo"[..].chars()`). This closes rust-lang#9735. --- changelog: fix the `string-extend-chars` suggestion for `str` slices
2 parents 33137dd + 7e68c71 commit 71ddf81

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

clippy_lints/src/methods/string_extend_chars.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
1919
let target = &arglists[0].0;
2020
let self_ty = cx.typeck_results().expr_ty(target).peel_refs();
2121
let ref_str = if *self_ty.kind() == ty::Str {
22-
""
22+
if matches!(target.kind, hir::ExprKind::Index(..)) {
23+
"&"
24+
} else {
25+
""
26+
}
2327
} else if is_type_diagnostic_item(cx, self_ty, sym::String) {
2428
"&"
2529
} else {

tests/ui/string_extend.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ fn main() {
2929

3030
let f = HasChars;
3131
s.extend(f.chars());
32+
33+
// issue #9735
34+
s.push_str(&abc[0..2]);
3235
}

tests/ui/string_extend.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ fn main() {
2929

3030
let f = HasChars;
3131
s.extend(f.chars());
32+
33+
// issue #9735
34+
s.extend(abc[0..2].chars());
3235
}

tests/ui/string_extend.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ error: calling `.extend(_.chars())`
1818
LL | s.extend(def.chars());
1919
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&def)`
2020

21-
error: aborting due to 3 previous errors
21+
error: calling `.extend(_.chars())`
22+
--> $DIR/string_extend.rs:34:5
23+
|
24+
LL | s.extend(abc[0..2].chars());
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&abc[0..2])`
26+
27+
error: aborting due to 4 previous errors
2228

0 commit comments

Comments
 (0)