]> git.lizzy.rs Git - rust.git/commitdiff
Suggest `&s` instead of `s.as_str()`
authorPhil Turnbull <philip.turnbull@gmail.com>
Sun, 20 Nov 2016 16:19:36 +0000 (11:19 -0500)
committerPhil Turnbull <philip.turnbull@gmail.com>
Sun, 20 Nov 2016 16:19:36 +0000 (11:19 -0500)
clippy_lints/src/methods.rs
tests/compile-fail/methods.rs

index 9506c8e838ada02be90a27b4781692a3f02b9ce3..d1192e3f26bc67cac3f6b9a173b926c37e3d7b1d 100644 (file)
 /// let def = String::from("def");
 /// let mut s = String::new();
 /// s.push_str(abc);
-/// s.push_str(def.as_str());
+/// s.push_str(&def));
 /// ```
 
 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"]) {
         let target = &arglists[0][0];
         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) {
-            ".as_str()"
+            "&"
         } else {
             return;
         };
@@ -860,8 +860,8 @@ fn lint_string_extend(cx: &LateContext, expr: &hir::Expr, args: &MethodArgs) {
                 db.span_suggestion(expr.span, "try this",
                         format!("{}.push_str({}{})",
                                 snippet(cx, args[0].span, "_"),
-                                snippet(cx, target.span, "_"),
-                                extra_suggestion));
+                                ref_str,
+                                snippet(cx, target.span, "_")));
             });
     }
 }
index 0e30876c3e5c173302736c1c8ee2d18637fe1dc2..0add705d6f8cfad0b4c47b32194e171e135d5316 100644 (file)
@@ -550,11 +550,11 @@ fn str_extend_chars() {
     //~|HELP try this
     //~|SUGGESTION s.push_str("abc")
 
-    s.push_str(def.as_str());
+    s.push_str(&def);
     s.extend(def.chars());
     //~^ERROR calling `.extend(_.chars())`
     //~|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));