1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::is_diag_trait_item;
3 use clippy_utils::source::snippet_with_context;
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::{self, print::with_forced_trimmed_paths};
11 use super::SUSPICIOUS_TO_OWNED;
13 pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) -> bool {
15 if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
16 if is_diag_trait_item(cx, method_def_id, sym::ToOwned);
17 let input_type = cx.typeck_results().expr_ty(expr);
18 if let ty::Adt(adt, _) = cx.typeck_results().expr_ty(expr).kind();
19 if cx.tcx.is_diagnostic_item(sym::Cow, adt.did());
21 let mut app = Applicability::MaybeIncorrect;
22 let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0;
27 &with_forced_trimmed_paths!(format!(
28 "this `to_owned` call clones the {input_type} itself and does not cause the {input_type} contents to become owned"
30 "consider using, depending on intent",
31 format!("{recv_snip}.clone()` or `{recv_snip}.into_owned()"),