Suggest `&s` instead of `s.as_str()`
This commit is contained in:
parent
73a73638c0
commit
e9f3911899
|
@ -511,7 +511,7 @@ declare_lint! {
|
||||||
/// let def = String::from("def");
|
/// let def = String::from("def");
|
||||||
/// let mut s = String::new();
|
/// let mut s = String::new();
|
||||||
/// s.push_str(abc);
|
/// s.push_str(abc);
|
||||||
/// s.push_str(def.as_str());
|
/// s.push_str(&def));
|
||||||
/// ```
|
/// ```
|
||||||
|
|
||||||
declare_lint! {
|
declare_lint! {
|
||||||
|
@ -843,10 +843,10 @@ fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &MethodArgs) {
|
||||||
if let Some(arglists) = method_chain_args(arg, &["chars"]) {
|
if let Some(arglists) = method_chain_args(arg, &["chars"]) {
|
||||||
let target = &arglists[0][0];
|
let target = &arglists[0][0];
|
||||||
let (self_ty, _) = walk_ptrs_ty_depth(cx.tcx.tables().expr_ty(target));
|
let (self_ty, _) = walk_ptrs_ty_depth(cx.tcx.tables().expr_ty(target));
|
||||||
let extra_suggestion = if self_ty.sty == ty::TyStr {
|
let ref_str = if self_ty.sty == ty::TyStr {
|
||||||
""
|
""
|
||||||
} else if match_type(cx, self_ty, &paths::STRING) {
|
} else if match_type(cx, self_ty, &paths::STRING) {
|
||||||
".as_str()"
|
"&"
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
@ -860,8 +860,8 @@ fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &MethodArgs) {
|
||||||
db.span_suggestion(expr.span, "try this",
|
db.span_suggestion(expr.span, "try this",
|
||||||
format!("{}.push_str({}{})",
|
format!("{}.push_str({}{})",
|
||||||
snippet(cx, args[0].span, "_"),
|
snippet(cx, args[0].span, "_"),
|
||||||
snippet(cx, target.span, "_"),
|
ref_str,
|
||||||
extra_suggestion));
|
snippet(cx, target.span, "_")));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -550,11 +550,11 @@ fn str_extend_chars() {
|
||||||
//~|HELP try this
|
//~|HELP try this
|
||||||
//~|SUGGESTION s.push_str("abc")
|
//~|SUGGESTION s.push_str("abc")
|
||||||
|
|
||||||
s.push_str(def.as_str());
|
s.push_str(&def);
|
||||||
s.extend(def.chars());
|
s.extend(def.chars());
|
||||||
//~^ERROR calling `.extend(_.chars())`
|
//~^ERROR calling `.extend(_.chars())`
|
||||||
//~|HELP try this
|
//~|HELP try this
|
||||||
//~|SUGGESTION s.push_str(def.as_str())
|
//~|SUGGESTION s.push_str(&def)
|
||||||
|
|
||||||
s.extend(abc.chars().skip(1));
|
s.extend(abc.chars().skip(1));
|
||||||
s.extend("abc".chars().skip(1));
|
s.extend("abc".chars().skip(1));
|
||||||
|
|
Loading…
Reference in New Issue