Skip to content

Commit 8f8ef03

Browse files
committed
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); } ```
1 parent 1f4e5e8 commit 8f8ef03

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

crates/ide-assists/src/handlers/replace_arith_op.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ fn replace_arith(acc: &mut Assists, ctx: &AssistContext<'_>, kind: ArithKind) ->
8888
|builder| {
8989
let method_name = kind.method_name(op);
9090

91-
builder.replace(range, format!("{lhs}.{method_name}({rhs})"))
91+
if lhs.precedence().needs_parentheses_in(ast::prec::ExprPrecedence::Postfix) {
92+
builder.replace(range, format!("({lhs}).{method_name}({rhs})"))
93+
} else {
94+
builder.replace(range, format!("{lhs}.{method_name}({rhs})"))
95+
}
9296
},
9397
)
9498
}
@@ -227,6 +231,23 @@ fn main() {
227231
)
228232
}
229233

234+
#[test]
235+
fn replace_arith_with_wrapping_add_add_parenthesis() {
236+
check_assist(
237+
replace_arith_with_wrapping,
238+
r#"
239+
fn main() {
240+
let x = 1*x $0+ 2;
241+
}
242+
"#,
243+
r#"
244+
fn main() {
245+
let x = (1*x).wrapping_add(2);
246+
}
247+
"#,
248+
)
249+
}
250+
230251
#[test]
231252
fn replace_arith_not_applicable_with_non_empty_selection() {
232253
check_assist_not_applicable(

0 commit comments

Comments
 (0)