Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement to if_let_some_result: apply to while loops as well #7594

Closed
dllu opened this issue Aug 23, 2021 · 2 comments
Closed

enhancement to if_let_some_result: apply to while loops as well #7594

dllu opened this issue Aug 23, 2021 · 2 comments
Assignees
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@dllu
Copy link

dllu commented Aug 23, 2021

What it does

Where res is a Result,

while let Some(a) = res.ok() {

should be rewritten as

while let Ok(a) = res {

This could either be an enhancement to if_let_some_result or have a new name like while_let_some_result.

SEE ALSO: #7586 where previously clippy suggested an incorrect fix (changing while to if). In that thread, @camsteffen suggested creating a new issue for this.

Categories (optional)

  • Kind: style

What is the advantage of the recommended code over the original code

Conciseness etc.

It maintains consistency with the existent if_let_some_result and has the same advantages, except now for while loops too.

Drawbacks

None.

Example

struct Wat {
    counter: i32,
}

impl Wat {
    fn next(&mut self) -> Result<i32, &str> {
        self.counter += 1;
        if self.counter < 5 {
            Ok(self.counter)
        } else {
            Err("Oh no")
        }
    }
}

fn main() {
    let mut wat = Wat { counter: 0 };
    while let Some(a) = wat.next().ok() { // <---- here
        dbg!(&a);
    }
}

Could be written as:

    while let Ok(a) = wat.next() {
@dllu dllu added the A-lint Area: New lints label Aug 23, 2021
@camsteffen camsteffen added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy and removed A-lint Area: New lints labels Aug 23, 2021
@andrewpollack
Copy link
Member

@rustbot claim

bors added a commit that referenced this issue Sep 28, 2021
…ishearth

#7594: Adding new 'while_let_some_result' linting

Excited for the opportunity to contribute to Clippy! Since the latest Bay Area Rust Meetup, I've been looking for an opportunity and found it 😄

Please let me know how I can improve this PR! Much of it is similar to ``[`if_let_some_result`]``, but I followed the description in the issue to create its own linting rules. Looking forward to feedback, and continuing to work on Clippy in the future!

changelog:
- Renamed Lint: `if_let_some_result` is now called [`match_result_ok`]. Now also handles `while let` case.
bors added a commit that referenced this issue Sep 28, 2021
…ishearth

#7594: Adding new 'while_let_some_result' linting

Excited for the opportunity to contribute to Clippy! Since the latest Bay Area Rust Meetup, I've been looking for an opportunity and found it 😄

Please let me know how I can improve this PR! Much of it is similar to ``[`if_let_some_result`]``, but I followed the description in the issue to create its own linting rules. Looking forward to feedback, and continuing to work on Clippy in the future!

changelog: Renamed Lint: `if_let_some_result` is now called [`match_result_ok`]. Now also handles `while let` case.
bors added a commit that referenced this issue Sep 28, 2021
…ishearth

#7594: Adding new 'while_let_some_result' linting

Excited for the opportunity to contribute to Clippy! Since the latest Bay Area Rust Meetup, I've been looking for an opportunity and found it 😄

Please let me know how I can improve this PR! Much of it is similar to ``[`if_let_some_result`]``, but I followed the description in the issue to create its own linting rules. Looking forward to feedback, and continuing to work on Clippy in the future!

changelog: Renamed Lint: `if_let_some_result` is now called [`match_result_ok`]. Now also handles `while let` case.
@camsteffen
Copy link
Contributor

Done in #7608

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

No branches or pull requests

3 participants