Skip to content

Commit 888dc2e

Browse files
authored
Rollup merge of #115424 - notriddle:notriddle/issue-106413, r=oli-obk
diagnostics: avoid wrong `unused_parens` on `x as (T) < y` Fixes #106413 Fixes #80636
2 parents db6ae07 + ba9af10 commit 888dc2e

File tree

5 files changed

+189
-1
lines changed

5 files changed

+189
-1
lines changed

compiler/rustc_lint/src/early.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
228228
}) => self.check_id(closure_id),
229229
_ => {}
230230
}
231+
lint_callback!(self, check_expr_post, e);
231232
}
232233

233234
fn visit_generic_arg(&mut self, arg: &'a ast::GenericArg) {

compiler/rustc_lint/src/passes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ macro_rules! early_lint_methods {
153153
fn check_pat(a: &ast::Pat);
154154
fn check_pat_post(a: &ast::Pat);
155155
fn check_expr(a: &ast::Expr);
156+
fn check_expr_post(a: &ast::Expr);
156157
fn check_ty(a: &ast::Ty);
157158
fn check_generic_arg(a: &ast::GenericArg);
158159
fn check_generic_param(a: &ast::GenericParam);

compiler/rustc_lint/src/unused.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,11 +955,14 @@ declare_lint! {
955955

956956
pub struct UnusedParens {
957957
with_self_ty_parens: bool,
958+
/// `1 as (i32) < 2` parses to ExprKind::Lt
959+
/// `1 as i32 < 2` parses to i32::<2[missing angle bracket]
960+
parens_in_cast_in_lt: Vec<ast::NodeId>,
958961
}
959962

960963
impl UnusedParens {
961964
pub fn new() -> Self {
962-
Self { with_self_ty_parens: false }
965+
Self { with_self_ty_parens: false, parens_in_cast_in_lt: Vec::new() }
963966
}
964967
}
965968

@@ -1055,6 +1058,14 @@ impl UnusedParens {
10551058
impl EarlyLintPass for UnusedParens {
10561059
#[inline]
10571060
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);
1067+
}
1068+
10581069
match e.kind {
10591070
ExprKind::Let(ref pat, _, _) | ExprKind::ForLoop(ref pat, ..) => {
10601071
self.check_unused_parens_pat(cx, pat, false, false, (true, true));
@@ -1101,6 +1112,17 @@ impl EarlyLintPass for UnusedParens {
11011112
<Self as UnusedDelimLint>::check_expr(self, cx, e)
11021113
}
11031114

1115+
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+
{
1121+
let id = self.parens_in_cast_in_lt.pop().expect("check_expr and check_expr_post must balance");
1122+
assert_eq!(id, ty.id, "check_expr, check_ty, and check_expr_post are called, in that order, by the visitor");
1123+
}
1124+
}
1125+
11041126
fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &ast::Pat) {
11051127
use ast::{Mutability, PatKind::*};
11061128
let keep_space = (false, false);
@@ -1141,6 +1163,11 @@ impl EarlyLintPass for UnusedParens {
11411163
}
11421164

11431165
fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1166+
if let ast::TyKind::Paren(_) = ty.kind &&
1167+
Some(&ty.id) == self.parens_in_cast_in_lt.last()
1168+
{
1169+
return;
1170+
}
11441171
match &ty.kind {
11451172
ast::TyKind::Array(_, len) => {
11461173
self.check_unused_delims_expr(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// check-pass
2+
#![warn(unused_parens)]
3+
4+
fn id<T>(t: T) -> T { t }
5+
6+
fn main() {
7+
// This should not warn
8+
let _ = 1 as (i32) < 2;
9+
let _ = id(1 as (i32) < 2);
10+
let _ = id(1 as i32)
11+
as (i32) < 2;
12+
// These should warn
13+
let _ = 1 as ((i32)) < 2; //~WARN unnecessary parentheses
14+
let _ = 1 as (i32); //~WARN unnecessary parentheses
15+
let _ = 1 as (i32) > 2; //~WARN unnecessary parentheses
16+
let _ = id(1 as (i32)) //~WARN unnecessary parentheses
17+
as (i32) < 2;
18+
let _ = id(1 as (i32)) < 2; //~WARN unnecessary parentheses
19+
20+
// This should not warn
21+
let _ = 1 as (i32) << 2;
22+
let _ = id(1 as (i32) << 2);
23+
let _ = id(1 as i32)
24+
as (i32) << 2;
25+
// These should warn
26+
let _ = 1 as ((i32)) << 2; //~WARN unnecessary parentheses
27+
let _ = 1 as (i32); //~WARN unnecessary parentheses
28+
let _ = 1 as (i32) >> 2; //~WARN unnecessary parentheses
29+
let _ = id(1 as (i32)) //~WARN unnecessary parentheses
30+
as (i32) << 2;
31+
let _ = id(1 as (i32)) << 2; //~WARN unnecessary parentheses
32+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
warning: unnecessary parentheses around type
2+
--> $DIR/unused-parens-issue-106413.rs:13:19
3+
|
4+
LL | let _ = 1 as ((i32)) < 2;
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-parens-issue-106413.rs:2:9
9+
|
10+
LL | #![warn(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - let _ = 1 as ((i32)) < 2;
15+
LL + let _ = 1 as (i32) < 2;
16+
|
17+
18+
warning: unnecessary parentheses around type
19+
--> $DIR/unused-parens-issue-106413.rs:14:18
20+
|
21+
LL | let _ = 1 as (i32);
22+
| ^ ^
23+
|
24+
help: remove these parentheses
25+
|
26+
LL - let _ = 1 as (i32);
27+
LL + let _ = 1 as i32;
28+
|
29+
30+
warning: unnecessary parentheses around type
31+
--> $DIR/unused-parens-issue-106413.rs:15:18
32+
|
33+
LL | let _ = 1 as (i32) > 2;
34+
| ^ ^
35+
|
36+
help: remove these parentheses
37+
|
38+
LL - let _ = 1 as (i32) > 2;
39+
LL + let _ = 1 as i32 > 2;
40+
|
41+
42+
warning: unnecessary parentheses around type
43+
--> $DIR/unused-parens-issue-106413.rs:16:21
44+
|
45+
LL | let _ = id(1 as (i32))
46+
| ^ ^
47+
|
48+
help: remove these parentheses
49+
|
50+
LL - let _ = id(1 as (i32))
51+
LL + let _ = id(1 as i32)
52+
|
53+
54+
warning: unnecessary parentheses around type
55+
--> $DIR/unused-parens-issue-106413.rs:18:21
56+
|
57+
LL | let _ = id(1 as (i32)) < 2;
58+
| ^ ^
59+
|
60+
help: remove these parentheses
61+
|
62+
LL - let _ = id(1 as (i32)) < 2;
63+
LL + let _ = id(1 as i32) < 2;
64+
|
65+
66+
warning: unnecessary parentheses around type
67+
--> $DIR/unused-parens-issue-106413.rs:26:19
68+
|
69+
LL | let _ = 1 as ((i32)) << 2;
70+
| ^ ^
71+
|
72+
help: remove these parentheses
73+
|
74+
LL - let _ = 1 as ((i32)) << 2;
75+
LL + let _ = 1 as (i32) << 2;
76+
|
77+
78+
warning: unnecessary parentheses around type
79+
--> $DIR/unused-parens-issue-106413.rs:27:18
80+
|
81+
LL | let _ = 1 as (i32);
82+
| ^ ^
83+
|
84+
help: remove these parentheses
85+
|
86+
LL - let _ = 1 as (i32);
87+
LL + let _ = 1 as i32;
88+
|
89+
90+
warning: unnecessary parentheses around type
91+
--> $DIR/unused-parens-issue-106413.rs:28:18
92+
|
93+
LL | let _ = 1 as (i32) >> 2;
94+
| ^ ^
95+
|
96+
help: remove these parentheses
97+
|
98+
LL - let _ = 1 as (i32) >> 2;
99+
LL + let _ = 1 as i32 >> 2;
100+
|
101+
102+
warning: unnecessary parentheses around type
103+
--> $DIR/unused-parens-issue-106413.rs:29:21
104+
|
105+
LL | let _ = id(1 as (i32))
106+
| ^ ^
107+
|
108+
help: remove these parentheses
109+
|
110+
LL - let _ = id(1 as (i32))
111+
LL + let _ = id(1 as i32)
112+
|
113+
114+
warning: unnecessary parentheses around type
115+
--> $DIR/unused-parens-issue-106413.rs:31:21
116+
|
117+
LL | let _ = id(1 as (i32)) << 2;
118+
| ^ ^
119+
|
120+
help: remove these parentheses
121+
|
122+
LL - let _ = id(1 as (i32)) << 2;
123+
LL + let _ = id(1 as i32) << 2;
124+
|
125+
126+
warning: 10 warnings emitted
127+

0 commit comments

Comments
 (0)