about summary refs log tree commit diff
diff options
context:
space:
mode:
authorShoyu Vanilla (Flint) <modulo641@gmail.com>2025-09-26 08:30:46 +0000
committerGitHub <noreply@github.com>2025-09-26 08:30:46 +0000
commita7df846d55a31b3c4cd796a9a8628f60938ec4c5 (patch)
treec0ab1e0ee20e54f8cb21b16d11d32d68559b53d0
parenteb0cacbd322380f97e71e94f5a4ca7fb12a9b22d (diff)
parentec34e8e69fb2e133e6ec0ebc87443f6fa8bda092 (diff)
downloadrust-a7df846d55a31b3c4cd796a9a8628f60938ec4c5.tar.gz
rust-a7df846d55a31b3c4cd796a9a8628f60938ec4c5.zip
Merge pull request #20611 from A4-Tacks/replace-arith-op-prec
Fix precedence parenthesis for replace_arith_op
-rw-r--r--src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_arith_op.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_arith_op.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_arith_op.rs
index 440ab4d4604..94ac1c342d3 100644
--- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_arith_op.rs
+++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_arith_op.rs
@@ -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})"))
+            }
         },
     )
 }
@@ -228,6 +232,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(
             replace_arith_with_checked,