Skip to content

Suggest changing -> T<U> to a -> T<&U> when trying to return a T<&U> #100699

Closed
@DropDemBits

Description

@DropDemBits

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a33eca3ff60c0fa5b2f73dc3e34c362f

fn bing<'a>() -> Option<&'a ()> {
    None
}

struct Thing;

impl Thing {
    fn bang(&self) -> Option<()> {
        bing()
    }
}

The current output is:

error[E0308]: mismatched types
 --> src/lib.rs:9:9
  |
8 |     fn bang(&self) -> Option<()> {
  |                       ---------- expected `Option<()>` because of return type
9 |         bing()
  |         ^^^^^^ expected `()`, found `&()`
  |
  = note: expected enum `Option<()>`
             found enum `Option<&()>`

Ideally, there should be a suggestion to add a & to the inner type

  = help: try adding a `&` to the inner type:
  |
8 |     fn bang(&self) -> Option<&()> {
  |                              +

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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