diff options
| author | yukang <moorekang@gmail.com> | 2023-10-29 00:29:12 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2023-11-28 22:54:45 +0800 |
| commit | eef4e653f2925e4923059f4571c8433a40f2e4a0 (patch) | |
| tree | f70111469c92b83135853f60fd1bebaa08953394 /compiler | |
| parent | e06c94d6cb61ef2fa28370fb69a8d2e11b6678c4 (diff) | |
| download | rust-eef4e653f2925e4923059f4571c8433a40f2e4a0.tar.gz rust-eef4e653f2925e4923059f4571c8433a40f2e4a0.zip | |
Fix unused_parens when cast is followed LT
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_lint/src/unused.rs | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index b4535c72d6c..4b99ffb4be4 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -1071,17 +1071,31 @@ impl UnusedParens { self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space, false); } } + + fn cast_followed_by_lt(&self, expr: &ast::Expr) -> Option<ast::NodeId> { + if let ExprKind::Binary(op, lhs, _rhs) = &expr.kind + && (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl) + { + let mut cur = lhs; + while let ExprKind::Binary(_, _, rhs) = &cur.kind { + cur = rhs; + } + + if let ExprKind::Cast(_, ty) = &cur.kind + && let ast::TyKind::Paren(_) = &ty.kind + { + return Some(ty.id); + } + } + None + } } impl EarlyLintPass for UnusedParens { #[inline] fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { - if let ExprKind::Binary(op, lhs, _rhs) = &e.kind - && (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl) - && let ExprKind::Cast(_expr, ty) = &lhs.kind - && let ast::TyKind::Paren(_) = &ty.kind - { - self.parens_in_cast_in_lt.push(ty.id); + if let Some(ty_id) = self.cast_followed_by_lt(e) { + self.parens_in_cast_in_lt.push(ty_id); } match e.kind { @@ -1131,17 +1145,13 @@ impl EarlyLintPass for UnusedParens { } fn check_expr_post(&mut self, _cx: &EarlyContext<'_>, e: &ast::Expr) { - if let ExprKind::Binary(op, lhs, _rhs) = &e.kind - && (op.node == ast::BinOpKind::Lt || op.node == ast::BinOpKind::Shl) - && let ExprKind::Cast(_expr, ty) = &lhs.kind - && let ast::TyKind::Paren(_) = &ty.kind - { + if let Some(ty_id) = self.cast_followed_by_lt(e) { let id = self .parens_in_cast_in_lt .pop() .expect("check_expr and check_expr_post must balance"); assert_eq!( - id, ty.id, + id, ty_id, "check_expr, check_ty, and check_expr_post are called, in that order, by the visitor" ); } |
