include a ref if argument is not just a numeric literal

This commit is contained in:
y21 2023-06-17 20:48:56 +02:00
parent 3217f8aeaa
commit 6331c943ff
4 changed files with 84 additions and 9 deletions

View file

@ -25,13 +25,13 @@ pub(super) fn check<'tcx>(
let get_args_str = snippet_with_applicability(cx, get_arg.span, "..", &mut applicability);
let mut needs_ref;
let caller_type = if derefs_to_slice(cx, recv, expr_ty).is_some() {
needs_ref = get_args_str.parse::<usize>().is_ok();
needs_ref = true;
"slice"
} else if is_type_diagnostic_item(cx, expr_ty, sym::Vec) {
needs_ref = get_args_str.parse::<usize>().is_ok();
needs_ref = true;
"Vec"
} else if is_type_diagnostic_item(cx, expr_ty, sym::VecDeque) {
needs_ref = get_args_str.parse::<usize>().is_ok();
needs_ref = true;
"VecDeque"
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym::HashMap) {
needs_ref = true;
@ -45,16 +45,23 @@ pub(super) fn check<'tcx>(
let mut span = expr.span;
// Handle the case where the result is immediately dereferenced
// by not requiring ref and pulling the dereference into the
// suggestion.
// Handle the case where the result is immediately dereferenced,
// either directly be the user, or as a result of a method call or the like
// by not requiring an explicit reference
if_chain! {
if needs_ref;
if let Some(parent) = get_parent_expr(cx, expr);
if let hir::ExprKind::Unary(hir::UnOp::Deref, _) = parent.kind;
if let hir::ExprKind::Unary(hir::UnOp::Deref, _)
| hir::ExprKind::MethodCall(..)
| hir::ExprKind::Field(..)
| hir::ExprKind::Index(..) = parent.kind;
then {
if let hir::ExprKind::Unary(hir::UnOp::Deref, _) = parent.kind {
// if the user explicitly dereferences the result, we can adjust
// the span to also include the deref part
span = parent.span;
}
needs_ref = false;
span = parent.span;
}
}