diff options
| -rw-r--r-- | crates/hir/src/display.rs | 17 | ||||
| -rw-r--r-- | crates/ide/src/hover/tests.rs | 42 |
2 files changed, 58 insertions, 1 deletions
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index e9ace855fc3..38bbf417971 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -325,7 +325,21 @@ fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), H fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), HirDisplayError> { let params = f.db.generic_params(def); - if params.where_predicates.is_empty() { + + // unnamed type targets are displayed inline with the argument itself, e.g. `f: impl Y`. + let is_unnamed_type_target = |target: &WherePredicateTypeTarget| match target { + WherePredicateTypeTarget::TypeRef(_) => false, + WherePredicateTypeTarget::TypeParam(id) => params.types[*id].name.is_none(), + }; + + let has_displayable_predicate = params + .where_predicates + .iter() + .any(|pred| { + !matches!(pred, WherePredicate::TypeBound { target, .. } if is_unnamed_type_target(target)) + }); + + if !has_displayable_predicate { return Ok(()); } @@ -348,6 +362,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir }; match pred { + WherePredicate::TypeBound { target, .. } if is_unnamed_type_target(target) => {} WherePredicate::TypeBound { target, bound } => { if matches!(prev_pred, Some(WherePredicate::TypeBound { target: target_, .. }) if target_ == target) { diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 5db6fd79749..c745f86d84e 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -341,6 +341,48 @@ fn main() { m::f$0oo(); } } #[test] +fn hover_omits_unnamed_where_preds() { + check( + r#" +pub fn foo(bar: impl T) { } + +fn main() { fo$0o(); } + "#, + expect![[r#" + *foo* + + ```rust + test + ``` + + ```rust + pub fn foo(bar: impl T) + ``` + "#]], + ); + check( + r#" +pub fn foo<V: AsRef<str>>(bar: impl T, baz: V) { } + +fn main() { fo$0o(); } + "#, + expect![[r#" + *foo* + + ```rust + test + ``` + + ```rust + pub fn foo<V>(bar: impl T, baz: V) + where + V: AsRef<str>, + ``` + "#]], + ); +} + +#[test] fn hover_shows_fn_signature_with_type_params() { check( r#" |
