about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorroife <roifewu@gmail.com>2024-04-20 10:07:33 +0800
committerroife <roifewu@gmail.com>2024-04-20 10:07:33 +0800
commitf7f02c0d127935aecf11b24420cdaba2cdfe0494 (patch)
tree227d454c458e917397870726839f8638944fdb49 /src
parent0e9d845fc79b368795268431cd26a3865d4414b6 (diff)
downloadrust-f7f02c0d127935aecf11b24420cdaba2cdfe0494.tar.gz
rust-f7f02c0d127935aecf11b24420cdaba2cdfe0494.zip
fix: remove space within `{}` when no fields in struct
Diffstat (limited to 'src')
-rw-r--r--src/tools/rust-analyzer/crates/hir/src/display.rs24
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/hover/tests.rs22
2 files changed, 35 insertions, 11 deletions
diff --git a/src/tools/rust-analyzer/crates/hir/src/display.rs b/src/tools/rust-analyzer/crates/hir/src/display.rs
index 1c3eac1590d..c276e87786d 100644
--- a/src/tools/rust-analyzer/crates/hir/src/display.rs
+++ b/src/tools/rust-analyzer/crates/hir/src/display.rs
@@ -249,19 +249,23 @@ fn display_fields(
         }
     } else {
         f.write_char('{')?;
-        f.write_char(separator)?;
-        for field in &fields[..count] {
-            f.write_str(indent)?;
-            field.hir_fmt(f)?;
-            f.write_char(',')?;
-            f.write_char(separator)?;
-        }
 
-        if fields.len() > count {
-            f.write_str(indent)?;
-            f.write_str("/* … */")?;
+        if !fields.is_empty() {
             f.write_char(separator)?;
+            for field in &fields[..count] {
+                f.write_str(indent)?;
+                field.hir_fmt(f)?;
+                f.write_char(',')?;
+                f.write_char(separator)?;
+            }
+
+            if fields.len() > count {
+                f.write_str(indent)?;
+                f.write_str("/* … */")?;
+                f.write_char(separator)?;
+            }
         }
+
         f.write_str("}")?;
     }
 
diff --git a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
index ff3258b4dbb..52e57d78892 100644
--- a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
@@ -1039,7 +1039,27 @@ fn hover_record_struct_limit() {
             struct Foo { /* … */ }
             ```
         "#]],
-    )
+    );
+
+    // No extra spaces within `{}` when there are no fields
+    check_hover_fields_limit(
+        5,
+        r#"
+    struct Foo$0 {}
+    "#,
+        expect![[r#"
+            *Foo*
+
+            ```rust
+            test
+            ```
+
+            ```rust
+            // size = 0, align = 1
+            struct Foo {}
+            ```
+        "#]],
+    );
 }
 
 #[test]