]> git.lizzy.rs Git - rust.git/blob - src/docs/mixed_read_write_in_expression.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / mixed_read_write_in_expression.txt
1 ### What it does
2 Checks for a read and a write to the same variable where
3 whether the read occurs before or after the write depends on the evaluation
4 order of sub-expressions.
5
6 ### Why is this bad?
7 It is often confusing to read. As described [here](https://doc.rust-lang.org/reference/expressions.html?highlight=subexpression#evaluation-order-of-operands),
8 the operands of these expressions are evaluated before applying the effects of the expression.
9
10 ### Known problems
11 Code which intentionally depends on the evaluation
12 order, or which is correct for any evaluation order.
13
14 ### Example
15 ```
16 let mut x = 0;
17
18 let a = {
19     x = 1;
20     1
21 } + x;
22 // Unclear whether a is 1 or 2.
23 ```
24
25 Use instead:
26 ```
27 let tmp = {
28     x = 1;
29     1
30 };
31 let a = tmp + x;
32 ```