ruff/crates/ruff_linter/resources/test/fixtures/flake8_bugbear
Tobias Fischer 312f6640b8
[`flake8-bugbear`] Implement `return-in-generator` (`B901`) (#11644)
## Summary

This PR implements the rule B901, which is part of the opinionated rules
of `flake8-bugbear`.

This rule seems to be desired in `ruff` as per
https://github.com/astral-sh/ruff/issues/3758 and
https://github.com/astral-sh/ruff/issues/2954#issuecomment-1441162976.

## Test Plan

As this PR was made closely following the
[CONTRIBUTING.md](8a25531a71/CONTRIBUTING.md),
it tests using the snapshot approach, that is described there.

## Sources

The implementation is inspired by [the original implementation in the
`flake8-bugbear`
repository](d1aec4cbef/bugbear.py (L1092)).
The error message and [test
file](d1aec4cbef/tests/b901.py)
where also copied from there.

The documentation I came up with on my own and needs improvement. Maybe
the example given in
https://github.com/astral-sh/ruff/issues/2954#issuecomment-1441162976
could be used, but maybe they are too complex, I'm not sure.

## Open Questions

- [ ] Documentation. (See above.)

- [x] Can I access the parent in a visitor?

The [original
implementation](d1aec4cbef/bugbear.py (L1100))
references the `yield` statement's parent to check if it is an
expression statement. I didn't find a way to do this in `ruff` and used
the `is_expresssion_statement` field on the visitor instead. What are
your thoughts on this? Is it possible and / or desired to access the
parent node here?

- [x] Is `Option::is_some(...)` -> `...unwrap()` the right thing to do?

Referring to [this piece of
code](9d5a280f71/crates/ruff_linter/src/rules/flake8_bugbear/rules/return_x_in_generator.rs?plain=1#L91-L96).
From my understanding, the `.unwrap()` is safe, because it is checked
that `return_` is not `None`. 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
https://github.com/astral-sh/ruff/labels/good%20first%20issue.

I'm new to Rust, so any constructive critisism is appreciated.

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
2024-05-31 21:48:36 +00:00
..
B002.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B003.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B004.py Improve handling of builtin symbols in linter rules (#10919) 2024-04-16 11:37:31 +01:00
B005.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B006_1.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B006_2.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B006_3.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B006_4.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B006_5.py Ensure that B006 autofixes are inserted after imports (#7629) 2023-09-27 01:26:29 +00:00
B006_6.py Ensure that B006 autofixes are inserted after imports (#7629) 2023-09-27 01:26:29 +00:00
B006_7.py Ensure that B006 autofixes are inserted after imports (#7629) 2023-09-27 01:26:29 +00:00
B006_8.py [`flake8-bugbear`] Treat `raise NotImplemented`-only bodies as stub functions (#10990) 2024-04-17 14:06:40 +00:00
B006_B008.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B006_extended.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B007.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B008_extended.py Resolve classes and functions relative to script name (#10965) 2024-04-18 01:42:50 +00:00
B009_B010.py Improve handling of builtin symbols in linter rules (#10919) 2024-04-16 11:37:31 +01:00
B011.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B012.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B013.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B014.py Insert necessary padding in B014 fixes (#7699) 2023-09-28 21:11:09 +00:00
B015.ipynb Avoid `B015`,`B018` for last expression in a cell (#8815) 2023-11-22 15:33:23 +00:00
B015.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B016.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B017.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B018.ipynb Avoid `B015`,`B018` for last expression in a cell (#8815) 2023-11-22 15:33:23 +00:00
B018.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B019.py [`flake8-bugbear`] Ignore enum classes in `cached-instance-method` (`B019`) (#11312) 2024-05-06 14:19:22 -04:00
B020.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B021.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B022.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B023.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B024.py Ignore non-abstract class attributes when enforcing B024 (#11210) 2024-04-30 09:01:08 -07:00
B025.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B026.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B027.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B027.pyi Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B028.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B029.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B030.py [`flake8-bugbear`] Allow tuples of exceptions (`B030`) (#10437) 2024-03-18 00:31:23 +00:00
B031.py [`flake8-bugbear`] Avoid false positive for usage after `continue` (`B031`) (#10539) 2024-03-25 00:38:30 +00:00
B032.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B033.py [`flake8-bugbear`] Add fix for `duplicate-value` (`B033`) (#9510) 2024-01-14 23:20:16 +00:00
B034.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B035.py Recategorize `static-key-dict-comprehension` from `RUF011` to `B035` (#9428) 2024-02-01 13:35:02 -06:00
B901.py [`flake8-bugbear`] Implement `return-in-generator` (`B901`) (#11644) 2024-05-31 21:48:36 +00:00
B904.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
B905.py Improve handling of builtin symbols in linter rules (#10919) 2024-04-16 11:37:31 +01:00
B909.py [`flake8-bugbear`] Implement `loop-iterator-mutation` (`B909`) (#9578) 2024-04-11 19:52:52 +00:00