Skip to content

Commit 7cbc6ae

Browse files
committed
ide: hover omits unnamed where preds
1 parent 14dff25 commit 7cbc6ae

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

crates/hir/src/display.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,21 @@ fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), H
325325

326326
fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
327327
let params = f.db.generic_params(def);
328-
if params.where_predicates.is_empty() {
328+
329+
// unnamed type targets are displayed inline with the argument itself, e.g. `f: impl Y`.
330+
let is_unnamed_type_target = |target: &WherePredicateTypeTarget| match target {
331+
WherePredicateTypeTarget::TypeRef(_) => false,
332+
WherePredicateTypeTarget::TypeParam(id) => params.types[*id].name.is_none(),
333+
};
334+
335+
let has_displayable_predicate = params
336+
.where_predicates
337+
.iter()
338+
.any(|pred| {
339+
!matches!(pred, WherePredicate::TypeBound { target, .. } if is_unnamed_type_target(target))
340+
});
341+
342+
if !has_displayable_predicate {
329343
return Ok(());
330344
}
331345

@@ -348,6 +362,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
348362
};
349363

350364
match pred {
365+
WherePredicate::TypeBound { target, .. } if is_unnamed_type_target(target) => {}
351366
WherePredicate::TypeBound { target, bound } => {
352367
if matches!(prev_pred, Some(WherePredicate::TypeBound { target: target_, .. }) if target_ == target)
353368
{

crates/ide/src/hover/tests.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,48 @@ fn main() { m::f$0oo(); }
340340
);
341341
}
342342

343+
#[test]
344+
fn hover_omits_unnamed_where_preds() {
345+
check(
346+
r#"
347+
pub fn foo(bar: impl T) { }
348+
349+
fn main() { fo$0o(); }
350+
"#,
351+
expect![[r#"
352+
*foo*
353+
354+
```rust
355+
test
356+
```
357+
358+
```rust
359+
pub fn foo(bar: impl T)
360+
```
361+
"#]],
362+
);
363+
check(
364+
r#"
365+
pub fn foo<V: AsRef<str>>(bar: impl T, baz: V) { }
366+
367+
fn main() { fo$0o(); }
368+
"#,
369+
expect![[r#"
370+
*foo*
371+
372+
```rust
373+
test
374+
```
375+
376+
```rust
377+
pub fn foo<V>(bar: impl T, baz: V)
378+
where
379+
V: AsRef<str>,
380+
```
381+
"#]],
382+
);
383+
}
384+
343385
#[test]
344386
fn hover_shows_fn_signature_with_type_params() {
345387
check(

0 commit comments

Comments
 (0)