Fix suggestion for multiple derefs
This commit is contained in:
parent
a2be050965
commit
2a9dec681f
3 changed files with 52 additions and 3 deletions
|
|
@ -1250,7 +1250,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp
|
|||
if is_copy(cx, ty) {
|
||||
let snip;
|
||||
if let Some(snippet) = sugg::Sugg::hir_opt(cx, arg) {
|
||||
// x.clone() might have dereferenced x, possibly through a Deref impl
|
||||
// x.clone() might have dereferenced x, possibly through Deref impls
|
||||
if cx.tables.expr_ty(arg) != ty {
|
||||
let parent = cx.tcx.hir.get_parent_node(expr.id);
|
||||
match cx.tcx.hir.get(parent) {
|
||||
|
|
@ -1273,7 +1273,18 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp
|
|||
},
|
||||
_ => {},
|
||||
}
|
||||
snip = Some(("try dereferencing it", format!("{}", snippet.deref())));
|
||||
|
||||
let deref_count = cx.tables.expr_adjustments(arg).iter()
|
||||
.filter(|adj| {
|
||||
if let ty::adjustment::Adjust::Deref(_) = adj.kind {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
.count();
|
||||
let derefs: String = iter::repeat('*').take(deref_count).collect();
|
||||
snip = Some(("try dereferencing it", format!("{}{}", derefs, snippet)));
|
||||
} else {
|
||||
snip = Some(("try removing the `clone` call", format!("{}", snippet)));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue