mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-28 18:43:01 +00:00
Merge pull request #20611 from A4-Tacks/replace-arith-op-prec
Fix precedence parenthesis for replace_arith_op
This commit is contained in:
commit
6135c07683
1 changed files with 22 additions and 1 deletions
|
|
@ -88,7 +88,11 @@ fn replace_arith(acc: &mut Assists, ctx: &AssistContext<'_>, kind: ArithKind) ->
|
||||||
|builder| {
|
|builder| {
|
||||||
let method_name = kind.method_name(op);
|
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]
|
#[test]
|
||||||
fn replace_arith_not_applicable_with_non_empty_selection() {
|
fn replace_arith_not_applicable_with_non_empty_selection() {
|
||||||
check_assist_not_applicable(
|
check_assist_not_applicable(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue