Don't lint cmp_owned when From::from results in a copy type.

This commit is contained in:
Jason Newcomb 2022-05-09 12:29:31 -04:00
parent 0f1544f15e
commit 993b4016db
3 changed files with 29 additions and 3 deletions

View File

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
use clippy_utils::source::{snippet, snippet_opt};
use clippy_utils::ty::implements_trait;
use clippy_utils::ty::{implements_trait, is_copy};
use if_chain::if_chain;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
@ -584,7 +584,11 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
ExprKind::Call(path, [arg])
if path_def_id(cx, path)
.and_then(|id| match_any_def_paths(cx, id, &[&paths::FROM_STR_METHOD, &paths::FROM_FROM]))
.is_some() =>
.map_or(false, |idx| match idx {
0 => true,
1 => !is_copy(cx, typeck.expr_ty(expr)),
_ => false,
}) =>
{
(arg, arg.span)
},

View File

@ -9,6 +9,10 @@ fn main() {
let x = &&Baz;
let y = &Baz;
y.to_owned() == **x;
let x = 0u32;
let y = U32Wrapper(x);
let _ = U32Wrapper::from(x) == y;
}
struct Foo;
@ -51,3 +55,21 @@ impl std::borrow::Borrow<Foo> for Bar {
&FOO
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct U32Wrapper(u32);
impl From<u32> for U32Wrapper {
fn from(x: u32) -> Self {
Self(x)
}
}
impl PartialEq<u32> for U32Wrapper {
fn eq(&self, other: &u32) -> bool {
self.0 == *other
}
}
impl PartialEq<U32Wrapper> for u32 {
fn eq(&self, other: &U32Wrapper) -> bool {
*self == other.0
}
}

View File

@ -13,7 +13,7 @@ LL | y.to_owned() == **x;
| ^^^^^^^^^^^^^^^^^^^ try implementing the comparison without allocating
error: this creates an owned instance just for comparison
--> $DIR/without_suggestion.rs:18:9
--> $DIR/without_suggestion.rs:22:9
|
LL | self.to_owned() == *other
| ^^^^^^^^^^^^^^^^^^^^^^^^^ try implementing the comparison without allocating