]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / ide / src / inlay_hints / closure_ret.rs
1 //! Implementation of "closure return type" inlay hints.
2 use ide_db::{base_db::FileId, famous_defs::FamousDefs};
3 use syntax::ast::{self, AstNode};
4
5 use crate::{
6 inlay_hints::closure_has_block_body, ClosureReturnTypeHints, InlayHint, InlayHintsConfig,
7 InlayKind, InlayTooltip,
8 };
9
10 use super::label_of_ty;
11
12 pub(super) fn hints(
13 acc: &mut Vec<InlayHint>,
14 famous_defs @ FamousDefs(sema, _): &FamousDefs<'_, '_>,
15 config: &InlayHintsConfig,
16 file_id: FileId,
17 closure: ast::ClosureExpr,
18 ) -> Option<()> {
19 if config.closure_return_type_hints == ClosureReturnTypeHints::Never {
20 return None;
21 }
22
23 if closure.ret_type().is_some() {
24 return None;
25 }
26
27 if !closure_has_block_body(&closure)
28 && config.closure_return_type_hints == ClosureReturnTypeHints::WithBlock
29 {
30 return None;
31 }
32
33 let param_list = closure.param_list()?;
34
35 let closure = sema.descend_node_into_attributes(closure).pop()?;
36 let ty = sema.type_of_expr(&ast::Expr::ClosureExpr(closure))?.adjusted();
37 let callable = ty.as_callable(sema.db)?;
38 let ty = callable.return_type();
39 if ty.is_unit() {
40 return None;
41 }
42 acc.push(InlayHint {
43 range: param_list.syntax().text_range(),
44 kind: InlayKind::ClosureReturnTypeHint,
45 label: label_of_ty(famous_defs, config, ty)?,
46 tooltip: Some(InlayTooltip::HoverRanged(file_id, param_list.syntax().text_range())),
47 });
48 Some(())
49 }