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 commit is contained in:
Andre Bogus 2022-10-28 17:22:40 +02:00
parent 33137dd612
commit 7e68c718c0
4 changed files with 18 additions and 2 deletions

View File

@ -19,7 +19,11 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
let target = &arglists[0].0; let target = &arglists[0].0;
let self_ty = cx.typeck_results().expr_ty(target).peel_refs(); let self_ty = cx.typeck_results().expr_ty(target).peel_refs();
let ref_str = if *self_ty.kind() == ty::Str { let ref_str = if *self_ty.kind() == ty::Str {
"" if matches!(target.kind, hir::ExprKind::Index(..)) {
"&"
} else {
""
}
} else if is_type_diagnostic_item(cx, self_ty, sym::String) { } else if is_type_diagnostic_item(cx, self_ty, sym::String) {
"&" "&"
} else { } else {

View File

@ -29,4 +29,7 @@ fn main() {
let f = HasChars; let f = HasChars;
s.extend(f.chars()); s.extend(f.chars());
// issue #9735
s.push_str(&abc[0..2]);
} }

View File

@ -29,4 +29,7 @@ fn main() {
let f = HasChars; let f = HasChars;
s.extend(f.chars()); s.extend(f.chars());
// issue #9735
s.extend(abc[0..2].chars());
} }

View File

@ -18,5 +18,11 @@ error: calling `.extend(_.chars())`
LL | s.extend(def.chars()); LL | s.extend(def.chars());
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&def)` | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&def)`
error: aborting due to 3 previous errors error: calling `.extend(_.chars())`
--> $DIR/string_extend.rs:34:5
|
LL | s.extend(abc[0..2].chars());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&abc[0..2])`
error: aborting due to 4 previous errors