@@ -5,10 +5,11 @@ use clippy_utils::{
55 diagnostics:: span_lint_and_sugg, is_default_equivalent_call, source:: snippet_with_applicability,
66 ty:: is_type_diagnostic_item,
77} ;
8+ use rustc_ast:: ast:: LitKind ;
89use rustc_errors:: Applicability ;
910use rustc_hir as hir;
1011use rustc_lint:: LateContext ;
11- use rustc_span:: sym;
12+ use rustc_span:: { sym, symbol } ;
1213
1314pub ( super ) fn check < ' tcx > (
1415 cx : & LateContext < ' tcx > ,
@@ -25,7 +26,7 @@ pub(super) fn check<'tcx>(
2526
2627 if_chain ! {
2728 if is_option || is_result;
28- if is_default_equivalent_call( cx, u_arg) ;
29+ if closure_body_returns_empty_to_string ( cx , u_arg ) || is_default_equivalent_call( cx, u_arg) ;
2930 then {
3031 let mut applicability = Applicability :: MachineApplicable ;
3132
@@ -44,3 +45,22 @@ pub(super) fn check<'tcx>(
4445 }
4546 }
4647}
48+
49+ fn closure_body_returns_empty_to_string ( cx : & LateContext < ' _ > , e : & hir:: Expr < ' _ > ) -> bool {
50+ if let hir:: ExprKind :: Closure ( & hir:: Closure { body, .. } ) = e. kind {
51+ let body = cx. tcx . hir ( ) . body ( body) ;
52+
53+ if body. params . is_empty ( )
54+ && let hir:: Expr { kind, .. } = & body. value
55+ && let hir:: ExprKind :: MethodCall ( hir:: PathSegment { ident, ..} , [ self_arg] , _) = kind
56+ && ident == & symbol:: Ident :: from_str ( "to_string" )
57+ && let hir:: Expr { kind, .. } = self_arg
58+ && let hir:: ExprKind :: Lit ( lit) = kind
59+ && let LitKind :: Str ( symbol:: kw:: Empty , _) = lit. node
60+ {
61+ return true ;
62+ }
63+ }
64+
65+ false
66+ }
0 commit comments