Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c07c046

Browse files
committed
refactor string_extend_chars: return when obj type is not string
1 parent 058d8c8 commit c07c046

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

clippy_lints/src/methods/string_extend_chars.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,35 @@ use super::STRING_EXTEND_CHARS;
1212

1313
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
1414
let obj_ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
15-
if is_type_diagnostic_item(cx, obj_ty, sym::string_type) {
16-
let arg = &args[1];
17-
if let Some(arglists) = method_chain_args(arg, &["chars"]) {
18-
let target = &arglists[0][0];
19-
let self_ty = cx.typeck_results().expr_ty(target).peel_refs();
20-
let ref_str = if *self_ty.kind() == ty::Str {
21-
""
22-
} else if is_type_diagnostic_item(cx, self_ty, sym::string_type) {
23-
"&"
24-
} else {
25-
return;
26-
};
15+
if !is_type_diagnostic_item(cx, obj_ty, sym::string_type) {
16+
return;
17+
}
18+
let arg = &args[1];
19+
if let Some(arglists) = method_chain_args(arg, &["chars"]) {
20+
let target = &arglists[0][0];
21+
let self_ty = cx.typeck_results().expr_ty(target).peel_refs();
22+
let ref_str = if *self_ty.kind() == ty::Str {
23+
""
24+
} else if is_type_diagnostic_item(cx, self_ty, sym::string_type) {
25+
"&"
26+
} else {
27+
return;
28+
};
2729

28-
let mut applicability = Applicability::MachineApplicable;
29-
span_lint_and_sugg(
30-
cx,
31-
STRING_EXTEND_CHARS,
32-
expr.span,
33-
"calling `.extend(_.chars())`",
34-
"try this",
35-
format!(
36-
"{}.push_str({}{})",
37-
snippet_with_applicability(cx, args[0].span, "..", &mut applicability),
38-
ref_str,
39-
snippet_with_applicability(cx, target.span, "..", &mut applicability)
40-
),
41-
applicability,
42-
);
43-
}
30+
let mut applicability = Applicability::MachineApplicable;
31+
span_lint_and_sugg(
32+
cx,
33+
STRING_EXTEND_CHARS,
34+
expr.span,
35+
"calling `.extend(_.chars())`",
36+
"try this",
37+
format!(
38+
"{}.push_str({}{})",
39+
snippet_with_applicability(cx, args[0].span, "..", &mut applicability),
40+
ref_str,
41+
snippet_with_applicability(cx, target.span, "..", &mut applicability)
42+
),
43+
applicability,
44+
);
4445
}
4546
}

0 commit comments

Comments
 (0)