Skip to content

Unhelpful error message not pointing to the issue #119665

@xNxExOx

Description

@xNxExOx

Code

struct MyStruct {
    foo: String
}

fn ok() -> MyStruct {
    MyStruct {
        foo: std::env::temp_dir().join("something").to_str().unwrap().to_string()
    }
}

fn bad_refactor() -> MyStruct {
    let foo: std::env::temp_dir().join("something").to_str().unwrap().to_string();
    MyStruct {
        foo
    }
}

Current output

error: expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `.`
  --> src/main.rs:14:34
   |
12 |     let foo: std::env::temp_dir().join("something").to_str().unwrap().to_string();
   |                                  ^ expected one of `!`, `+`, `->`, `::`, `;`, or `=`

Desired output

error: did you mean `=` instead of `:`?
  --> src/main.rs:14:34
   |
12 |     let foo: std::env::temp_dir().join("something").to_str().unwrap().to_string();
   |            ^ did you mean `=` instead of `:`?

OR
error: expected type, found function `expresion`
  --> src/main.rs:14:34
   |
12 |     let foo: std::env::temp_dir().join("something").to_str().unwrap().to_string();
   |            --^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a type
   |            |
   |            help: use `=` if you meant to assign

Rationale and extra context

I was just been asked about this in not too much complicated code, and we was both thinking, why join does not work, if there needs to be some use ... to bring it to the scope.

When I tried to write the simplified example I got much better error. (help: use `=` if you meant to assign)

Other cases

fn ok2() -> MyStruct {
    MyStruct {
        foo: some_fn()
    }
}

fn bad_refactor2() -> MyStruct {
    let foo: some_fn();
    MyStruct {
        foo
    }
}

fn some_fn() -> String {
    "".into()
}

// produces:
error[E0573]: expected type, found function `some_fn`
  --> src/main.rs:25:14
   |
25 |     let foo: some_fn();
   |            --^^^^^^^^^ not a type
   |            |
   |            help: use `=` if you meant to assign

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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