@@ -1071,17 +1071,31 @@ impl UnusedParens {
1071
1071
self . emit_unused_delims ( cx, value. span , spans, "pattern" , keep_space, false ) ;
1072
1072
}
1073
1073
}
1074
+
1075
+ fn cast_followed_by_lt ( & self , expr : & ast:: Expr ) -> Option < ast:: NodeId > {
1076
+ if let ExprKind :: Binary ( op, lhs, _rhs) = & expr. kind
1077
+ && ( op. node == ast:: BinOpKind :: Lt || op. node == ast:: BinOpKind :: Shl )
1078
+ {
1079
+ let mut cur = lhs;
1080
+ while let ExprKind :: Binary ( _, _, rhs) = & cur. kind {
1081
+ cur = rhs;
1082
+ }
1083
+
1084
+ if let ExprKind :: Cast ( _, ty) = & cur. kind
1085
+ && let ast:: TyKind :: Paren ( _) = & ty. kind
1086
+ {
1087
+ return Some ( ty. id ) ;
1088
+ }
1089
+ }
1090
+ None
1091
+ }
1074
1092
}
1075
1093
1076
1094
impl EarlyLintPass for UnusedParens {
1077
1095
#[ inline]
1078
1096
fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
1079
- if let ExprKind :: Binary ( op, lhs, _rhs) = & e. kind
1080
- && ( op. node == ast:: BinOpKind :: Lt || op. node == ast:: BinOpKind :: Shl )
1081
- && let ExprKind :: Cast ( _expr, ty) = & lhs. kind
1082
- && let ast:: TyKind :: Paren ( _) = & ty. kind
1083
- {
1084
- self . parens_in_cast_in_lt . push ( ty. id ) ;
1097
+ if let Some ( ty_id) = self . cast_followed_by_lt ( e) {
1098
+ self . parens_in_cast_in_lt . push ( ty_id) ;
1085
1099
}
1086
1100
1087
1101
match e. kind {
@@ -1131,17 +1145,13 @@ impl EarlyLintPass for UnusedParens {
1131
1145
}
1132
1146
1133
1147
fn check_expr_post ( & mut self , _cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
1134
- if let ExprKind :: Binary ( op, lhs, _rhs) = & e. kind
1135
- && ( op. node == ast:: BinOpKind :: Lt || op. node == ast:: BinOpKind :: Shl )
1136
- && let ExprKind :: Cast ( _expr, ty) = & lhs. kind
1137
- && let ast:: TyKind :: Paren ( _) = & ty. kind
1138
- {
1148
+ if let Some ( ty_id) = self . cast_followed_by_lt ( e) {
1139
1149
let id = self
1140
1150
. parens_in_cast_in_lt
1141
1151
. pop ( )
1142
1152
. expect ( "check_expr and check_expr_post must balance" ) ;
1143
1153
assert_eq ! (
1144
- id, ty . id ,
1154
+ id, ty_id ,
1145
1155
"check_expr, check_ty, and check_expr_post are called, in that order, by the visitor"
1146
1156
) ;
1147
1157
}
0 commit comments