Skip to content

Commit 93fe356

Browse files
committed
Auto merge of #7174 - camsteffen:eval-order-async, r=flip1995
Fix eval_order_dependence async false positive changelog: Fix [`eval_order_dependence`] false positive in async code Fixes #6925
2 parents 182a185 + 7a7b8bd commit 93fe356

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

clippy_lints/src/eval_order_dependence.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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};
3+
use if_chain::if_chain;
34
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
45
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Guard, HirId, Local, Node, Stmt, StmtKind};
56
use rustc_lint::{LateContext, LateLintPass};
@@ -70,20 +71,19 @@ declare_lint_pass!(EvalOrderDependence => [EVAL_ORDER_DEPENDENCE, DIVERGING_SUB_
7071
impl<'tcx> LateLintPass<'tcx> for EvalOrderDependence {
7172
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
7273
// Find a write to a local variable.
73-
match expr.kind {
74-
ExprKind::Assign(lhs, ..) | ExprKind::AssignOp(_, lhs, _) => {
75-
if let Some(var) = path_to_local(lhs) {
76-
let mut visitor = ReadVisitor {
77-
cx,
78-
var,
79-
write_expr: expr,
80-
last_expr: expr,
81-
};
82-
check_for_unsequenced_reads(&mut visitor);
83-
}
84-
},
85-
_ => {},
86-
}
74+
let var = if_chain! {
75+
if let ExprKind::Assign(lhs, ..) | ExprKind::AssignOp(_, lhs, _) = expr.kind;
76+
if let Some(var) = path_to_local(lhs);
77+
if expr.span.desugaring_kind().is_none();
78+
then { var } else { return; }
79+
};
80+
let mut visitor = ReadVisitor {
81+
cx,
82+
var,
83+
write_expr: expr,
84+
last_expr: expr,
85+
};
86+
check_for_unsequenced_reads(&mut visitor);
8787
}
8888
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
8989
match stmt.kind {
@@ -305,7 +305,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> {
305305
self.cx,
306306
EVAL_ORDER_DEPENDENCE,
307307
expr.span,
308-
"unsequenced read of a variable",
308+
&format!("unsequenced read of `{}`", self.cx.tcx.hir().name(self.var)),
309309
Some(self.write_expr.span),
310310
"whether read occurs before this write depends on evaluation order",
311311
);

tests/ui/eval_order_dependence.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// edition:2018
2+
13
#[warn(clippy::eval_order_dependence)]
24
#[allow(
35
unused_assignments,
@@ -107,3 +109,7 @@ fn main() {
107109
},
108110
);
109111
}
112+
113+
async fn issue_6925() {
114+
let _ = vec![async { true }.await, async { false }.await];
115+
}

tests/ui/eval_order_dependence.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
error: unsequenced read of a variable
2-
--> $DIR/eval_order_dependence.rs:15:9
1+
error: unsequenced read of `x`
2+
--> $DIR/eval_order_dependence.rs:17:9
33
|
44
LL | } + x;
55
| ^
66
|
77
= note: `-D clippy::eval-order-dependence` implied by `-D warnings`
88
note: whether read occurs before this write depends on evaluation order
9-
--> $DIR/eval_order_dependence.rs:13:9
9+
--> $DIR/eval_order_dependence.rs:15:9
1010
|
1111
LL | x = 1;
1212
| ^^^^^
1313

14-
error: unsequenced read of a variable
15-
--> $DIR/eval_order_dependence.rs:18:5
14+
error: unsequenced read of `x`
15+
--> $DIR/eval_order_dependence.rs:20:5
1616
|
1717
LL | x += {
1818
| ^
1919
|
2020
note: whether read occurs before this write depends on evaluation order
21-
--> $DIR/eval_order_dependence.rs:19:9
21+
--> $DIR/eval_order_dependence.rs:21:9
2222
|
2323
LL | x = 20;
2424
| ^^^^^^
2525

26-
error: unsequenced read of a variable
27-
--> $DIR/eval_order_dependence.rs:31:12
26+
error: unsequenced read of `x`
27+
--> $DIR/eval_order_dependence.rs:33:12
2828
|
2929
LL | a: x,
3030
| ^
3131
|
3232
note: whether read occurs before this write depends on evaluation order
33-
--> $DIR/eval_order_dependence.rs:33:13
33+
--> $DIR/eval_order_dependence.rs:35:13
3434
|
3535
LL | x = 6;
3636
| ^^^^^
3737

38-
error: unsequenced read of a variable
39-
--> $DIR/eval_order_dependence.rs:40:9
38+
error: unsequenced read of `x`
39+
--> $DIR/eval_order_dependence.rs:42:9
4040
|
4141
LL | x += {
4242
| ^
4343
|
4444
note: whether read occurs before this write depends on evaluation order
45-
--> $DIR/eval_order_dependence.rs:41:13
45+
--> $DIR/eval_order_dependence.rs:43:13
4646
|
4747
LL | x = 20;
4848
| ^^^^^^

0 commit comments

Comments
 (0)