Skip to content

Commit 2e91f6e

Browse files
committed
fix: use pretty_print_pat for params in fn
1 parent 75a0437 commit 2e91f6e

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed

crates/hir/src/display.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,20 @@ impl HirDisplay for Function {
9999
}
100100

101101
// FIXME: Use resolved `param.ty` once we no longer discard lifetimes
102+
let body = db.body(self.id.into());
102103
for (type_ref, param) in data.params.iter().zip(self.assoc_fn_params(db)).skip(skip_self) {
103-
let local = param.as_local(db).map(|it| it.name(db));
104104
if !first {
105105
f.write_str(", ")?;
106106
} else {
107107
first = false;
108108
}
109-
match local {
110-
Some(name) => write!(f, "{}: ", name.display(f.db.upcast(), f.edition()))?,
111-
None => f.write_str("_: ")?,
112-
}
109+
110+
let pat_id = body.params[param.idx - body.self_param.is_some() as usize];
111+
let pat_str =
112+
body.pretty_print_pat(db.upcast(), self.id.into(), pat_id, true, f.edition());
113+
f.write_str(&pat_str)?;
114+
115+
f.write_str(": ")?;
113116
type_ref.hir_fmt(f)?;
114117
}
115118

crates/ide/src/hover/tests.rs

+115
Original file line numberDiff line numberDiff line change
@@ -8742,3 +8742,118 @@ fn foo() {
87428742
"#]],
87438743
);
87448744
}
8745+
8746+
8747+
#[test]
8748+
fn test_hover_function_with_pat_param() {
8749+
check(
8750+
r#"fn test_1$0((start_range, end_range): (u32, u32), a: i32) {}"#,
8751+
expect![[r#"
8752+
*test_1*
8753+
8754+
```rust
8755+
test
8756+
```
8757+
8758+
```rust
8759+
fn test_1((start_range, end_range): (u32, u32), a: i32)
8760+
```
8761+
"#]],
8762+
);
8763+
8764+
// Test case with tuple pattern and mutable parameters
8765+
check(
8766+
r#"fn test_2$0((mut x, y): (i32, i32)) {}"#,
8767+
expect![[r#"
8768+
*test_2*
8769+
8770+
```rust
8771+
test
8772+
```
8773+
8774+
```rust
8775+
fn test_2((mut x, y): (i32, i32))
8776+
```
8777+
"#]],
8778+
);
8779+
8780+
// Test case with a pattern in a reference type
8781+
check(
8782+
r#"fn test_3$0(&(a, b): &(i32, i32)) {}"#,
8783+
expect![[r#"
8784+
*test_3*
8785+
8786+
```rust
8787+
test
8788+
```
8789+
8790+
```rust
8791+
fn test_3(&(a, b): &(i32, i32))
8792+
```
8793+
"#]],
8794+
);
8795+
8796+
// Test case with complex pattern (struct destructuring)
8797+
check(
8798+
r#"struct Point { x: i32, y: i32 } fn test_4$0(Point { x, y }: Point) {}"#,
8799+
expect![[r#"
8800+
*test_4*
8801+
8802+
```rust
8803+
test
8804+
```
8805+
8806+
```rust
8807+
fn test_4(Point { x: x, y: y, }: Point)
8808+
```
8809+
"#]],
8810+
);
8811+
8812+
// Test case with a nested pattern
8813+
check(
8814+
r#"fn test_5$0(((a, b), c): ((i32, i32), i32)) {}"#,
8815+
expect![[r#"
8816+
*test_5*
8817+
8818+
```rust
8819+
test
8820+
```
8821+
8822+
```rust
8823+
fn test_5(((a, b), c): ((i32, i32), i32))
8824+
```
8825+
"#]],
8826+
);
8827+
8828+
// Test case with an unused variable in the pattern
8829+
check(
8830+
r#"fn test_6$0((_, y): (i32, i64)) {}"#,
8831+
expect![[r#"
8832+
*test_6*
8833+
8834+
```rust
8835+
test
8836+
```
8837+
8838+
```rust
8839+
fn test_6((_, y): (i32, i64))
8840+
```
8841+
"#]],
8842+
);
8843+
8844+
// Test case with a complex pattern involving both tuple and struct
8845+
check(
8846+
r#"struct Foo { a: i32, b: i32 } fn test_7$0((x, Foo { a, b }): (i32, Foo)) {}"#,
8847+
expect![[r#"
8848+
*test_7*
8849+
8850+
```rust
8851+
test
8852+
```
8853+
8854+
```rust
8855+
fn test_7((x, Foo { a: a, b: b, }): (i32, Foo))
8856+
```
8857+
"#]],
8858+
);
8859+
}

0 commit comments

Comments
 (0)