Auto merge of #8298 - ebobrow:op_ref_fp, r=giraffate
fix op_ref false positive fixes #7572 changelog: `op_ref` don't lint for unnecessary reference in BinOp impl if removing the reference will lead to unconditional recursion
This commit is contained in:
commit
fff8e78f6d
@ -1,12 +1,16 @@
|
|||||||
use clippy_utils::diagnostics::{multispan_sugg, span_lint, span_lint_and_then};
|
use clippy_utils::diagnostics::{multispan_sugg, span_lint, span_lint_and_then};
|
||||||
|
use clippy_utils::get_enclosing_block;
|
||||||
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace};
|
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace};
|
||||||
use clippy_utils::source::snippet;
|
use clippy_utils::source::snippet;
|
||||||
use clippy_utils::ty::{implements_trait, is_copy};
|
use clippy_utils::ty::{implements_trait, is_copy};
|
||||||
use clippy_utils::{ast_utils::is_useless_with_eq_exprs, eq_expr_value, is_in_test_function};
|
use clippy_utils::{ast_utils::is_useless_with_eq_exprs, eq_expr_value, is_in_test_function};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind};
|
use rustc_hir::{
|
||||||
|
def::Res, def_id::DefId, BinOpKind, BorrowKind, Expr, ExprKind, GenericArg, ItemKind, QPath, Ty, TyKind,
|
||||||
|
};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
|
use rustc_middle::ty::{self, TyS};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
@ -146,6 +150,13 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
|
|||||||
let rty = cx.typeck_results().expr_ty(r);
|
let rty = cx.typeck_results().expr_ty(r);
|
||||||
let lcpy = is_copy(cx, lty);
|
let lcpy = is_copy(cx, lty);
|
||||||
let rcpy = is_copy(cx, rty);
|
let rcpy = is_copy(cx, rty);
|
||||||
|
if let Some((self_ty, other_ty)) = in_impl(cx, e, trait_id) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
// either operator autorefs or both args are copyable
|
// either operator autorefs or both args are copyable
|
||||||
if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
|
if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
@ -206,6 +217,14 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
|
|||||||
// &foo == bar
|
// &foo == bar
|
||||||
(&ExprKind::AddrOf(BorrowKind::Ref, _, l), _) => {
|
(&ExprKind::AddrOf(BorrowKind::Ref, _, l), _) => {
|
||||||
let lty = cx.typeck_results().expr_ty(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);
|
let lcpy = is_copy(cx, lty);
|
||||||
if (requires_ref || lcpy)
|
if (requires_ref || lcpy)
|
||||||
&& implements_trait(cx, lty, trait_id, &[cx.typeck_results().expr_ty(right).into()])
|
&& implements_trait(cx, lty, trait_id, &[cx.typeck_results().expr_ty(right).into()])
|
||||||
@ -230,6 +249,14 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
|
|||||||
// foo == &bar
|
// foo == &bar
|
||||||
(_, &ExprKind::AddrOf(BorrowKind::Ref, _, r)) => {
|
(_, &ExprKind::AddrOf(BorrowKind::Ref, _, r)) => {
|
||||||
let rty = cx.typeck_results().expr_ty(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);
|
let rcpy = is_copy(cx, rty);
|
||||||
if (requires_ref || rcpy)
|
if (requires_ref || rcpy)
|
||||||
&& implements_trait(cx, cx.typeck_results().expr_ty(left), trait_id, &[rty.into()])
|
&& implements_trait(cx, cx.typeck_results().expr_ty(left), trait_id, &[rty.into()])
|
||||||
@ -251,3 +278,43 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn in_impl<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, bin_op: DefId) -> Option<(&'tcx Ty<'tcx>, &'tcx Ty<'tcx>)> {
|
||||||
|
if_chain! {
|
||||||
|
if let Some(block) = get_enclosing_block(cx, e.hir_id);
|
||||||
|
if let Some(impl_def_id) = cx.tcx.impl_of_method(block.hir_id.owner.to_def_id());
|
||||||
|
let item = cx.tcx.hir().expect_item(impl_def_id.expect_local());
|
||||||
|
if let ItemKind::Impl(item) = &item.kind;
|
||||||
|
if let Some(of_trait) = &item.of_trait;
|
||||||
|
if let Some(seg) = of_trait.path.segments.last();
|
||||||
|
if let Some(Res::Def(_, trait_id)) = seg.res;
|
||||||
|
if trait_id == bin_op;
|
||||||
|
if let Some(generic_args) = seg.args;
|
||||||
|
if let Some(GenericArg::Type(other_ty)) = generic_args.args.last();
|
||||||
|
|
||||||
|
then {
|
||||||
|
Some((item.self_ty, other_ty))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn are_equal<'tcx>(cx: &LateContext<'tcx>, middle_ty: &TyS<'_>, hir_ty: &Ty<'_>) -> bool {
|
||||||
|
if_chain! {
|
||||||
|
if let ty::Adt(adt_def, _) = middle_ty.kind();
|
||||||
|
if let Some(local_did) = adt_def.did.as_local();
|
||||||
|
let item = cx.tcx.hir().expect_item(local_did);
|
||||||
|
let middle_ty_id = item.def_id.to_def_id();
|
||||||
|
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind;
|
||||||
|
if let Res::Def(_, hir_ty_id) = path.res;
|
||||||
|
|
||||||
|
then {
|
||||||
|
hir_ty_id == middle_ty_id
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#![allow(unused_variables, clippy::blacklisted_name)]
|
#![allow(unused_variables, clippy::blacklisted_name)]
|
||||||
#![warn(clippy::op_ref)]
|
#![warn(clippy::op_ref)]
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::ops::BitAnd;
|
use std::ops::{BitAnd, Mul};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let tracked_fds: HashSet<i32> = HashSet::new();
|
let tracked_fds: HashSet<i32> = HashSet::new();
|
||||||
@ -55,3 +55,40 @@ fn main() {
|
|||||||
let y = Y(2);
|
let y = Y(2);
|
||||||
let z = x & &y;
|
let z = x & &y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct A(i32);
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct B(i32);
|
||||||
|
|
||||||
|
impl Mul<&A> for B {
|
||||||
|
type Output = i32;
|
||||||
|
fn mul(self, rhs: &A) -> Self::Output {
|
||||||
|
self.0 * rhs.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Mul<A> for B {
|
||||||
|
type Output = i32;
|
||||||
|
fn mul(self, rhs: A) -> Self::Output {
|
||||||
|
// Should not lint because removing the reference would lead to unconditional recursion
|
||||||
|
self * &rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Mul<&A> for A {
|
||||||
|
type Output = i32;
|
||||||
|
fn mul(self, rhs: &A) -> Self::Output {
|
||||||
|
self.0 * rhs.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Mul<A> for A {
|
||||||
|
type Output = i32;
|
||||||
|
fn mul(self, rhs: A) -> Self::Output {
|
||||||
|
let one = B(1);
|
||||||
|
let two = 2;
|
||||||
|
let three = 3;
|
||||||
|
let _ = one * &self;
|
||||||
|
let _ = two + &three;
|
||||||
|
// Removing the reference would lead to unconditional recursion
|
||||||
|
self * &rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -18,5 +18,21 @@ LL | let z = x & &y;
|
|||||||
| |
|
| |
|
||||||
| help: use the right value directly: `y`
|
| help: use the right value directly: `y`
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: taken reference of right operand
|
||||||
|
--> $DIR/op_ref.rs:89:17
|
||||||
|
|
|
||||||
|
LL | let _ = one * &self;
|
||||||
|
| ^^^^^^-----
|
||||||
|
| |
|
||||||
|
| help: use the right value directly: `self`
|
||||||
|
|
||||||
|
error: taken reference of right operand
|
||||||
|
--> $DIR/op_ref.rs:90:17
|
||||||
|
|
|
||||||
|
LL | let _ = two + &three;
|
||||||
|
| ^^^^^^------
|
||||||
|
| |
|
||||||
|
| help: use the right value directly: `three`
|
||||||
|
|
||||||
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user