Improve needless_pass_by_value suggestion

This commit is contained in:
Guillaume Gomez 2024-12-26 12:00:57 +01:00
parent f83c94cb3a
commit 79013f813d
3 changed files with 24 additions and 3 deletions

View file

@ -279,10 +279,18 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
}
}
let suggestion = if is_type_diagnostic_item(cx, ty, sym::Option)
&& let snip = snippet(cx, input.span, "_")
&& let Some((first, rest)) = snip.split_once('<')
{
format!("{first}<&{rest}")
} else {
format!("&{}", snippet(cx, input.span, "_"))
};
diag.span_suggestion(
input.span,
"consider taking a reference instead",
format!("&{}", snippet(cx, input.span, "_")),
suggestion,
Applicability::MaybeIncorrect,
);
};