]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/string_extend_chars.rs
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / string_extend_chars.rs
CommitLineData
cdc7bbd5
XL
1use clippy_utils::diagnostics::span_lint_and_sugg;
2use clippy_utils::method_chain_args;
3use clippy_utils::source::snippet_with_applicability;
487cf647 4use clippy_utils::ty::is_type_lang_item;
f20569fa
XL
5use rustc_errors::Applicability;
6use rustc_hir as hir;
7use rustc_lint::LateContext;
f20569fa
XL
8
9use super::STRING_EXTEND_CHARS;
10
cdc7bbd5
XL
11pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
12 let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs();
487cf647 13 if !is_type_lang_item(cx, obj_ty, hir::LangItem::String) {
cdc7bbd5
XL
14 return;
15 }
16 if let Some(arglists) = method_chain_args(arg, &["chars"]) {
f2b60f7d 17 let target = &arglists[0].0;
cdc7bbd5 18 let self_ty = cx.typeck_results().expr_ty(target).peel_refs();
9ffffee4 19 let ref_str = if self_ty.is_str() {
487cf647
FG
20 if matches!(target.kind, hir::ExprKind::Index(..)) {
21 "&"
22 } else {
23 ""
24 }
25 } else if is_type_lang_item(cx, self_ty, hir::LangItem::String) {
cdc7bbd5
XL
26 "&"
27 } else {
28 return;
29 };
f20569fa 30
cdc7bbd5
XL
31 let mut applicability = Applicability::MachineApplicable;
32 span_lint_and_sugg(
33 cx,
34 STRING_EXTEND_CHARS,
35 expr.span,
36 "calling `.extend(_.chars())`",
add651ee 37 "try",
cdc7bbd5 38 format!(
2b03887a 39 "{}.push_str({ref_str}{})",
cdc7bbd5 40 snippet_with_applicability(cx, recv.span, "..", &mut applicability),
cdc7bbd5
XL
41 snippet_with_applicability(cx, target.span, "..", &mut applicability)
42 ),
43 applicability,
44 );
f20569fa
XL
45 }
46}