Skip to content

Commit 3071aef

Browse files
committed
Auto merge of rust-lang#117321 - chenyukang:yukang-fix-117142, r=petrochenkov
Fix unused_parens issue when cast is followed LT Fixes rust-lang#117142 The original check only checks `a as (i32) < 0`, this fix extends it to handle `b + a as (i32) < 0`. A better way is maybe we suggest `(a as i32) < 0` instead of suppressing the warning, maybe following PR could improve it.
2 parents 2319be8 + eef4e65 commit 3071aef

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 {
@@ -1133,17 +1147,13 @@ impl EarlyLintPass for UnusedParens {
11331147
}
11341148

11351149
fn check_expr_post(&mut self, _cx: &EarlyContext<'_>, e: &ast::Expr) {
1136-
if let ExprKind::Binary(op, lhs, _rhs) = &e.kind
1137-
&& (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl)
1138-
&& let ExprKind::Cast(_expr, ty) = &lhs.kind
1139-
&& let ast::TyKind::Paren(_) = &ty.kind
1140-
{
1150+
if let Some(ty_id) = self.cast_followed_by_lt(e) {
11411151
let id = self
11421152
.parens_in_cast_in_lt
11431153
.pop()
11441154
.expect("check_expr and check_expr_post must balance");
11451155
assert_eq!(
1146-
id, ty.id,
1156+
id, ty_id,
11471157
"check_expr, check_ty, and check_expr_post are called, in that order, by the visitor"
11481158
);
11491159
}
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)