Skip to content

Suggest not using method call syntax when type can't be inferred #118804

Open
@dullbananas

Description

@dullbananas

Code

struct S<T> {
    f: fn(T) -> T,
}

fn main() {
    let mut n = 0i32;
    let s = S {
        f: |x| x.clone(),
    };
    n = (s.f)(n);
}

Current output

error[E0282]: type annotations needed
 --> src/main.rs:8:13
  |
8 |         f: |x| x.clone(),
  |             ^  - type must be known at this point
  |
help: consider giving this closure parameter an explicit type
  |
8 |         f: |x: /* Type */| x.clone(),
  |              ++++++++++++

Desired output

error[E0282]: type annotations needed
 --> src/main.rs:8:13
  |
8 |         f: |x| x.clone(),
  |             ^  - type must be known at this point
  |
help: consider giving this closure parameter an explicit type
  |
8 |         f: |x: /* Type */| x.clone(),
  |              ++++++++++++
  |
help: consider calling the function directly
  |
8 |         f: |x| Clone::clone(&x),

Rationale and extra context

Calling the function directly gets rid of the "type annotations needed" error

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions