ide_diagnostics/handlers/
expected_function.rs

1use hir::HirDisplay;
2
3use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
4
5// Diagnostic: expected-function
6//
7// This diagnostic is triggered if a call is made on something that is not callable.
8pub(crate) fn expected_function(
9    ctx: &DiagnosticsContext<'_>,
10    d: &hir::ExpectedFunction<'_>,
11) -> Diagnostic {
12    Diagnostic::new_with_syntax_node_ptr(
13        ctx,
14        DiagnosticCode::RustcHardError("E0618"),
15        format!("expected function, found {}", d.found.display(ctx.sema.db, ctx.display_target)),
16        d.call.map(|it| it.into()),
17    )
18}
19
20#[cfg(test)]
21mod tests {
22    use crate::tests::check_diagnostics;
23
24    #[test]
25    fn smoke_test() {
26        check_diagnostics(
27            r#"
28fn foo() {
29    let x = 3;
30    x();
31 // ^^^ error: expected function, found i32
32    ""();
33 // ^^^^ error: expected function, found &'static str
34    foo();
35}
36"#,
37        );
38    }
39
40    #[test]
41    fn no_error_for_async_fn_traits() {
42        check_diagnostics(
43            r#"
44//- minicore: async_fn
45async fn f(it: impl AsyncFn(u32) -> i32) {
46    let fut = it(0);
47    let _: i32 = fut.await;
48}
49async fn g(mut it: impl AsyncFnMut(u32) -> i32) {
50    let fut = it(0);
51    let _: i32 = fut.await;
52}
53async fn h(it: impl AsyncFnOnce(u32) -> i32) {
54    let fut = it(0);
55    let _: i32 = fut.await;
56}
57        "#,
58        );
59    }
60}