about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2021-10-03 14:45:21 +0200
committerLukas Wirth <lukastw97@gmail.com>2021-10-03 14:53:18 +0200
commit992b4648d9f6a186c7c444a4db36b2bba00b09a5 (patch)
tree9148fbd43f1e92e76273566800fe0a09cc520629
parent5b0c91d11844ee3975476cd2c7bf42af480d7781 (diff)
downloadrust-992b4648d9f6a186c7c444a4db36b2bba00b09a5.tar.gz
rust-992b4648d9f6a186c7c444a4db36b2bba00b09a5.zip
feat: Hide type inlay hints for constructors
-rw-r--r--crates/ide/src/inlay_hints.rs96
-rw-r--r--crates/test_utils/src/minicore.rs11
2 files changed, 102 insertions, 5 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index 401feffff42..8dfceb35aff 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -198,28 +198,69 @@ fn get_bind_pat_hints(
 
     let descended = sema.descend_node_into_attributes(pat.clone()).pop();
     let desc_pat = descended.as_ref().unwrap_or(pat);
-    let krate = sema.scope(desc_pat.syntax()).module().map(|it| it.krate());
-    let famous_defs = FamousDefs(sema, krate);
-
     let ty = sema.type_of_pat(&desc_pat.clone().into())?.original;
 
     if should_not_display_type_hint(sema, &pat, &ty) {
         return None;
     }
 
+    let krate = sema.scope(desc_pat.syntax()).module().map(|it| it.krate());
+    let famous_defs = FamousDefs(sema, krate);
+    let label = hint_iterator(sema, &famous_defs, config, &ty);
+
+    let label = match label {
+        Some(label) => label,
+        None => {
+            let ty = ty.display_truncated(sema.db, config.max_length).to_string();
+            if Some(&*ty) == get_constructor_name(sema, pat).as_deref() {
+                return None;
+            }
+            ty.into()
+        }
+    };
+
     acc.push(InlayHint {
         range: match pat.name() {
             Some(name) => name.syntax().text_range(),
             None => pat.syntax().text_range(),
         },
         kind: InlayKind::TypeHint,
-        label: hint_iterator(sema, &famous_defs, config, &ty)
-            .unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string().into()),
+        label,
     });
 
     Some(())
 }
 
+fn get_constructor_name(sema: &Semantics<RootDatabase>, pat: &ast::IdentPat) -> Option<String> {
+    let it = pat.syntax().parent()?;
+    let expr = match_ast! {
+        match it {
+            ast::LetStmt(it) => it.initializer(),
+            ast::Condition(it) => it.expr(),
+            _ => None,
+        }
+    };
+
+    if let Some(expr) = expr {
+        let expr = sema.descend_node_into_attributes(expr.clone()).pop().unwrap_or(expr);
+        let expr = match expr {
+            ast::Expr::TryExpr(it) => it.expr(),
+            ast::Expr::AwaitExpr(it) => it.expr(),
+            expr => Some(expr),
+        }?;
+        let path = match expr {
+            ast::Expr::CallExpr(call) => match call.expr()? {
+                ast::Expr::PathExpr(p) => p.path(),
+                _ => None,
+            },
+            _ => None,
+        }?;
+        let seg = path.qualifier()?.segment()?;
+        return Some(seg.to_string());
+    }
+    None
+}
+
 /// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator<Item = Ty>`.
 fn hint_iterator(
     sema: &Semantics<RootDatabase>,
@@ -1235,6 +1276,51 @@ fn main() {
     }
 
     #[test]
+    fn skip_constructor_type_hints() {
+        check_types(
+            r#"
+//- minicore: try
+use core::ops::ControlFlow;
+
+struct Struct;
+struct TupleStruct();
+
+impl Struct {
+    fn new() -> Self {
+        Struct
+    }
+    fn try_new() -> ControlFlow<(), Self> {
+        ControlFlow::Continue(Struct)
+    }
+}
+
+struct Generic<T>(T);
+impl Generic<i32> {
+    fn new() -> Self {
+        Generic(0)
+    }
+}
+
+fn main() {
+    let strukt = Struct::new();
+    let tuple_struct = TupleStruct();
+     // ^^^^^^^^^^^^ TupleStruct
+    let generic0 = Generic::new();
+     // ^^^^^^^^ Generic<i32>
+    let generic1 = Generic::<i32>::new();
+     // ^^^^^^^^ Generic<i32>
+    let generic2 = <Generic<i32>>::new();
+     // ^^^^^^^^ Generic<i32>
+}
+
+fn fallible() -> ControlFlow<()> {
+    let strukt = Struct::try_new()?;
+}
+"#,
+        );
+    }
+
+    #[test]
     fn closures() {
         check(
             r#"
diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs
index 5e17047a407..045b4898e50 100644
--- a/crates/test_utils/src/minicore.rs
+++ b/crates/test_utils/src/minicore.rs
@@ -300,6 +300,17 @@ pub mod ops {
             #[lang = "branch"]
             fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
         }
+
+        impl<B, C> Try for ControlFlow<B, C> {
+            type Output = C;
+            type Residual = ControlFlow<B, convert::Infallible>;
+            fn from_output(output: Self::Output) -> Self {}
+            fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {}
+        }
+
+        impl<B, C> FromResidual for ControlFlow<B, C> {
+            fn from_residual(residual: ControlFlow<B, convert::Infallible>) -> Self {}
+        }
     }
     pub use self::try_::{ControlFlow, FromResidual, Try};
     // endregion:try