Merge pull request #20611 from A4-Tacks/replace-arith-op-prec

Fix precedence parenthesis for replace_arith_op
This commit is contained in:
Shoyu Vanilla (Flint) 2025-09-26 08:30:46 +00:00 committed by GitHub
commit 6135c07683
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -88,7 +88,11 @@ fn replace_arith(acc: &mut Assists, ctx: &AssistContext<'_>, kind: ArithKind) ->
|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})"))
}
},
)
}
@ -227,6 +231,23 @@ fn main() {
)
}
#[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(