Improve needless_pass_by_value suggestion (#13880)

Fixes https://github.com/rust-lang/rust-clippy/issues/13744.

A simple check to check if the type is an `Option` allows to improve the
suggestion.

changelog: Improve `needless_pass_by_value` suggestion
This commit is contained in:
Philipp Krones 2025-03-10 15:48:43 +00:00 committed by GitHub
commit a25cbd50ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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,
);
};