[flake8-bugbear] Implement return-in-generator (B901)#11644
Merged
charliermarsh merged 7 commits intoastral-sh:mainfrom May 31, 2024
Merged
[flake8-bugbear] Implement return-in-generator (B901)#11644charliermarsh merged 7 commits intoastral-sh:mainfrom
flake8-bugbear] Implement return-in-generator (B901)#11644charliermarsh merged 7 commits intoastral-sh:mainfrom
Conversation
Contributor
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| B901 | 2 | 2 | 0 | 0 | 0 |
crates/ruff_linter/src/rules/flake8_bugbear/rules/return_x_in_generator.rs
Outdated
Show resolved
Hide resolved
| } | ||
| } | ||
| _ => { | ||
| self.in_expr_statement = stmt.is_expr_stmt(); |
Member
There was a problem hiding this comment.
I think I would do this by checking Stmt::Expr here, and then just introspecting the value directly to see if the value is Expr::Yield.
Contributor
Author
There was a problem hiding this comment.
Good idea, I updated the implementation to this.
Member
|
Thanks! Two small comments based on your questions. |
b88f550 to
a335dea
Compare
flake8-bugbear] Implement return-x-in-generator (B901)flake8-bugbear] Implement return-in-generator (B901)
a335dea to
253e157
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements the rule B901, which is part of the opinionated rules of
flake8-bugbear.This rule seems to be desired in
ruffas per #3758 and #2954 (comment).Test Plan
As this PR was made closely following the CONTRIBUTING.md, it tests using the snapshot approach, that is described there.
Sources
The implementation is inspired by the original implementation in the
flake8-bugbearrepository. The error message and test file where also copied from there.The documentation I came up with on my own and needs improvement. Maybe the example given in #2954 (comment) could be used, but maybe they are too complex, I'm not sure.
Open Questions
Documentation. (See above.)
Can I access the parent in a visitor?
The original implementation references the
yieldstatement's parent to check if it is an expression statement. I didn't find a way to do this inruffand used theis_expresssion_statementfield on the visitor instead. What are your thoughts on this? Is it possible and / or desired to access the parent node here?Is
Option::is_some(...)->...unwrap()the right thing to do?Referring to this piece of code. From my understanding, the
.unwrap()is safe, because it is checked thatreturn_is notNone. However, I feel like I missed a more elegant solution that does both in one.Other
I don't know a lot about this rule, I just implemented it because I found it in a good first issueGood for newcomers
.
I'm new to Rust, so any constructive critisism is appreciated.