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

Fix FP on if_then_some_else_none when there is early return #7980

Merged
merged 1 commit into from
Nov 23, 2021

Conversation

dswij
Copy link
Member

@dswij dswij commented Nov 16, 2021

closes #7870

changelog: [if_then_some_else_none] now does not fire when there is early return

@rust-highfive
Copy link

r? @xFrednet

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 16, 2021
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks good to me.


For future reference: The example in the issue could in theory still use bool::then() and then call flatten on the result like this(playground)

 fn f2(b: bool, v: Option<()>) -> Option<()> {
    b.then(|| {
        v?;
        Some(())
    })
    .flatten()
}

However, this only works if the early return is also inside the return expression of function. I like this solution implemented in this PR. Just wanted to document it for future reference 🙃

clippy_lints/src/if_then_some_else_none.rs Outdated Show resolved Hide resolved
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, only one small formatting NIT 😅

span_lint_and_help(
cx,
IF_THEN_SOME_ELSE_NONE,
expr.span,
"this could be simplified with `bool::then`",
None,
&help,
);
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustfmt sadly doesn't work inside macros. Could you fix this indention and the one in line 103? Then it's all good 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I thought that this change was because of rustfmt 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These indentations also often happen in my editor, when I move code around. It's good that rustfmt is really solid otherwise, and this might be fixed when Rust supports if let chains in the future 🙃

@xFrednet
Copy link
Member

Looks good to me, thank you for the bug fix! 🙃

@bors r+

@bors
Copy link
Contributor

bors commented Nov 23, 2021

📌 Commit ec3d1c8 has been approved by xFrednet

@bors
Copy link
Contributor

bors commented Nov 23, 2021

⌛ Testing commit ec3d1c8 with merge d6c707d...

@bors
Copy link
Contributor

bors commented Nov 23, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet
Pushing d6c707d to master...

@bors bors merged commit d6c707d into rust-lang:master Nov 23, 2021
@dswij dswij deleted the 7870 branch November 23, 2021 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive with clippy::if_then_some_else_none
4 participants