Skip to content

Commit 3528630

Browse files
committed
cmp_owned refactor
1 parent c9718fa commit 3528630

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

clippy_lints/src/misc.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::utils::{get_item_name, get_parent_expr, implements_trait, in_constant
2121
iter_input_pats, last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint,
2222
span_lint_and_then, walk_ptrs_ty, SpanlessEq};
2323
use crate::utils::sugg::Sugg;
24-
use crate::syntax::ast::{LitKind, CRATE_NODE_ID};
24+
use crate::syntax::ast::LitKind;
2525
use crate::consts::{constant, Constant};
2626
use crate::rustc_errors::Applicability;
2727

@@ -540,18 +540,10 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr, other: &Expr) {
540540
_ => false,
541541
};
542542

543-
let (lint_span, try_hint) = if deref_arg_impl_partial_eq_other {
544-
// suggest deref on the left
545-
(expr.span, format!("*{}", snip))
546-
} else if other_gets_derefed {
547-
// suggest dropping the to_owned on the left and the deref on the right
548-
let other_snippet = snippet(cx, other.span, "..").into_owned();
549-
let other_without_deref = other_snippet.replacen('*', "", 1);
550-
551-
(expr.span.to(other.span), format!("{} == {}", snip.to_string(), other_without_deref))
543+
let lint_span = if other_gets_derefed {
544+
expr.span.to(other.span)
552545
} else {
553-
// suggest dropping the to_owned on the left
554-
(expr.span, snip.to_string())
546+
expr.span
555547
};
556548

557549
span_lint_and_then(
@@ -560,29 +552,20 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr, other: &Expr) {
560552
lint_span,
561553
"this creates an owned instance just for comparison",
562554
|db| {
563-
// this is as good as our recursion check can get, we can't prove that the
564-
// current function is
565-
// called by
566-
// PartialEq::eq, but we can at least ensure that this code is not part of it
567-
let parent_fn = cx.tcx.hir.get_parent(expr.id);
568-
let parent_impl = cx.tcx.hir.get_parent(parent_fn);
569-
if parent_impl != CRATE_NODE_ID {
570-
if let Node::Item(item) = cx.tcx.hir.get(parent_impl) {
571-
if let ItemKind::Impl(.., Some(ref trait_ref), _, _) = item.node {
572-
if trait_ref.path.def.def_id() == partial_eq_trait_id {
573-
// we are implementing PartialEq, don't suggest not doing `to_owned`, otherwise
574-
// we go into
575-
// recursion
576-
db.span_label(lint_span, "try implementing the comparison without allocating");
577-
return;
578-
}
579-
}
580-
}
581-
}
555+
// this also catches PartialEq implementations that call to_owned
582556
if other_gets_derefed {
583557
db.span_label(lint_span, "try implementing the comparison without allocating");
584558
return;
585559
}
560+
561+
let try_hint = if deref_arg_impl_partial_eq_other {
562+
// suggest deref on the left
563+
format!("*{}", snip)
564+
} else {
565+
// suggest dropping the to_owned on the left
566+
snip.to_string()
567+
};
568+
586569
db.span_suggestion_with_applicability(
587570
lint_span,
588571
"try",

0 commit comments

Comments
 (0)