Skip to content

E0277 should put emphasis on containing function's return type #63078

Closed

Description

I got the following error today:

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
  --> src/main.rs:29:20
   |
29 |     let mut resp = reqwest::get("...")?;
   |                    ^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_error`

I was confused for a minute as I misinterpreted what the error was saying. I double-checked that reqwest::get does indeed return a Result but a moment later I realized that it's the function that's containing reqwest::get I should be looking at which indeed did return () after which I fixed the issue.

I believe this error message could be somehow improved and/or worded differently to put more stress on the containing function - after all, that's what I should've looked at (maybe the containing function should be even shown in the error message like reqwest::get is?) though I can see how it can not always be the case. However, I do still think both the containing method's return type and the reqwest::get call should be highlighted for the best experience and to easily allow a full understanding of what's happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.F-on_unimplementedError messages that can be tackled with `#[rustc_on_unimplemented]`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