@@ -12,12 +12,13 @@ use rustc_middle::ty::{self, Ty};
1212use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1313use rustc_span:: hygiene:: DesugaringKind ;
1414use rustc_span:: source_map:: { ExpnKind , Span } ;
15+ use rustc_span:: symbol:: sym;
1516
1617use crate :: consts:: { constant, Constant } ;
1718use crate :: utils:: sugg:: Sugg ;
1819use crate :: utils:: {
19- get_item_name, get_parent_expr, higher, implements_trait, in_constant, is_integer_const , iter_input_pats ,
20- last_path_segment , match_qpath , match_trait_method , paths , snippet, snippet_opt, span_lint, span_lint_and_sugg,
20+ get_item_name, get_parent_expr, higher, implements_trait, in_constant, is_diagnostic_assoc_item , is_integer_const ,
21+ iter_input_pats , last_path_segment , match_qpath , snippet, snippet_opt, span_lint, span_lint_and_sugg,
2122 span_lint_and_then, span_lint_hir_and_then, unsext, SpanlessEq ,
2223} ;
2324
@@ -554,11 +555,16 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
554555
555556 let ( arg_ty, snip) = match expr. kind {
556557 ExprKind :: MethodCall ( .., ref args, _) if args. len ( ) == 1 => {
557- if match_trait_method ( cx, expr, & paths:: TO_STRING ) || match_trait_method ( cx, expr, & paths:: TO_OWNED ) {
558- ( cx. typeck_results ( ) . expr_ty ( & args[ 0 ] ) , snippet ( cx, args[ 0 ] . span , ".." ) )
559- } else {
560- return ;
561- }
558+ if_chain ! (
559+ if let Some ( expr_def_id) = cx. typeck_results( ) . type_dependent_def_id( expr. hir_id) ;
560+ if is_diagnostic_assoc_item( cx, expr_def_id, sym:: ToString )
561+ || is_diagnostic_assoc_item( cx, expr_def_id, sym:: ToOwned ) ;
562+ then {
563+ ( cx. typeck_results( ) . expr_ty( & args[ 0 ] ) , snippet( cx, args[ 0 ] . span, ".." ) )
564+ } else {
565+ return ;
566+ }
567+ )
562568 } ,
563569 ExprKind :: Call ( ref path, ref v) if v. len ( ) == 1 => {
564570 if let ExprKind :: Path ( ref path) = path. kind {
0 commit comments