Skip to content

Commit eef4e65

Browse files
committed
Fix unused_parens when cast is followed LT
1 parent e06c94d commit eef4e65

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

compiler/rustc_lint/src/unused.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -1071,17 +1071,31 @@ impl UnusedParens {
10711071
self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space, false);
10721072
}
10731073
}
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+
}
10741092
}
10751093

10761094
impl EarlyLintPass for UnusedParens {
10771095
#[inline]
10781096
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);
10851099
}
10861100

10871101
match e.kind {
@@ -1131,17 +1145,13 @@ impl EarlyLintPass for UnusedParens {
11311145
}
11321146

11331147
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) {
11391149
let id = self
11401150
.parens_in_cast_in_lt
11411151
.pop()
11421152
.expect("check_expr and check_expr_post must balance");
11431153
assert_eq!(
1144-
id, ty.id,
1154+
id, ty_id,
11451155
"check_expr, check_ty, and check_expr_post are called, in that order, by the visitor"
11461156
);
11471157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// check-pass
2+
#![warn(unused_parens)]
3+
4+
fn main() {
5+
let a: i32 = 1;
6+
let b: i64 = 1;
7+
8+
if b + a as (i64) < 0 {
9+
println!(":D");
10+
}
11+
if b + b + a as (i64) < 0 {
12+
println!(":D");
13+
}
14+
let c = a + b as (i32) < 0;
15+
let mut x = false;
16+
x |= false || (b as (i32) < 0);
17+
18+
let d = 1 + 2 + 3 * 4 as (i32) < 10;
19+
}

0 commit comments

Comments
 (0)