Closed
Description
Lint name: eval_order_dependence
The documentation states:
In addition, the sub-expression evaluation order for Rust is not well documented.
Since the merging of Document execution order, is this not outdated advice? Sub-expression/ operand evaluation is now documented here.
Discussion
Take the following code, which is based on a heavily distilled macro expansion:
pub fn case_0() {
let mut count = 0;
let arr = [
{
let v = count;
count += 1;
v
},
{
let v = count;
count += 1;
v
},
{
let v = count;
count += 1;
v
},
{ count },
];
assert_eq!(arr, [0, 1, 2, 3]);
}
It fires off the warning: unsequenced read of 'count'
/ whether read occurs before this write depends on evaluation order
.
Fair enough. The code looks messy and the evaluation order is something we need to consider. However if we then read the documentation:
In addition, the sub-expression evaluation order for Rust is not well documented.
We are left with the impression that we have potentially undefined behaviour and unsound code.
However, based on my understanding of the updated reference, the evaluation order is well defined. The code is sound.
The following list of expressions all evaluate their operands the same way, as described after the list.
Other expressions either don't take operands or evaluate them conditionally as described on their respective pages.
Dereference expression
Error propagation expression
Negation expression
Arithmetic and logical binary operators
Comparison operators
Type cast expression
Grouped expression
Array expression
Await expression
Index expression
Tuple expression
Tuple index expression
Struct expression
Call expression
Method call expression
Field expression
Break expression
Range expression
Return expression
The operands of these expressions are evaluated prior to applying the effects of the expression.
Expressions taking multiple operands are evaluated left to right as written in the source code.
Meta
clippy 0.1.54 (a178d03 2021-07-26)
rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-unknown-linux-gnu
release: 1.54.0
LLVM version: 12.0.1
```.