Skip to content

clippy::clone_on_copy fix on code with ? leads to code with wrong operator precedence #9277

@SillyFreak

Description

@SillyFreak

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions