Skip to content

Commit 228ae82

Browse files
committed
Improve changes
1 parent 8dec63c commit 228ae82

File tree

4 files changed

+91
-32
lines changed

4 files changed

+91
-32
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,25 @@ trait UnusedDelimLint {
656656
is_kw: bool,
657657
);
658658

659+
#[inline]
660+
fn impacts_followed_by_block(e: &ast::Expr) -> bool {
661+
return match e.kind {
662+
ExprKind::Block(..) | ExprKind::Call(..) | ExprKind::MethodCall(..) => true,
663+
_ => Self::is_followed_by_block(e),
664+
};
665+
}
666+
667+
#[inline]
668+
fn is_followed_by_block(e: &ast::Expr) -> bool {
669+
return match e.kind {
670+
ExprKind::If(ref cond, ..) if !matches!(cond.kind, ExprKind::Let(..)) => true,
671+
ExprKind::While(ref cond, ..) if !matches!(cond.kind, ExprKind::Let(..)) => true,
672+
ExprKind::ForLoop { .. } => true,
673+
ExprKind::Match(_, _, ast::MatchKind::Prefix) => true,
674+
_ => false,
675+
};
676+
}
677+
659678
fn is_expr_delims_necessary(
660679
inner: &ast::Expr,
661680
ctx: UnusedDelimsCtx,
@@ -1050,6 +1069,7 @@ pub(crate) struct UnusedParens {
10501069
/// `1 as (i32) < 2` parses to ExprKind::Lt
10511070
/// `1 as i32 < 2` parses to i32::<2[missing angle bracket]
10521071
parens_in_cast_in_lt: Vec<ast::NodeId>,
1072+
<<<<<<< HEAD
10531073
/// Ty nodes in this map are in TypeNoBounds position. Any bounds they
10541074
/// contain may be ambiguous w/r/t trailing `+` operators.
10551075
in_no_bounds_pos: FxHashMap<ast::NodeId, NoBoundsException>,
@@ -1075,6 +1095,12 @@ enum NoBoundsException {
10751095
/// The type is the last bound of the containing type expression. If it has exactly one bound,
10761096
/// parentheses around the type are unnecessary.
10771097
OneBound,
1098+
=======
1099+
// Used for tracking parent expressions that would immediately followed
1100+
// by block. Storing false here indicates that expression itself is Block
1101+
// expression. This is meant for to prevent report false positive cases.
1102+
followed_by_block: Vec<bool>,
1103+
>>>>>>> 5c02bc9efda (Improve changes)
10781104
}
10791105

10801106
impl_lint_pass!(UnusedParens => [UNUSED_PARENS]);
@@ -1477,32 +1503,14 @@ declare_lint! {
14771503

14781504
#[derive(Default)]
14791505
pub(crate) struct UnusedBraces {
1480-
parent_followed_by_block: Vec<bool>,
1506+
// Used for tracking parent expressions that would immediately followed
1507+
// by block. Storing false here indicates that expression itself is Block
1508+
// expression. This is meant for to prevent report false positive cases.
1509+
followed_by_block: Vec<bool>,
14811510
}
14821511

14831512
impl_lint_pass!(UnusedBraces => [UNUSED_BRACES]);
14841513

1485-
impl UnusedBraces {
1486-
#[inline]
1487-
fn should_mark_block(e: &ast::Expr) -> bool {
1488-
return match e.kind {
1489-
ExprKind::Block(..) => true,
1490-
_ => Self::is_followed_by_block(e),
1491-
};
1492-
}
1493-
1494-
#[inline]
1495-
fn is_followed_by_block(e: &ast::Expr) -> bool {
1496-
return match e.kind {
1497-
ExprKind::If(ref cond, ..) if !matches!(cond.kind, ExprKind::Let(..)) => true,
1498-
ExprKind::While(ref cond, ..) if !matches!(cond.kind, ExprKind::Let(..)) => true,
1499-
ExprKind::ForLoop { .. } => true,
1500-
ExprKind::Match(_, _, ast::MatchKind::Prefix) => true,
1501-
_ => false,
1502-
};
1503-
}
1504-
}
1505-
15061514
impl UnusedDelimLint for UnusedBraces {
15071515
const DELIM_STR: &'static str = "braces";
15081516

@@ -1600,8 +1608,8 @@ impl EarlyLintPass for UnusedBraces {
16001608
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
16011609
<Self as UnusedDelimLint>::check_expr(self, cx, e);
16021610

1603-
if Self::should_mark_block(e) {
1604-
self.parent_followed_by_block.push(Self::is_followed_by_block(e));
1611+
if <Self as UnusedDelimLint>::impacts_followed_by_block(e) {
1612+
self.followed_by_block.push(<Self as UnusedDelimLint>::is_followed_by_block(e));
16051613
}
16061614

16071615
if let ExprKind::Repeat(_, ref anon_const) = e.kind {
@@ -1618,11 +1626,9 @@ impl EarlyLintPass for UnusedBraces {
16181626
}
16191627

16201628
fn check_expr_post(&mut self, _cx: &EarlyContext<'_>, e: &ast::Expr) {
1621-
if Self::should_mark_block(e) {
1622-
let followed_by_block = self
1623-
.parent_followed_by_block
1624-
.pop()
1625-
.expect("check_expr and check_expr_post must balance");
1629+
if <Self as UnusedDelimLint>::impacts_followed_by_block(e) {
1630+
let followed_by_block =
1631+
self.followed_by_block.pop().expect("check_expr and check_expr_post must balance");
16261632
assert_eq!(
16271633
followed_by_block,
16281634
Self::is_followed_by_block(e),
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
//@ run-rustfix
22
//@ check-pass
33
#[allow(unreachable_code)]
4+
#[allow(unused)]
45
#[warn(unused_braces)]
6+
#[warn(unused_parens)]
57

68
fn main() {
79
return return; //~ WARN: unnecessary braces
10+
if f(return) {} else {} //~ WARN: unnecessary braces
811
if return { return } { return } else { return }
912
match return { return } {
1013
_ => { return }
1114
}
1215
while return { return } {}
16+
loop {
17+
if break Foo {}
18+
};
19+
}
20+
21+
struct Foo;
22+
23+
fn f(_a: ()) -> bool {
24+
true
1325
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
//@ run-rustfix
22
//@ check-pass
33
#[allow(unreachable_code)]
4+
#[allow(unused)]
45
#[warn(unused_braces)]
6+
#[warn(unused_parens)]
57

68
fn main() {
79
return { return }; //~ WARN: unnecessary braces
10+
if f({ return }) {} else {} //~ WARN: unnecessary braces
811
if return { return } { return } else { return }
912
match return { return } {
1013
_ => { return }
1114
}
1215
while return { return } {}
16+
loop {
17+
if break (Foo) {}
18+
};
19+
}
20+
21+
struct Foo;
22+
23+
fn f(_a: ()) -> bool {
24+
true
1325
}
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
warning: unnecessary braces around `return` value
2-
--> $DIR/unused-braces-respect-parent-block-issue-141783.rs:7:12
2+
--> $DIR/unused-braces-respect-parent-block-issue-141783.rs:9:12
33
|
44
LL | return { return };
55
| ^^ ^^
66
|
77
note: the lint level is defined here
8-
--> $DIR/unused-braces-respect-parent-block-issue-141783.rs:4:8
8+
--> $DIR/unused-braces-respect-parent-block-issue-141783.rs:5:8
99
|
1010
LL | #[warn(unused_braces)]
1111
| ^^^^^^^^^^^^^
@@ -15,5 +15,34 @@ LL - return { return };
1515
LL + return return;
1616
|
1717

18-
warning: 1 warning emitted
18+
warning: unnecessary braces around function argument
19+
--> $DIR/unused-braces-respect-parent-block-issue-141783.rs:10:10
20+
|
21+
LL | if f({ return }) {} else {}
22+
| ^^ ^^
23+
|
24+
help: remove these braces
25+
|
26+
LL - if f({ return }) {} else {}
27+
LL + if f(return) {} else {}
28+
|
29+
30+
warning: unnecessary parentheses around `break` value
31+
--> $DIR/unused-braces-respect-parent-block-issue-141783.rs:17:18
32+
|
33+
LL | if break (Foo) {}
34+
| ^ ^
35+
|
36+
note: the lint level is defined here
37+
--> $DIR/unused-braces-respect-parent-block-issue-141783.rs:6:8
38+
|
39+
LL | #[warn(unused_parens)]
40+
| ^^^^^^^^^^^^^
41+
help: remove these parentheses
42+
|
43+
LL - if break (Foo) {}
44+
LL + if break Foo {}
45+
|
46+
47+
warning: 3 warnings emitted
1948

0 commit comments

Comments
 (0)