[ruff] New rule useless-finally (RUF072)#24165
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF072 | 2 | 2 | 0 | 0 | 0 |
Formatter (stable)
✅ ecosystem check detected no format changes.
Formatter (preview)
✅ ecosystem check detected no format changes.
7b06e01 to
387d670
Compare
MichaReiser
left a comment
There was a problem hiding this comment.
This is great, thank you. I only have a few smaller questions
| /// - [`useless-try-except`][TRY203]: Flags `try/except` that only re-raises | ||
| #[derive(ViolationMetadata)] | ||
| #[violation_metadata(preview_since = "0.15.7")] | ||
| pub(crate) struct UselessFinally; |
There was a problem hiding this comment.
Can you say more on why you named the rule UselessFinally over NeedlessFinally (I'm not a native English speaker, so there might be a very good reason that isn't clear to me :)). I'm mainly asking because we have needless-else.
There was a problem hiding this comment.
That's the question I've been asking myself. The issue was titled "useless finally", so I started with that naming from the very beginning. Maybe there is some distinction between useless and needless, but I'm not sure whether existing rules follow it. Actually, there are only two needless rules (needless_bool, needless_else) and a lot more useless (useless_comparison, useless_expression, useless_return, useless_if_else, useless_try_except etc).
I can rename it if necessary.
There was a problem hiding this comment.
I'm fine with useless, but we should rename needless_if to unnecessary_if.
...ter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF072_RUF072.py.snap
Outdated
Show resolved
Hide resolved
...ter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF072_RUF072.py.snap
Outdated
Show resolved
Hide resolved
* main: [ty] make `test-case` a dev-dependency (#24187) [ty] implement cycle normalization for more types to prevent too-many-cycle panics (#24061) [ty] Silence all diagnostics in unreachable code (#24179) [ty] Intern `InferableTypeVars` (#24161) Implement unnecessary-if (RUF050) (#24114) Recognize `Self` annotation and `self` assignment in SLF001 (#24144) Bump the npm version before publish (#24178) [ty] Disallow Self in metaclass and static methods (#23231) Use trusted publishing for NPM packages (#24171) [ty] Respect non-explicitly defined dataclass params (#24170) Add RUF072: warn when using operator on an f-string (#24162) [ty] Check return type of generator functions (#24026) Implement useless-finally (RUF-072) (#24165) [ty] Add test for a dataclass with a default field converter (#24169) [ty] Dataclass field converters (#23088) [flake8-bandit] Treat sys.executable as trusted input in S603 (#24106) [ty] Add support for `typing.Concatenate` (#23689) `ASYNC115`: autofix to use full qualified `anyio.lowlevel` import (#24166) [ty] Disallow read-only fields in TypedDict updates (#24128) Speed up diagnostic rendering (#24146)
ruff] New rule useless-finally (RUF072)
Closes #19158
Implements the
useless_finallyrule (RUF072), which detects uselessfinallyblocks that only containpassor....It handles two cases:
try/except/finally: pass- thefinallyclause is removed, leaving a validtry/excepttry/finally: pass: the entiretry/finallyis unwrapped, the try body is dedented to replace the whole statementFix is skipped when comments are present in or around the
finallyblock.It complements with existing rules like
RUF047(needless-else) andSIM105(suppressible-exception). It case ofSIM105it also unblocks this rule, as currentlySIM105got skipped iffinallyhas any body at all (even justpass).Test Plan
RUF072.py- main rule test with error cases and non-error.useless_finally_and_needless_else- test function, which checks howRUF047andRUF072work together on the sametrystatement.useless_finally_and_suppressible_exception- test function, which checks howRUF072andSIM105work together.