]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 XL |
1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
2 | use clippy_utils::method_chain_args; | |
3 | use clippy_utils::source::snippet_with_applicability; | |
4 | use clippy_utils::ty::is_type_diagnostic_item; | |
f20569fa XL |
5 | use rustc_errors::Applicability; |
6 | use rustc_hir as hir; | |
7 | use rustc_lint::LateContext; | |
8 | use rustc_middle::ty; | |
9 | use rustc_span::symbol::sym; | |
10 | ||
11 | use super::STRING_EXTEND_CHARS; | |
12 | ||
cdc7bbd5 XL |
13 | pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) { |
14 | let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs(); | |
c295e0f8 | 15 | if !is_type_diagnostic_item(cx, obj_ty, sym::String) { |
cdc7bbd5 XL |
16 | return; |
17 | } | |
18 | if let Some(arglists) = method_chain_args(arg, &["chars"]) { | |
f2b60f7d | 19 | let target = &arglists[0].0; |
cdc7bbd5 XL |
20 | let self_ty = cx.typeck_results().expr_ty(target).peel_refs(); |
21 | let ref_str = if *self_ty.kind() == ty::Str { | |
22 | "" | |
c295e0f8 | 23 | } else if is_type_diagnostic_item(cx, self_ty, sym::String) { |
cdc7bbd5 XL |
24 | "&" |
25 | } else { | |
26 | return; | |
27 | }; | |
f20569fa | 28 | |
cdc7bbd5 XL |
29 | let mut applicability = Applicability::MachineApplicable; |
30 | span_lint_and_sugg( | |
31 | cx, | |
32 | STRING_EXTEND_CHARS, | |
33 | expr.span, | |
34 | "calling `.extend(_.chars())`", | |
35 | "try this", | |
36 | format!( | |
37 | "{}.push_str({}{})", | |
38 | snippet_with_applicability(cx, recv.span, "..", &mut applicability), | |
39 | ref_str, | |
40 | snippet_with_applicability(cx, target.span, "..", &mut applicability) | |
41 | ), | |
42 | applicability, | |
43 | ); | |
f20569fa XL |
44 | } |
45 | } |