]> git.proxmox.com Git - rustc.git/blobdiff - src/tools/clippy/clippy_lints/src/methods/string_extend_chars.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / string_extend_chars.rs
index 6e7890a3080e60a0b99838e13c83a1fe70af04dc..f35d81cee8e97f969bb8b16c04ff39b494270704 100644 (file)
@@ -1,26 +1,29 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
 use clippy_utils::method_chain_args;
 use clippy_utils::source::snippet_with_applicability;
-use clippy_utils::ty::is_type_diagnostic_item;
+use clippy_utils::ty::is_type_lang_item;
 use rustc_errors::Applicability;
 use rustc_hir as hir;
 use rustc_lint::LateContext;
 use rustc_middle::ty;
-use rustc_span::symbol::sym;
 
 use super::STRING_EXTEND_CHARS;
 
 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
     let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs();
-    if !is_type_diagnostic_item(cx, obj_ty, sym::string_type) {
+    if !is_type_lang_item(cx, obj_ty, hir::LangItem::String) {
         return;
     }
     if let Some(arglists) = method_chain_args(arg, &["chars"]) {
-        let target = &arglists[0][0];
+        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 {
-            ""
-        } else if is_type_diagnostic_item(cx, self_ty, sym::string_type) {
+            if matches!(target.kind, hir::ExprKind::Index(..)) {
+                "&"
+            } else {
+                ""
+            }
+        } else if is_type_lang_item(cx, self_ty, hir::LangItem::String) {
             "&"
         } else {
             return;
@@ -34,9 +37,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
             "calling `.extend(_.chars())`",
             "try this",
             format!(
-                "{}.push_str({}{})",
+                "{}.push_str({ref_str}{})",
                 snippet_with_applicability(cx, recv.span, "..", &mut applicability),
-                ref_str,
                 snippet_with_applicability(cx, target.span, "..", &mut applicability)
             ),
             applicability,