Skip to content

Suggest if let when a failed if block could be parsed as one #44990

Closed

Description

@estebank

When writing if Some(x) = foo {}, attempt to parse as if let Some(x) = foo {} for better diagnostic output and if successful provide a suggestion to add the let.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Oct 3, 2017
  2. cramertj commented on Oct 3, 2017

    @cramertj
    Member

    Is if let the right suggestion here? Often the code would be fixed by just adding an extra =. This code is perfectly valid, for example:

    fn main() {
        let x = 0;
        let foo = Some(0);
        if Some(x) == foo {}
    }
  3. estebank commented on Oct 3, 2017

    @estebank
    ContributorAuthor

    I think it would if x wasn't in the scope. We can't know that during parsing, but the errors for

    fn main() {
        let foo = Some(0);
        if Some(x) = foo {}
    }

    are

    error[E0425]: cannot find value `x` in this scope
     --> src/main.rs:3:13
      |
    3 |     if Some(x) = foo {}
      |             ^ not found in this scope
    
    error[E0308]: mismatched types
     --> src/main.rs:3:8
      |
    3 |     if Some(x) = foo {}
      |        ^^^^^^^^^^^^^
      |        |
      |        help: did you mean to compare equality?: `Some(x) == foo`
      |        expected bool, found ()
      |
      = note: expected type `bool`
                 found type `()`
    

    Which tells me we could catch not found in this scope errors that occur on if conditions and try them as if let. If it succeeds to parse the rest of the block, then emit the original error with an extra suggestion.

    For the case where let x = 0 is in the code, the output is only the mismatched types error.

  4. estebank commented on May 22, 2019

    @estebank
    ContributorAuthor

    cc @Centril, given the recent it let desugaring work.

  5. Centril commented on May 23, 2019

    @Centril
    Contributor

    @estebank Added to the tracking issue for list of potential issues to work on; tho since it is preexisting, not as a blocker but a nice-to-have.

  6. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    on Jan 22, 2020
  7. added a commit that references this issue on Sep 1, 2020
    d824b23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-let-chainsArea: let chains (if-let, while-let, ...)C-enhancementCategory: An issue proposing an enhancement or a PR with one.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.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions