@@ -572,6 +572,7 @@ enum UnusedDelimsCtx {
572572 AnonConst ,
573573 MatchArmExpr ,
574574 IndexExpr ,
575+ CastExpr ,
575576}
576577
577578impl From < UnusedDelimsCtx > for & ' static str {
@@ -592,10 +593,18 @@ impl From<UnusedDelimsCtx> for &'static str {
592593 UnusedDelimsCtx :: ArrayLenExpr | UnusedDelimsCtx :: AnonConst => "const expression" ,
593594 UnusedDelimsCtx :: MatchArmExpr => "match arm expression" ,
594595 UnusedDelimsCtx :: IndexExpr => "index expression" ,
596+ UnusedDelimsCtx :: CastExpr => "cast expression" ,
595597 }
596598 }
597599}
598600
601+ #[ derive( Copy , Clone , Eq , PartialEq ) ]
602+ enum UnusedDelimCtxFollowedTokenKind {
603+ Block ,
604+ Else ,
605+ Cast ,
606+ }
607+
599608/// Used by both `UnusedParens` and `UnusedBraces` to prevent code duplication.
600609trait UnusedDelimLint {
601610 const DELIM_STR : & ' static str ;
@@ -629,9 +638,14 @@ trait UnusedDelimLint {
629638
630639 fn is_expr_delims_necessary (
631640 inner : & ast:: Expr ,
632- followed_by_block : bool ,
633- followed_by_else : bool ,
641+ followed_token : Option < UnusedDelimCtxFollowedTokenKind > ,
634642 ) -> bool {
643+ let followed_by_block =
644+ matches ! ( followed_token, Some ( UnusedDelimCtxFollowedTokenKind :: Block ) ) ;
645+ let followed_by_else =
646+ matches ! ( followed_token, Some ( UnusedDelimCtxFollowedTokenKind :: Else ) ) ;
647+ let followed_by_cast =
648+ matches ! ( followed_token, Some ( UnusedDelimCtxFollowedTokenKind :: Cast ) ) ;
635649 if followed_by_else {
636650 match inner. kind {
637651 ast:: ExprKind :: Binary ( op, ..) if op. node . lazy ( ) => return true ,
@@ -640,6 +654,20 @@ trait UnusedDelimLint {
640654 }
641655 }
642656
657+ if followed_by_cast {
658+ match inner. kind {
659+ // `as` has higher precedence than any binary operator
660+ ast:: ExprKind :: Binary ( ..)
661+ // #88519
662+ | ast:: ExprKind :: Block ( ..)
663+ | ast:: ExprKind :: Match ( ..)
664+ | ast:: ExprKind :: If ( ..)
665+ // #51185
666+ | ast:: ExprKind :: Closure ( ..) => return true ,
667+ _ => { }
668+ }
669+ }
670+
643671 // Check if LHS needs parens to prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`.
644672 {
645673 let mut innermost = inner;
@@ -964,9 +992,18 @@ impl UnusedDelimLint for UnusedParens {
964992 ) {
965993 match value. kind {
966994 ast:: ExprKind :: Paren ( ref inner) => {
967- let followed_by_else = ctx == UnusedDelimsCtx :: AssignedValueLetElse ;
968- if !Self :: is_expr_delims_necessary ( inner, followed_by_block, followed_by_else)
969- && value. attrs . is_empty ( )
995+ if !Self :: is_expr_delims_necessary (
996+ inner,
997+ if followed_by_block {
998+ Some ( UnusedDelimCtxFollowedTokenKind :: Block )
999+ } else if ctx == UnusedDelimsCtx :: AssignedValueLetElse {
1000+ Some ( UnusedDelimCtxFollowedTokenKind :: Else )
1001+ } else if ctx == UnusedDelimsCtx :: CastExpr {
1002+ Some ( UnusedDelimCtxFollowedTokenKind :: Cast )
1003+ } else {
1004+ None
1005+ } ,
1006+ ) && value. attrs . is_empty ( )
9701007 && !value. span . from_expansion ( )
9711008 && ( ctx != UnusedDelimsCtx :: LetScrutineeExpr
9721009 || !matches ! ( inner. kind, ast:: ExprKind :: Binary (
@@ -989,6 +1026,15 @@ impl UnusedDelimLint for UnusedParens {
9891026 false ,
9901027 ) ;
9911028 }
1029+ ast:: ExprKind :: Cast ( ref expr, _) => self . check_unused_delims_expr (
1030+ cx,
1031+ expr,
1032+ UnusedDelimsCtx :: CastExpr ,
1033+ followed_by_block,
1034+ None ,
1035+ None ,
1036+ ) ,
1037+
9921038 _ => { }
9931039 }
9941040 }
@@ -1248,10 +1294,12 @@ impl UnusedDelimLint for UnusedBraces {
12481294 // FIXME(const_generics): handle paths when #67075 is fixed.
12491295 if let [ stmt] = inner. stmts . as_slice ( ) {
12501296 if let ast:: StmtKind :: Expr ( ref expr) = stmt. kind {
1251- if !Self :: is_expr_delims_necessary ( expr, followed_by_block, false )
1252- && ( ctx != UnusedDelimsCtx :: AnonConst
1253- || ( matches ! ( expr. kind, ast:: ExprKind :: Lit ( _) )
1254- && !expr. span . from_expansion ( ) ) )
1297+ if !Self :: is_expr_delims_necessary (
1298+ expr,
1299+ followed_by_block. then_some ( UnusedDelimCtxFollowedTokenKind :: Block ) ,
1300+ ) && ( ctx != UnusedDelimsCtx :: AnonConst
1301+ || ( matches ! ( expr. kind, ast:: ExprKind :: Lit ( _) )
1302+ && !expr. span . from_expansion ( ) ) )
12551303 && !cx. sess ( ) . source_map ( ) . is_multiline ( value. span )
12561304 && value. attrs . is_empty ( )
12571305 && !value. span . from_expansion ( )
0 commit comments