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

[ruff] Empty branches (RUF050) #14763

Closed
wants to merge 3 commits into from

Conversation

InSyncWithFoo
Copy link
Contributor

@InSyncWithFoo InSyncWithFoo commented Dec 4, 2024

Summary

Resolves #13929 and #9472.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Dec 4, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+286 -0 violations, +0 -0 fixes in 18 projects; 37 projects unchanged)

RasaHQ/rasa (+4 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ rasa/shared/core/generator.py:244:13: RUF050 Empty code branch
+ rasa/shared/core/training_data/story_reader/yaml_story_reader.py:294:9: RUF050 Empty code branch
+ tests/nlu/extractors/test_mitie_entity_extractor.py:102:9: RUF050 Empty code branch
+ tests/shared/nlu/training_data/test_features.py:432:9: RUF050 Empty code branch

apache/airflow (+30 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ airflow/api_connexion/endpoints/pool_endpoint.py:104:9: RUF050 Empty code branch
+ airflow/api_fastapi/core_api/routes/public/pools.py:131:9: RUF050 Empty code branch
+ airflow/models/dag.py:1187:9: RUF050 Empty code branch
+ airflow/models/dag.py:1761:9: RUF050 Empty code branch
+ airflow/models/dagrun.py:1353:17: RUF050 Empty code branch
+ airflow/serialization/serialized_objects.py:1393:13: RUF050 Empty code branch
+ airflow/serialization/serialized_objects.py:1757:13: RUF050 Empty code branch
+ dev/chart/build_changelog_annotations.py:94:9: RUF050 Empty code branch
+ providers/src/airflow/providers/amazon/aws/executors/ecs/ecs_executor.py:161:13: RUF050 Empty code branch
+ providers/src/airflow/providers/amazon/aws/triggers/eks.py:151:17: RUF050 Empty code branch
... 20 additional changes omitted for project

apache/superset (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ superset/commands/dataset/importers/v0.py:282:13: RUF050 Empty code branch
+ superset/daos/chart.py:30:1: RUF050 Empty code branch
+ superset/db_engine_specs/gsheets.py:358:13: RUF050 Empty code branch
+ superset/models/helpers.py:1764:17: RUF050 Empty code branch
+ superset/utils/log.py:37:1: RUF050 Empty code branch
+ tests/integration_tests/datasource_tests.py:119:9: RUF050 Empty code branch

bokeh/bokeh (+5 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ src/bokeh/command/subcommands/serve.py:884:9: RUF050 Empty code branch
+ src/bokeh/plotting/_renderer.py:282:17: RUF050 Empty code branch
+ src/bokeh/plotting/_renderer.py:284:17: RUF050 Empty code branch
+ src/bokeh/plotting/_renderer.py:286:17: RUF050 Empty code branch
+ src/bokeh/plotting/_renderer.py:288:17: RUF050 Empty code branch

ibis-project/ibis (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ ibis/common/deferred.py:393:9: RUF050 Empty code branch

lnbits/lnbits (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ lnbits/core/crud.py:783:5: RUF050 Empty code branch
+ lnbits/core/crud.py:791:5: RUF050 [*] Empty code branch
+ lnbits/core/crud.py:794:5: RUF050 Empty code branch
+ lnbits/core/crud.py:800:5: RUF050 [*] Empty code branch
+ lnbits/wallets/nwc.py:579:13: RUF050 Empty code branch
+ tests/helpers.py:52:5: RUF050 Empty code branch

pandas-dev/pandas (+109 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ asv_bench/benchmarks/frame_methods.py:162:9: RUF050 Empty code branch
+ asv_bench/benchmarks/frame_methods.py:176:9: RUF050 Empty code branch
+ asv_bench/benchmarks/frame_methods.py:192:9: RUF050 Empty code branch
+ asv_bench/benchmarks/frame_methods.py:208:9: RUF050 Empty code branch
+ asv_bench/benchmarks/frame_methods.py:224:9: RUF050 Empty code branch
+ asv_bench/benchmarks/frame_methods.py:234:9: RUF050 Empty code branch
+ asv_bench/benchmarks/index_object.py:101:9: RUF050 Empty code branch
+ asv_bench/benchmarks/index_object.py:97:9: RUF050 Empty code branch
+ asv_bench/benchmarks/io/csv.py:497:9: RUF050 Empty code branch
+ asv_bench/benchmarks/series_methods.py:370:9: RUF050 Empty code branch
+ asv_bench/benchmarks/strings.py:283:9: RUF050 Empty code branch
+ asv_bench/benchmarks/timeseries.py:138:9: RUF050 Empty code branch
+ pandas/_testing/asserters.py:1009:9: RUF050 Empty code branch
+ pandas/_testing/asserters.py:129:13: RUF050 Empty code branch
+ pandas/_testing/asserters.py:132:13: RUF050 Empty code branch
+ pandas/_testing/asserters.py:1406:5: RUF050 Empty code branch
+ pandas/core/arrays/arrow/array.py:616:13: RUF050 Empty code branch
+ pandas/core/arrays/datetimelike.py:2002:9: RUF050 Empty code branch
+ pandas/core/arrays/datetimelike.py:2269:9: RUF050 Empty code branch
... 90 additional changes omitted for project

pypa/cibuildwheel (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ cibuildwheel/pyodide.py:406:5: RUF050 [*] Empty code branch
+ test/utils.py:29:1: RUF050 Empty code branch

python-poetry/poetry (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ src/poetry/puzzle/provider.py:240:9: RUF050 Empty code branch
+ src/poetry/utils/shell.py:129:9: RUF050 Empty code branch

reflex-dev/reflex (+4 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ reflex/base.py:35:13: RUF050 Empty code branch
+ reflex/utils/processes.py:298:5: RUF050 Empty code branch
+ tests/units/test_app.py:1195:5: RUF050 Empty code branch
+ tests/units/test_state.py:2076:9: RUF050 Empty code branch

rotki/rotki (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ rotkehlchen/data_import/importers/nexo.py:155:9: RUF050 Empty code branch
+ rotkehlchen/icons.py:32:1: RUF050 Empty code branch

scikit-build/scikit-build-core (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ tests/test_cmake_ast.py:137:5: RUF050 Empty code branch

zulip/zulip (+35 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ analytics/views/stats.py:67:9: RUF050 Empty code branch
+ tools/lib/provision_inner.py:291:13: RUF050 Empty code branch
+ zerver/data_import/slack_message_conversion.py:241:5: RUF050 Empty code branch
+ zerver/decorator.py:80:5: RUF050 Empty code branch
+ zerver/lib/event_schema.py:270:5: RUF050 Empty code branch
+ zerver/lib/events.py:1375:9: RUF050 Empty code branch
+ zerver/lib/events.py:1517:5: RUF050 Empty code branch
+ zerver/lib/events.py:1520:5: RUF050 Empty code branch
+ zerver/lib/events.py:1523:5: RUF050 Empty code branch
+ zerver/lib/events.py:1526:5: RUF050 Empty code branch
... 25 additional changes omitted for project

indico/indico (+9 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ indico/cli/devserver.py:231:9: RUF050 Empty code branch
+ indico/core/celery/controllers.py:25:13: RUF050 Empty code branch
+ indico/modules/designer/pdf.py:155:17: RUF050 Empty code branch
+ indico/modules/events/api.py:620:13: RUF050 Empty code branch
+ indico/modules/events/export.py:384:17: RUF050 Empty code branch
+ indico/modules/logs/util.py:54:9: RUF050 Empty code branch
+ indico/testing/util.py:99:5: RUF050 Empty code branch
+ indico/util/string.py:87:13: RUF050 Empty code branch
+ indico/web/flask/app.py:171:5: RUF050 Empty code branch

python-trio/trio (+10 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ src/trio/_core/_io_kqueue.py:198:17: RUF050 Empty code branch
+ src/trio/_core/_io_windows.py:570:17: RUF050 Empty code branch
+ src/trio/_core/_io_windows.py:632:13: RUF050 Empty code branch
+ src/trio/_core/_run.py:601:13: RUF050 Empty code branch
+ src/trio/_core/_tests/test_ki.py:107:5: RUF050 Empty code branch
+ src/trio/_core/_tests/test_ki.py:99:5: RUF050 Empty code branch
+ src/trio/_dtls.py:793:17: RUF050 Empty code branch
+ src/trio/_subprocess_platform/__init__.py:82:5: RUF050 Empty code branch
+ src/trio/_tests/test_tracing.py:36:5: RUF050 Empty code branch
+ src/trio/testing/_fake_net.py:202:9: RUF050 Empty code branch

... Truncated remaining completed project reports due to GitHub comment length restrictions

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF050 286 286 0 0 0

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@InSyncWithFoo
Copy link
Contributor Author

Currently this rule is a bit too overzealous. It will also flag empty if/elif followed by non-empty elif/else.

Personally, I'm in favor of keeping this behaviour, as it enforces refactoring/denesting. I understand that I'm exceedingly likely to be in the minority, however.

@MichaReiser
Copy link
Member

From a first skim through the ecosystem checks:

We should exclude branches with comments because it expresses that the branch is intentional:

I know this is quiet the opposite of what I said in the issue, but I just realised that clippy intentionally has multiple rules:

Thinking this through more, this makes sense to me because a needless-if is just useless where an empty loop can be correct sometimes.

Currently this rule is a bit too overzealous. It will also flag empty if/elif followed by non-empty elif/else.

I like using if or elif branches to skip early. It can help to avoid repeating the same condition in negated form for all following branches.

@InSyncWithFoo
Copy link
Contributor Author

I like using if or elif branches to skip early. It can help to avoid repeating the same condition in negated form for all following branches.

In such cases, I would move the conditions to another function to take advantage of early returns, but that's just me.


As for splitting, I would like to use and reserve the RUF06* block, if that's alright with you. That way, it would be easier to select all of these closely related rules using a single selector.

@MichaReiser
Copy link
Member

In such cases, I would move the conditions to another function to take advantage of early returns, but that's just me.

That's fair. I just think it's too opinionated for this rule. Such a branch isn't needless; it's more of a stylistic choice; you don't want any empty branches.

As for splitting, I would like to use and reserve the RUF06* block, if that's alright with you. That way, it would be easier to select all of these closely related rules using a single selector.

I don't think we want to block off an entire range for just a few rules.

Let's also hear from @AlexWaygood to ensure we're aligned to avoid changing our approach mroe than once

@gpauloski
Copy link

That's fair. I just think it's too opinionated for this rule. Such a branch isn't needless; it's more of a stylistic choice; you don't want any empty branches.

I agree. My intention when I opened the issue was just to detect what is most likely unintentional dead code (e.g., empty conditionals from other auto-fixes/formatters). I would not want deliberate style choices to get flagged, especially the empty branches with comments.

@MichaReiser
Copy link
Member

Thanks for keeping this PR up to date. I talked to @AlexWaygood, and we agree that we should split this rule into multiple smaller rules. I'll comment on the issue with a suggestion. I'm sorry to have lead you in the wrong direction :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect empty conditional statements
3 participants