Skip to content

Commit afcce38

Browse files
committed
actually fix it
1 parent 5514f32 commit afcce38

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

clippy_lints/src/mixed_read_write_in_expression.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
22
use clippy_utils::{get_parent_expr, path_to_local, path_to_local_id};
33
use if_chain::if_chain;
4-
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
4+
use rustc_hir::intravisit::{walk_expr, Visitor};
55
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Guard, HirId, Local, Node, Stmt, StmtKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::ty;
@@ -114,7 +114,7 @@ struct DivergenceVisitor<'a, 'tcx> {
114114
impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
115115
fn maybe_walk_expr(&mut self, e: &'tcx Expr<'_>) {
116116
match e.kind {
117-
ExprKind::Closure { .. } => {},
117+
ExprKind::Closure(..) | ExprKind::If(..) | ExprKind::Loop(..) => {},
118118
ExprKind::Match(e, arms, _) => {
119119
self.visit_expr(e);
120120
for arm in arms {
@@ -128,6 +128,7 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
128128
_ => walk_expr(self, e),
129129
}
130130
}
131+
131132
fn report_diverging_sub_expr(&mut self, e: &Expr<'_>) {
132133
span_lint(self.cx, DIVERGING_SUB_EXPRESSION, e.span, "sub-expression diverges");
133134
}
@@ -136,6 +137,15 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
136137
impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
137138
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
138139
match e.kind {
140+
// fix #10776
141+
ExprKind::Block(block, ..) => {
142+
if let Some(stmt) = block.stmts.first() && block.stmts.len() == 1 {
143+
match stmt.kind {
144+
StmtKind::Expr(e) | StmtKind::Semi(e) => self.visit_expr(e),
145+
_ => {},
146+
}
147+
}
148+
},
139149
ExprKind::Continue(_) | ExprKind::Break(_, _) | ExprKind::Ret(_) => self.report_diverging_sub_expr(e),
140150
ExprKind::Call(func, _) => {
141151
let typ = self.cx.typeck_results().expr_ty(func);
@@ -155,7 +165,6 @@ impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
155165
self.report_diverging_sub_expr(e);
156166
}
157167
},
158-
ExprKind::Block(block, ..) => walk_block(self, block),
159168
_ => {
160169
// do not lint expressions referencing objects of type `!`, as that required a
161170
// diverging expression
@@ -164,6 +173,9 @@ impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
164173
}
165174
self.maybe_walk_expr(e);
166175
}
176+
fn visit_block(&mut self, _: &'tcx Block<'_>) {
177+
// don't continue over blocks, LateLintPass already does that
178+
}
167179
}
168180

169181
/// Walks up the AST from the given write expression (`vis.write_expr`) looking

tests/ui/diverging_sub_expression.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn main() {
2222
}
2323

2424
#[allow(dead_code, unused_variables)]
25+
#[rustfmt::skip]
2526
fn foobar() {
2627
loop {
2728
let x = match 5 {

tests/ui/diverging_sub_expression.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,45 @@ LL | b || A.foo();
1313
| ^^^^^^^
1414

1515
error: sub-expression diverges
16-
--> $DIR/diverging_sub_expression.rs:30:26
16+
--> $DIR/diverging_sub_expression.rs:31:26
1717
|
1818
LL | 6 => true || return,
1919
| ^^^^^^
2020

2121
error: sub-expression diverges
22-
--> $DIR/diverging_sub_expression.rs:31:26
22+
--> $DIR/diverging_sub_expression.rs:32:26
2323
|
2424
LL | 7 => true || continue,
2525
| ^^^^^^^^
2626

2727
error: sub-expression diverges
28-
--> $DIR/diverging_sub_expression.rs:34:26
28+
--> $DIR/diverging_sub_expression.rs:35:26
2929
|
3030
LL | 3 => true || diverge(),
3131
| ^^^^^^^^^
3232

3333
error: sub-expression diverges
34-
--> $DIR/diverging_sub_expression.rs:37:30
34+
--> $DIR/diverging_sub_expression.rs:38:30
3535
|
3636
LL | _ => true || panic!("boo"),
3737
| ^^^^^^^^^^^^^
3838
|
3939
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
4040

4141
error: sub-expression diverges
42-
--> $DIR/diverging_sub_expression.rs:39:29
42+
--> $DIR/diverging_sub_expression.rs:41:29
4343
|
4444
LL | 15 => true || { return; },
4545
| ^^^^^^
4646

4747
error: sub-expression diverges
48-
--> $DIR/diverging_sub_expression.rs:40:30
48+
--> $DIR/diverging_sub_expression.rs:42:30
4949
|
5050
LL | 16 => false || { return; },
5151
| ^^^^^^
5252

5353
error: sub-expression diverges
54-
--> $DIR/diverging_sub_expression.rs:41:26
54+
--> $DIR/diverging_sub_expression.rs:43:26
5555
|
5656
LL | _ => true || break,
5757
| ^^^^^

tests/ui/never_loop.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ LL | | }
126126
LL | | }
127127
| |_____^
128128

129+
error: sub-expression diverges
130+
--> $DIR/never_loop.rs:247:17
131+
|
132+
LL | break 'a;
133+
| ^^^^^^^^
134+
|
135+
= note: `-D clippy::diverging-sub-expression` implied by `-D warnings`
136+
129137
error: this loop never actually loops
130138
--> $DIR/never_loop.rs:278:13
131139
|
@@ -139,5 +147,5 @@ help: if you need the first element of the iterator, try writing
139147
LL | if let Some(_) = (0..20).next() {
140148
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141149

142-
error: aborting due to 12 previous errors
150+
error: aborting due to 13 previous errors
143151

0 commit comments

Comments
 (0)