Summary
? has higher precedence than * (dereference), but cargo clippy --fix will not parenthesize an expression when it introduces a * in an expression ending in ?, at least for the clone_on_copy lint, leading to broken code.
The correct fix requiring explicit parentheses could be seen as the lint being a false positive in that case - that's subjective of course.
Reproducer
I tried this code:
fn foo() -> Option<()> {
let opt = &None;
let value = opt.clone()?; // <--
Some(value)
}
I expected to see this happen: The marked line should be fixed to be let value = (*opt)?;
Instead, this happened: The line became let value = *opt?;, i.e. equivalent to let value = *(opt?);, which does not compile.
Version
rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-unknown-linux-gnu
release: 1.62.0
LLVM version: 14.0.5
Additional Labels
@rustbot label +I-suggestion-causes-error
Summary
?has higher precedence than*(dereference), butcargo clippy --fixwill not parenthesize an expression when it introduces a*in an expression ending in?, at least for theclone_on_copylint, leading to broken code.The correct fix requiring explicit parentheses could be seen as the lint being a false positive in that case - that's subjective of course.
Reproducer
I tried this code:
I expected to see this happen: The marked line should be fixed to be
let value = (*opt)?;Instead, this happened: The line became
let value = *opt?;, i.e. equivalent tolet value = *(opt?);, which does not compile.Version
Additional Labels
@rustbot label +I-suggestion-causes-error