@@ -64,8 +64,8 @@ use rustc_hir::intravisit::{self, walk_expr, ErasedMap, NestedVisitorMap, Visito
6464use rustc_hir:: LangItem :: { ResultErr , ResultOk } ;
6565use rustc_hir:: {
6666 def, Arm , BindingAnnotation , Block , Body , Constness , Destination , Expr , ExprKind , FnDecl , GenericArgs , HirId , Impl ,
67- ImplItem , ImplItemKind , Item , ItemKind , LangItem , MatchSource , Node , Param , Pat , PatKind , Path , PathSegment , QPath ,
68- Stmt , StmtKind , TraitItem , TraitItemKind , TraitRef , TyKind ,
67+ ImplItem , ImplItemKind , Item , ItemKind , LangItem , Local , MatchSource , Node , Param , Pat , PatKind , Path , PathSegment ,
68+ QPath , Stmt , StmtKind , TraitItem , TraitItemKind , TraitRef , TyKind ,
6969} ;
7070use rustc_lint:: { LateContext , Level , Lint , LintContext } ;
7171use rustc_middle:: hir:: exports:: Export ;
@@ -1312,48 +1312,7 @@ pub fn is_must_use_func_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
13121312 did. map_or ( false , |did| must_use_attr ( & cx. tcx . get_attrs ( did) ) . is_some ( ) )
13131313}
13141314
1315- pub fn get_expr_use_node ( tcx : TyCtxt < ' tcx > , expr : & Expr < ' _ > ) -> Option < Node < ' tcx > > {
1316- let map = tcx. hir ( ) ;
1317- let mut child_id = expr. hir_id ;
1318- let mut iter = map. parent_iter ( child_id) ;
1319- loop {
1320- match iter. next ( ) {
1321- None => break None ,
1322- Some ( ( id, Node :: Block ( _) ) ) => child_id = id,
1323- Some ( ( id, Node :: Arm ( arm) ) ) if arm. body . hir_id == child_id => child_id = id,
1324- Some ( ( _, Node :: Expr ( expr) ) ) => match expr. kind {
1325- ExprKind :: Break (
1326- Destination {
1327- target_id : Ok ( dest) , ..
1328- } ,
1329- _,
1330- ) => {
1331- iter = map. parent_iter ( dest) ;
1332- child_id = dest;
1333- } ,
1334- ExprKind :: DropTemps ( _) | ExprKind :: Block ( ..) => child_id = expr. hir_id ,
1335- ExprKind :: If ( control_expr, ..) | ExprKind :: Match ( control_expr, ..)
1336- if control_expr. hir_id != child_id =>
1337- {
1338- child_id = expr. hir_id
1339- } ,
1340- _ => break Some ( Node :: Expr ( expr) ) ,
1341- } ,
1342- Some ( ( _, node) ) => break Some ( node) ,
1343- }
1344- }
1345- }
1346-
1347- pub fn is_expr_used ( tcx : TyCtxt < ' _ > , expr : & Expr < ' _ > ) -> bool {
1348- !matches ! (
1349- get_expr_use_node( tcx, expr) ,
1350- Some ( Node :: Stmt ( Stmt {
1351- kind: StmtKind :: Expr ( _) | StmtKind :: Semi ( _) ,
1352- ..
1353- } ) )
1354- )
1355- }
1356-
1315+ /// Gets the node where an expression is either used, or it's type is unified with another branch.
13571316pub fn get_expr_use_or_unification_node ( tcx : TyCtxt < ' tcx > , expr : & Expr < ' _ > ) -> Option < Node < ' tcx > > {
13581317 let map = tcx. hir ( ) ;
13591318 let mut child_id = expr. hir_id ;
@@ -1374,16 +1333,26 @@ pub fn get_expr_use_or_unification_node(tcx: TyCtxt<'tcx>, expr: &Expr<'_>) -> O
13741333 }
13751334}
13761335
1336+ /// Checks if the result of an expression is used, or it's type is unified with another branch.
13771337pub fn is_expr_used_or_unified ( tcx : TyCtxt < ' _ > , expr : & Expr < ' _ > ) -> bool {
13781338 !matches ! (
13791339 get_expr_use_or_unification_node( tcx, expr) ,
13801340 None | Some ( Node :: Stmt ( Stmt {
1381- kind: StmtKind :: Expr ( _) | StmtKind :: Semi ( _) ,
1341+ kind: StmtKind :: Expr ( _)
1342+ | StmtKind :: Semi ( _)
1343+ | StmtKind :: Local ( Local {
1344+ pat: Pat {
1345+ kind: PatKind :: Wild ,
1346+ ..
1347+ } ,
1348+ ..
1349+ } ) ,
13821350 ..
13831351 } ) )
13841352 )
13851353}
13861354
1355+ /// Checks if the expression is the final expression returned from a block.
13871356pub fn is_expr_final_block_expr ( tcx : TyCtxt < ' _ > , expr : & Expr < ' _ > ) -> bool {
13881357 matches ! ( get_parent_node( tcx, expr. hir_id) , Some ( Node :: Block ( ..) ) )
13891358}
0 commit comments