@@ -11,7 +11,7 @@ use clippy_utils::{
1111use core:: iter;
1212use core:: ops:: ControlFlow ;
1313use rustc_errors:: Applicability ;
14- use rustc_hir:: { BinOpKind , Block , Expr , ExprKind , HirId , HirIdSet , Stmt , StmtKind , intravisit} ;
14+ use rustc_hir:: { BinOpKind , Block , Expr , ExprKind , HirId , HirIdSet , LetStmt , Node , Stmt , StmtKind , intravisit} ;
1515use rustc_lint:: { LateContext , LateLintPass } ;
1616use rustc_middle:: ty:: TyCtxt ;
1717use rustc_session:: impl_lint_pass;
@@ -295,7 +295,7 @@ fn lint_branches_sharing_code<'tcx>(
295295 sugg,
296296 Applicability :: Unspecified ,
297297 ) ;
298- if !cx. typeck_results ( ) . expr_ty ( expr) . is_unit ( ) {
298+ if is_expr_parent_assignment ( cx , expr ) || !cx. typeck_results ( ) . expr_ty ( expr) . is_unit ( ) {
299299 diag. note ( "the end suggestion probably needs some adjustments to use the expression result correctly" ) ;
300300 }
301301 }
@@ -660,3 +660,17 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
660660 ) ;
661661 }
662662}
663+
664+ fn is_expr_parent_assignment ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
665+ let parent = cx. tcx . parent_hir_node ( expr. hir_id ) ;
666+ if let Node :: LetStmt ( LetStmt { init : Some ( e) , .. } )
667+ | Node :: Expr ( Expr {
668+ kind : ExprKind :: Assign ( _, e, _) ,
669+ ..
670+ } ) = parent
671+ {
672+ return e. hir_id == expr. hir_id ;
673+ }
674+
675+ false
676+ }
0 commit comments