Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
315 changes: 173 additions & 142 deletions clippy_lints/src/operators/op_ref.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::get_enclosing_block;
use clippy_utils::source::snippet_with_context;
use clippy_utils::source::walk_span_to_context;
use clippy_utils::ty::{implements_trait, is_copy};
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::def_id::DefId;
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, GenericArg, ItemKind, QPath, TyKind};
use rustc_hir::{self as hir, BinOpKind, BorrowKind, Expr, ExprKind, GenericArg, ItemKind, QPath, TyKind};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};
use rustc_middle::ty::Ty;

use super::OP_REF;

Expand Down Expand Up @@ -37,139 +37,146 @@ pub(crate) fn check<'tcx>(
(cx.tcx.lang_items().partial_ord_trait(), true)
},
};
if let Some(trait_id) = trait_id {
match (&left.kind, &right.kind) {
// do not suggest to dereference literals
(&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
// &foo == &bar
(&ExprKind::AddrOf(BorrowKind::Ref, _, l), &ExprKind::AddrOf(BorrowKind::Ref, _, r)) => {
let lty = cx.typeck_results().expr_ty(l);
let rty = cx.typeck_results().expr_ty(r);
let lcpy = is_copy(cx, lty);
let rcpy = is_copy(cx, rty);
if let Some((self_ty, other_ty)) = in_impl(cx, e, trait_id)
&& ((are_equal(cx, rty, self_ty) && are_equal(cx, lty, other_ty))
|| (are_equal(cx, rty, other_ty) && are_equal(cx, lty, self_ty)))
{
return; // Don't lint
}
// either operator autorefs or both args are copyable
if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
span_lint_and_then(
cx,
OP_REF,
e.span,
"needlessly taken reference of both operands",
|diag| {
let mut applicability = Applicability::MachineApplicable;
let (lsnip, _) = snippet_with_context(cx, l.span, e.span.ctxt(), "...", &mut applicability);
let (rsnip, _) = snippet_with_context(cx, r.span, e.span.ctxt(), "...", &mut applicability);
diag.multipart_suggestion(
"use the values directly",
vec![(left.span, lsnip.to_string()), (right.span, rsnip.to_string())],
applicability,
);
},
);
} else if lcpy
&& !rcpy
&& implements_trait(cx, lty, trait_id, &[cx.typeck_results().expr_ty(right).into()])
{
span_lint_and_then(
cx,
OP_REF,
e.span,
"needlessly taken reference of left operand",
|diag| {
let mut applicability = Applicability::MachineApplicable;
let (lsnip, _) = snippet_with_context(cx, l.span, e.span.ctxt(), "...", &mut applicability);
diag.span_suggestion(left.span, "use the left value directly", lsnip, applicability);
},
);
} else if !lcpy
&& rcpy
&& implements_trait(cx, cx.typeck_results().expr_ty(left), trait_id, &[rty.into()])
{
span_lint_and_then(
cx,
OP_REF,
e.span,
"needlessly taken reference of right operand",
|diag| {
let mut applicability = Applicability::MachineApplicable;
let (rsnip, _) = snippet_with_context(cx, r.span, e.span.ctxt(), "...", &mut applicability);
diag.span_suggestion(
right.span,
"use the right value directly",
rsnip,
Applicability::MachineApplicable,
);
},
);
}
},
// &foo == bar
(&ExprKind::AddrOf(BorrowKind::Ref, _, l), _) => {
let lty = cx.typeck_results().expr_ty(l);
if let Some((self_ty, other_ty)) = in_impl(cx, e, trait_id) {
let rty = cx.typeck_results().expr_ty(right);
if (are_equal(cx, rty, self_ty) && are_equal(cx, lty, other_ty))
|| (are_equal(cx, rty, other_ty) && are_equal(cx, lty, self_ty))
{
return; // Don't lint
}
}
let lcpy = is_copy(cx, lty);
if (requires_ref || lcpy)
&& implements_trait(cx, lty, trait_id, &[cx.typeck_results().expr_ty(right).into()])
{
span_lint_and_then(
cx,
OP_REF,
e.span,
"needlessly taken reference of left operand",
|diag| {
let mut applicability = Applicability::MachineApplicable;
let (lsnip, _) = snippet_with_context(cx, l.span, e.span.ctxt(), "...", &mut applicability);
diag.span_suggestion(
left.span,
"use the left value directly",
lsnip,
Applicability::MachineApplicable,
);
},
);
}
},
// foo == &bar
(_, &ExprKind::AddrOf(BorrowKind::Ref, _, r)) => {
let rty = cx.typeck_results().expr_ty(r);
if let Some((self_ty, other_ty)) = in_impl(cx, e, trait_id) {
let lty = cx.typeck_results().expr_ty(left);
if (are_equal(cx, rty, self_ty) && are_equal(cx, lty, other_ty))
|| (are_equal(cx, rty, other_ty) && are_equal(cx, lty, self_ty))
{
return; // Don't lint
}
}
let rcpy = is_copy(cx, rty);
if (requires_ref || rcpy)
&& implements_trait(cx, cx.typeck_results().expr_ty(left), trait_id, &[rty.into()])
{
span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |diag| {
let mut applicability = Applicability::MachineApplicable;
let (rsnip, _) = snippet_with_context(cx, r.span, e.span.ctxt(), "...", &mut applicability);
diag.span_suggestion(
right.span,
let Some(trait_id) = trait_id else {
return;
};

let left_ty = cx.typeck_results().expr_ty(left);
let right_ty = cx.typeck_results().expr_ty(right);
match (left.kind, right.kind) {
// do not suggest to dereference literals
(ExprKind::Lit(..), _) | (_, ExprKind::Lit(..)) => {},
// &foo == &bar
(ExprKind::AddrOf(BorrowKind::Ref, _, l), ExprKind::AddrOf(BorrowKind::Ref, _, r)) => {
let lty = cx.typeck_results().expr_ty(l);
let rty = cx.typeck_results().expr_ty(r);
let lcpy = is_copy(cx, lty);
let rcpy = is_copy(cx, rty);
if let Some((self_ty, other_ty)) = in_impl(cx, e, trait_id)
&& are_pairwise_equal(cx, lty, rty, self_ty, other_ty)
{
return; // Don't lint
}
// either operator autorefs or both args are copyable
if (requires_ref || (lcpy && rcpy))
&& implements_trait(cx, lty, trait_id, &[rty.into()])
&& let Some(l_span) = walk_span_to_context(l.span, e.span.ctxt())
&& let Some(r_span) = walk_span_to_context(r.span, e.span.ctxt())
{
span_lint_and_then(
cx,
OP_REF,
e.span,
"needlessly taken reference of both operands",
|diag| {
let applicability = Applicability::MachineApplicable;
diag.multipart_suggestion(
"use the values directly",
vec![
(left.span.until(l_span), String::new()),
(right.span.until(r_span), String::new()),
],
applicability,
);
},
);
} else if lcpy
&& !rcpy
&& implements_trait(cx, lty, trait_id, &[right_ty.into()])
&& let Some(l_span) = walk_span_to_context(l.span, e.span.ctxt())
{
span_lint_and_then(
cx,
OP_REF,
e.span,
"needlessly taken reference of left operand",
|diag| {
let applicability = Applicability::MachineApplicable;
diag.span_suggestion_verbose(
left.span.until(l_span),
"use the left value directly",
String::new(),
applicability,
);
},
);
} else if !lcpy
&& rcpy
&& implements_trait(cx, left_ty, trait_id, &[rty.into()])
&& let Some(r_span) = walk_span_to_context(r.span, e.span.ctxt())
{
span_lint_and_then(
cx,
OP_REF,
e.span,
"needlessly taken reference of right operand",
|diag| {
let applicability = Applicability::MachineApplicable;
diag.span_suggestion_verbose(
right.span.until(r_span),
"use the right value directly",
rsnip,
Applicability::MachineApplicable,
String::new(),
applicability,
);
});
}
},
_ => {},
}
},
);
}
},
// &foo == bar
(ExprKind::AddrOf(BorrowKind::Ref, _, l), _) => {
let lty = cx.typeck_results().expr_ty(l);
if let Some((self_ty, other_ty)) = in_impl(cx, e, trait_id)
&& are_pairwise_equal(cx, lty, right_ty, self_ty, other_ty)
{
return; // Don't lint
}
let lcpy = is_copy(cx, lty);
if (requires_ref || lcpy)
&& implements_trait(cx, lty, trait_id, &[right_ty.into()])
&& let Some(l_span) = walk_span_to_context(l.span, e.span.ctxt())
{
span_lint_and_then(
cx,
OP_REF,
e.span,
"needlessly taken reference of left operand",
|diag| {
let applicability = Applicability::MachineApplicable;
diag.span_suggestion_verbose(
left.span.until(l_span),
"use the left value directly",
String::new(),
applicability,
);
},
);
}
},
// foo == &bar
(_, ExprKind::AddrOf(BorrowKind::Ref, _, r)) => {
let rty = cx.typeck_results().expr_ty(r);
if let Some((self_ty, other_ty)) = in_impl(cx, e, trait_id)
&& are_pairwise_equal(cx, left_ty, rty, self_ty, other_ty)
{
return; // Don't lint
}
let rcpy = is_copy(cx, rty);
if (requires_ref || rcpy)
&& implements_trait(cx, left_ty, trait_id, &[rty.into()])
&& let Some(r_span) = walk_span_to_context(r.span, e.span.ctxt())
{
span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |diag| {
let applicability = Applicability::MachineApplicable;
diag.span_suggestion_verbose(
right.span.until(r_span),
"use the right value directly",
String::new(),
applicability,
);
});
}
},
_ => {},
}
}

