Skip to content

Commit 282c740

Browse files
committed
Fix unused_parens when cast is followed LT
1 parent a483969 commit 282c740

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,17 +1053,31 @@ impl UnusedParens {
10531053
self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space, false);
10541054
}
10551055
}
1056+
1057+
fn cast_followed_by_lt(&self, e: &ast::Expr) -> Option<ast::NodeId> {
1058+
if let ExprKind::Binary(op, lhs, _rhs) = &e.kind
1059+
&& (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
1060+
{
1061+
let rhs_of_lhs = match &lhs.kind {
1062+
ExprKind::Binary(_, _, rhs) => rhs,
1063+
_ => lhs,
1064+
};
1065+
1066+
if let ExprKind::Cast(_expr, ty) = &rhs_of_lhs.kind
1067+
&& let ast::TyKind::Paren(_) = &ty.kind
1068+
{
1069+
return Some(ty.id);
1070+
}
1071+
}
1072+
None
1073+
}
10561074
}
10571075

10581076
impl EarlyLintPass for UnusedParens {
10591077
#[inline]
10601078
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
1061-
if let ExprKind::Binary(op, lhs, _rhs) = &e.kind
1062-
&& (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
1063-
&& let ExprKind::Cast(_expr, ty) = &lhs.kind
1064-
&& let ast::TyKind::Paren(_) = &ty.kind
1065-
{
1066-
self.parens_in_cast_in_lt.push(ty.id);
1079+
if let Some(ty_id) = self.cast_followed_by_lt(e) {
1080+
self.parens_in_cast_in_lt.push(ty_id);
10671081
}
10681082

10691083
match e.kind {
@@ -1113,17 +1127,13 @@ impl EarlyLintPass for UnusedParens {
11131127
}
11141128

11151129
fn check_expr_post(&mut self, _cx: &EarlyContext<'_>, e: &ast::Expr) {
1116-
if let ExprKind::Binary(op, lhs, _rhs) = &e.kind
1117-
&& (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
1118-
&& let ExprKind::Cast(_expr, ty) = &lhs.kind
1119-
&& let ast::TyKind::Paren(_) = &ty.kind
1120-
{
1130+
if let Some(ty_id) = self.cast_followed_by_lt(e) {
11211131
let id = self
11221132
.parens_in_cast_in_lt
11231133
.pop()
11241134
.expect("check_expr and check_expr_post must balance");
11251135
assert_eq!(
1126-
id, ty.id,
1136+
id, ty_id,
11271137
"check_expr, check_ty, and check_expr_post are called, in that order, by the visitor"
11281138
);
11291139
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
#![allow(unused_variables)]
3+
#![warn(unused_parens)]
4+
5+
fn main() {
6+
let a: i32 = 1;
7+
let b: i64 = 1;
8+
9+
if b + a as (i64) < 0 {
10+
println!(":D");
11+
}
12+
if b + b + a as (i64) < 0 {
13+
println!(":D");
14+
}
15+
let _c = a + b as (i32) < 0;
16+
let mut _x = false;
17+
_x |= false || (b as (i32) < 0);
18+
}

0 commit comments

Comments
 (0)