Skip to content

Commit d44ea7c

Browse files
committed
Auto merge of #10785 - Centri3:diverting_sub_expression, r=Jarcho
Fix `diverging_sub_expression` not checking body of block Fixes #10776 This also adds a warning to the test `ui/never_loop.rs`, not sure if this is correct or not. changelog: [`diverging_sub_expression`]: Fix false negatives with body of block
2 parents 476efe9 + 35aff1a commit d44ea7c

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

clippy_lints/src/mixed_read_write_in_expression.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -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, ..) => match (block.stmts, block.expr) {
142+
([], Some(e)) => self.visit_expr(e),
143+
([stmt], None) => 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);

tests/ui/diverging_sub_expression.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::diverging_sub_expression)]
22
#![allow(clippy::match_same_arms, clippy::overly_complex_bool_expr)]
3+
#![allow(clippy::nonminimal_bool)]
34
#[allow(clippy::empty_loop)]
45
fn diverge() -> ! {
56
loop {}
@@ -21,6 +22,7 @@ fn main() {
2122
}
2223

2324
#[allow(dead_code, unused_variables)]
25+
#[rustfmt::skip]
2426
fn foobar() {
2527
loop {
2628
let x = match 5 {
@@ -35,6 +37,20 @@ fn foobar() {
3537
99 => return,
3638
_ => true || panic!("boo"),
3739
},
40+
// lint blocks as well
41+
15 => true || { return; },
42+
16 => false || { return; },
43+
// ... and when it's a single expression
44+
17 => true || { return },
45+
18 => false || { return },
46+
// ... but not when there's both an expression and a statement
47+
19 => true || { _ = 1; return },
48+
20 => false || { _ = 1; return },
49+
// ... or multiple statements
50+
21 => true || { _ = 1; return; },
51+
22 => false || { _ = 1; return; },
52+
23 => true || { return; true },
53+
24 => true || { return; true },
3854
_ => true || break,
3955
};
4056
}
+39-7
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,72 @@
11
error: sub-expression diverges
2-
--> $DIR/diverging_sub_expression.rs:19:10
2+
--> $DIR/diverging_sub_expression.rs:20:10
33
|
44
LL | b || diverge();
55
| ^^^^^^^^^
66
|
77
= note: `-D clippy::diverging-sub-expression` implied by `-D warnings`
88

99
error: sub-expression diverges
10-
--> $DIR/diverging_sub_expression.rs:20:10
10+
--> $DIR/diverging_sub_expression.rs:21:10
1111
|
1212
LL | b || A.foo();
1313
| ^^^^^^^
1414

1515
error: sub-expression diverges
16-
--> $DIR/diverging_sub_expression.rs:29: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:30: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:33: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:38:26
34+
--> $DIR/diverging_sub_expression.rs:38:30
35+
|
36+
LL | _ => true || panic!("boo"),
37+
| ^^^^^^^^^^^^^
38+
|
39+
= 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)
40+
41+
error: sub-expression diverges
42+
--> $DIR/diverging_sub_expression.rs:41:29
43+
|
44+
LL | 15 => true || { return; },
45+
| ^^^^^^
46+
47+
error: sub-expression diverges
48+
--> $DIR/diverging_sub_expression.rs:42:30
49+
|
50+
LL | 16 => false || { return; },
51+
| ^^^^^^
52+
53+
error: sub-expression diverges
54+
--> $DIR/diverging_sub_expression.rs:44:29
55+
|
56+
LL | 17 => true || { return },
57+
| ^^^^^^
58+
59+
error: sub-expression diverges
60+
--> $DIR/diverging_sub_expression.rs:45:30
61+
|
62+
LL | 18 => false || { return },
63+
| ^^^^^^
64+
65+
error: sub-expression diverges
66+
--> $DIR/diverging_sub_expression.rs:54:26
3567
|
3668
LL | _ => true || break,
3769
| ^^^^^
3870

39-
error: aborting due to 6 previous errors
71+
error: aborting due to 11 previous errors
4072

tests/ui/never_loop.stderr

+9-1
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)