Fix precedence parenthesis for replace_arith_op
Example
---
```rust
fn main() {
let x = 1*x $0+ 2;
}
```
**Before this PR**:
```rust
fn main() {
let x = 1*x.wrapping_add(2);
}
```
**After this PR**:
```rust
fn main() {
let x = (1*x).wrapping_add(2);
}
```
diff --git a/crates/ide-assists/src/handlers/replace_arith_op.rs b/crates/ide-assists/src/handlers/replace_arith_op.rs
index 440ab4d..94ac1c3 100644
--- a/crates/ide-assists/src/handlers/replace_arith_op.rs
+++ b/crates/ide-assists/src/handlers/replace_arith_op.rs
@@ -88,7 +88,11 @@
|builder| {
let method_name = kind.method_name(op);
- builder.replace(range, format!("{lhs}.{method_name}({rhs})"))
+ if lhs.precedence().needs_parentheses_in(ast::prec::ExprPrecedence::Postfix) {
+ builder.replace(range, format!("({lhs}).{method_name}({rhs})"))
+ } else {
+ builder.replace(range, format!("{lhs}.{method_name}({rhs})"))
+ }
},
)
}
@@ -228,6 +232,23 @@
}
#[test]
+ fn replace_arith_with_wrapping_add_add_parenthesis() {
+ check_assist(
+ replace_arith_with_wrapping,
+ r#"
+fn main() {
+ let x = 1*x $0+ 2;
+}
+"#,
+ r#"
+fn main() {
+ let x = (1*x).wrapping_add(2);
+}
+"#,
+ )
+ }
+
+ #[test]
fn replace_arith_not_applicable_with_non_empty_selection() {
check_assist_not_applicable(
replace_arith_with_checked,