Expand All @@ -196,15 +203,39 @@ fn in_impl<'tcx>(
}
}

fn are_equal(cx: &LateContext<'_>, middle_ty: Ty<'_>, hir_ty: &rustc_hir::Ty<'_>) -> bool {
if let ty::Adt(adt_def, _) = middle_ty.kind()
&& let Some(local_did) = adt_def.did().as_local()
&& let item = cx.tcx.hir_expect_item(local_did)
&& let middle_ty_id = item.owner_id.to_def_id()
&& let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind
&& let Res::Def(_, hir_ty_id) = path.res
/// ```ignore
/// (ty1 == hir_ty1 && ty2 == hir_ty2)
/// || (ty1 == hir_ty2 && ty2 == hir_ty1)
/// ```
fn are_pairwise_equal(
cx: &LateContext<'_>,
ty1: Ty<'_>,
ty2: Ty<'_>,
hir_ty1: &hir::Ty<'_>,
hir_ty2: &hir::Ty<'_>,
) -> bool {
fn middle_ty_did(cx: &LateContext<'_>, ty: Ty<'_>) -> Option<DefId> {
let local_did = ty.ty_adt_def()?.did().as_local()?;
let item = cx.tcx.hir_expect_item(local_did);
Some(item.owner_id.to_def_id())
}

fn hir_ty_did(hir_ty: &hir::Ty<'_>) -> Option<DefId> {
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind
&& let Res::Def(_, hir_ty_id) = path.res
{
Some(hir_ty_id)
} else {
None
}
}

if let Some(ty1_did) = middle_ty_did(cx, ty1)
&& let Some(ty2_did) = middle_ty_did(cx, ty2)
&& let Some(hir_ty1_did) = hir_ty_did(hir_ty1)
&& let Some(hir_ty2_did) = hir_ty_did(hir_ty2)
{
hir_ty_id == middle_ty_id
(ty1_did == hir_ty1_did && ty2_did == hir_ty2_did) || (ty2_did == hir_ty1_did && ty1_did == hir_ty2_did)
} else {
false
}
Expand Down
Loading