diff options
| author | Andre Bogus <bogusandre@gmail.com> | 2022-10-28 17:22:40 +0200 |
|---|---|---|
| committer | Andre Bogus <bogusandre@gmail.com> | 2022-10-28 17:35:44 +0200 |
| commit | 7e68c718c0db31c317c77cf776441d695f715f41 (patch) | |
| tree | f25f308b755aa26406a473dc25ae4901c69d2e49 | |
| parent | 33137dd612cab59026bc5c0469b8761b198e3cae (diff) | |
| download | rust-7e68c718c0db31c317c77cf776441d695f715f41.tar.gz rust-7e68c718c0db31c317c77cf776441d695f715f41.zip | |
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()`).
| -rw-r--r-- | clippy_lints/src/methods/string_extend_chars.rs | 6 | ||||
| -rw-r--r-- | tests/ui/string_extend.fixed | 3 | ||||
| -rw-r--r-- | tests/ui/string_extend.rs | 3 | ||||
| -rw-r--r-- | tests/ui/string_extend.stderr | 8 |
4 files changed, 18 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/string_extend_chars.rs b/clippy_lints/src/methods/string_extend_chars.rs index 6974260f70d..0e93543fd7e 100644 --- a/clippy_lints/src/methods/string_extend_chars.rs +++ b/clippy_lints/src/methods/string_extend_chars.rs @@ -19,7 +19,11 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr let target = &arglists[0].0; let self_ty = cx.typeck_results().expr_ty(target).peel_refs(); 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 { diff --git a/tests/ui/string_extend.fixed b/tests/ui/string_extend.fixed index 1883a9f8325..d200d7310fc 100644 --- a/tests/ui/string_extend.fixed +++ b/tests/ui/string_extend.fixed @@ -29,4 +29,7 @@ fn main() { let f = HasChars; s.extend(f.chars()); + + // issue #9735 + s.push_str(&abc[0..2]); } diff --git a/tests/ui/string_extend.rs b/tests/ui/string_extend.rs index 07d0baa1be6..0dd96a3b210 100644 --- a/tests/ui/string_extend.rs +++ b/tests/ui/string_extend.rs @@ -29,4 +29,7 @@ fn main() { let f = HasChars; s.extend(f.chars()); + + // issue #9735 + s.extend(abc[0..2].chars()); } diff --git a/tests/ui/string_extend.stderr b/tests/ui/string_extend.stderr index 6af8c9e1662..b35c77fd961 100644 --- a/tests/ui/string_extend.stderr +++ b/tests/ui/string_extend.stderr @@ -18,5 +18,11 @@ error: calling `.extend(_.chars())` LL | s.extend(def.chars()); | ^^^^^^^^^^^^^^^^^^^^^ 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 |
