@@ -572,6 +572,7 @@ enum UnusedDelimsCtx {
572
572
AnonConst ,
573
573
MatchArmExpr ,
574
574
IndexExpr ,
575
+ CastExpr ,
575
576
}
576
577
577
578
impl From < UnusedDelimsCtx > for & ' static str {
@@ -592,10 +593,18 @@ impl From<UnusedDelimsCtx> for &'static str {
592
593
UnusedDelimsCtx :: ArrayLenExpr | UnusedDelimsCtx :: AnonConst => "const expression" ,
593
594
UnusedDelimsCtx :: MatchArmExpr => "match arm expression" ,
594
595
UnusedDelimsCtx :: IndexExpr => "index expression" ,
596
+ UnusedDelimsCtx :: CastExpr => "cast expression" ,
595
597
}
596
598
}
597
599
}
598
600
601
+ #[ derive( Copy , Clone , Eq , PartialEq ) ]
602
+ enum UnusedDelimCtxFollowedTokenKind {
603
+ Block ,
604
+ Else ,
605
+ Cast ,
606
+ }
607
+
599
608
/// Used by both `UnusedParens` and `UnusedBraces` to prevent code duplication.
600
609
trait UnusedDelimLint {
601
610
const DELIM_STR : & ' static str ;
@@ -629,9 +638,14 @@ trait UnusedDelimLint {
629
638
630
639
fn is_expr_delims_necessary (
631
640
inner : & ast:: Expr ,
632
- followed_by_block : bool ,
633
- followed_by_else : bool ,
641
+ followed_token : Option < UnusedDelimCtxFollowedTokenKind > ,
634
642
) -> 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 ) ) ;
635
649
if followed_by_else {
636
650
match inner. kind {
637
651
ast:: ExprKind :: Binary ( op, ..) if op. node . lazy ( ) => return true ,
@@ -640,6 +654,20 @@ trait UnusedDelimLint {
640
654
}
641
655
}
642
656
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
+
643
671
// Check if LHS needs parens to prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`.
644
672
{
645
673
let mut innermost = inner;
@@ -964,9 +992,18 @@ impl UnusedDelimLint for UnusedParens {
964
992
) {
965
993
match value. kind {
966
994
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 ( )
970
1007
&& !value. span . from_expansion ( )
971
1008
&& ( ctx != UnusedDelimsCtx :: LetScrutineeExpr
972
1009
|| !matches ! ( inner. kind, ast:: ExprKind :: Binary (
@@ -989,6 +1026,15 @@ impl UnusedDelimLint for UnusedParens {
989
1026
false ,
990
1027
) ;
991
1028
}
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
+
992
1038
_ => { }
993
1039
}
994
1040
}
@@ -1248,10 +1294,12 @@ impl UnusedDelimLint for UnusedBraces {
1248
1294
// FIXME(const_generics): handle paths when #67075 is fixed.
1249
1295
if let [ stmt] = inner. stmts . as_slice ( ) {
1250
1296
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 ( ) ) )
1255
1303
&& !cx. sess ( ) . source_map ( ) . is_multiline ( value. span )
1256
1304
&& value. attrs . is_empty ( )
1257
1305
&& !value. span . from_expansion ( )
0 commit comments