Skip to content

Commit e9f3911

Browse files
Suggest &s instead of s.as_str()
1 parent 73a7363 commit e9f3911

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

clippy_lints/src/methods.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ declare_lint! {
511511
/// let def = String::from("def");
512512
/// let mut s = String::new();
513513
/// s.push_str(abc);
514-
/// s.push_str(def.as_str());
514+
/// s.push_str(&def));
515515
/// ```
516516
517517
declare_lint! {
@@ -843,10 +843,10 @@ fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &MethodArgs) {
843843
if let Some(arglists) = method_chain_args(arg, &["chars"]) {
844844
let target = &arglists[0][0];
845845
let (self_ty, _) = walk_ptrs_ty_depth(cx.tcx.tables().expr_ty(target));
846-
let extra_suggestion = if self_ty.sty == ty::TyStr {
846+
let ref_str = if self_ty.sty == ty::TyStr {
847847
""
848848
} else if match_type(cx, self_ty, &paths::STRING) {
849-
".as_str()"
849+
"&"
850850
} else {
851851
return;
852852
};
@@ -860,8 +860,8 @@ fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &MethodArgs) {
860860
db.span_suggestion(expr.span, "try this",
861861
format!("{}.push_str({}{})",
862862
snippet(cx, args[0].span, "_"),
863-
snippet(cx, target.span, "_"),
864-
extra_suggestion));
863+
ref_str,
864+
snippet(cx, target.span, "_")));
865865
});
866866
}
867867
}

tests/compile-fail/methods.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,11 @@ fn str_extend_chars() {
550550
//~|HELP try this
551551
//~|SUGGESTION s.push_str("abc")
552552

553-
s.push_str(def.as_str());
553+
s.push_str(&def);
554554
s.extend(def.chars());
555555
//~^ERROR calling `.extend(_.chars())`
556556
//~|HELP try this
557-
//~|SUGGESTION s.push_str(def.as_str())
557+
//~|SUGGESTION s.push_str(&def)
558558

559559
s.extend(abc.chars().skip(1));
560560
s.extend("abc".chars().skip(1));

0 commit comments

Comments
 (0)