Skip to content

Commit 8aaaf19

Browse files
committed
Use utils::sugg in methods lints
1 parent c5e91e7 commit 8aaaf19

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

clippy_lints/src/methods.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ use std::fmt;
1111
use syntax::codemap::Span;
1212
use syntax::ptr::P;
1313
use utils::{get_trait_def_id, implements_trait, in_external_macro, in_macro, match_path, match_trait_method,
14-
match_type, method_chain_args, return_ty, same_tys, snippet, snippet_opt, span_lint,
14+
match_type, method_chain_args, return_ty, same_tys, snippet, span_lint,
1515
span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
1616
use utils::MethodArgs;
1717
use utils::paths;
18+
use utils::sugg;
1819

1920
#[derive(Clone)]
2021
pub struct Pass;
@@ -628,8 +629,8 @@ fn lint_clone_double_ref(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr, ty
628629
expr.span,
629630
"using `clone` on a double-reference; \
630631
this will copy the reference instead of cloning the inner type",
631-
|db| if let Some(snip) = snippet_opt(cx, arg.span) {
632-
db.span_suggestion(expr.span, "try dereferencing it", format!("(*{}).clone()", snip));
632+
|db| if let Some(snip) = sugg::Sugg::hir_opt(cx, arg) {
633+
db.span_suggestion(expr.span, "try dereferencing it", format!("({}).clone()", snip.deref()));
633634
});
634635
}
635636
}
@@ -641,14 +642,13 @@ fn lint_extend(cx: &LateContext, expr: &hir::Expr, args: &MethodArgs) {
641642
return;
642643
}
643644
let arg_ty = cx.tcx.expr_ty(&args[1]);
644-
if let Some((span, r)) = derefs_to_slice(cx, &args[1], &arg_ty) {
645+
if let Some(slice) = derefs_to_slice(cx, &args[1], &arg_ty) {
645646
span_lint_and_then(cx, EXTEND_FROM_SLICE, expr.span, "use of `extend` to extend a Vec by a slice", |db| {
646647
db.span_suggestion(expr.span,
647648
"try this",
648-
format!("{}.extend_from_slice({}{})",
649+
format!("{}.extend_from_slice({})",
649650
snippet(cx, args[0].span, "_"),
650-
r,
651-
snippet(cx, span, "_")));
651+
slice));
652652
});
653653
}
654654
}
@@ -695,7 +695,7 @@ fn lint_iter_nth(cx: &LateContext, expr: &hir::Expr, iter_args: &MethodArgs, is_
695695
);
696696
}
697697

698-
fn derefs_to_slice(cx: &LateContext, expr: &hir::Expr, ty: &ty::Ty) -> Option<(Span, &'static str)> {
698+
fn derefs_to_slice(cx: &LateContext, expr: &hir::Expr, ty: &ty::Ty) -> Option<sugg::Sugg<'static>> {
699699
fn may_slice(cx: &LateContext, ty: &ty::Ty) -> bool {
700700
match ty.sty {
701701
ty::TySlice(_) => true,
@@ -706,19 +706,22 @@ fn derefs_to_slice(cx: &LateContext, expr: &hir::Expr, ty: &ty::Ty) -> Option<(S
706706
_ => false,
707707
}
708708
}
709+
709710
if let hir::ExprMethodCall(name, _, ref args) = expr.node {
710711
if &name.node.as_str() == &"iter" && may_slice(cx, &cx.tcx.expr_ty(&args[0])) {
711-
Some((args[0].span, "&"))
712+
sugg::Sugg::hir_opt(cx, &*args[0]).map(|sugg| {
713+
sugg.addr()
714+
})
712715
} else {
713716
None
714717
}
715718
} else {
716719
match ty.sty {
717-
ty::TySlice(_) => Some((expr.span, "")),
720+
ty::TySlice(_) => sugg::Sugg::hir_opt(cx, expr),
718721
ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) |
719722
ty::TyBox(ref inner) => {
720723
if may_slice(cx, inner) {
721-
Some((expr.span, ""))
724+
sugg::Sugg::hir_opt(cx, expr)
722725
} else {
723726
None
724727
}

0 commit comments

Comments
 (0)