Skip to content

suggest replacing let else with ? statement where applicable #8755

Closed

Description

@matthiaskrgr

Sorry, filed via mobil which doesn't know about issue templates. 😅

might be nice if clippy could lint patterns like this: rust-lang/rust#96471

Activity

  1. est31 commented on Apr 27, 2022

    @est31
    Member

    I guess the question_mark lint could be extended to handle this case? Also, maybe one needs to watch out to not suggest let-else first (#8437) to then suggest ?, but suggest ? right away. IDK how conflicts like this are handled.

  2. est31 commented on Jan 11, 2023

    @est31
    Member

    Another example where this lint would have been useful: rust-lang/cargo@cf716fc

  3. est31 commented on Jan 11, 2023

    @est31
    Member

    Given the two samples that we have, I think that most of the times the lint would fire is for functions that return Option. If we simplify for Option, that means that checking for the pattern should be pretty easy. For Result it doesn't make that much sense, as the else clause can't access the thing inside Err.

  4. MarijnS95 commented on Jan 24, 2023

    @MarijnS95
    Contributor

    Yes, would love to see the question_mark lint extended to let-else, and also the mention by @est31 to not first suggest manual_let_else on a block of code, followed by question_mark on the refactored code.

    We currently have two patterns in our codebase with both Option and Result where the question mark operator could/should be suggested:

        let x = None::<u32>;
        // Suggest ? here
        let Some(x) = x else {
            return None;
        };
    
        // Suggest .ok()? here
        let Ok(x) = std::fs::read_to_string("foo") else {
            return None;
        };

    Thanks!

  5. added a commit that references this issue on Jul 3, 2023
    3f17c5c
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions