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

[flake8-bugbear] Handle shadowing and scope in unused-loop-control-variable (B007) #14122

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

dylwil3
Copy link
Collaborator

@dylwil3 dylwil3 commented Nov 6, 2024

This PR changes the logic employed to search for uses of the control variable within the loop body in order to account for shadowing and nested scope. This is achieved by using the semantic model to check whether there are any references to the binding of the control variable within the loop body.

Closes #14113 .

Comment on lines 93 to 105
let scope = &checker.semantic().current_scope();
if checker
.semantic()
.binding(scope.get(name).unwrap())
.references
.iter()
.map(|&refid| checker.semantic().reference(refid).range())
.any(|refrange| {
stmt_for
.body
.iter()
.any(|stmt| stmt.range().contains_range(refrange))
})
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If someone has a cleaner way of doing this I'd love to know!

Copy link
Contributor

github-actions bot commented Nov 6, 2024

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+13 -0 violations, +0 -0 fixes in 9 projects; 45 projects unchanged)

DisnakeDev/disnake (+1 -0 violations, +0 -0 fixes)

+ disnake/ext/commands/core.py:991:13: B007 Loop control variable `name` not used within loop body

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

+ tests/utils/decorators/test_checks.py:429:26: B007 Loop control variable `val` not used within loop body
+ tests/utils/decorators/test_validators.py:191:26: B007 Loop control variable `val` not used within loop body

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

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

+ airflow/api_fastapi/core_api/routes/public/dag_run.py:128:20: B007 Loop control variable `attr_value` not used within loop body
+ airflow/serialization/serialized_objects.py:973:16: B007 Loop control variable `v` not used within loop body
+ providers/src/airflow/providers/google/cloud/hooks/bigquery.py:2299:32: B007 Loop control variable `param_default` not used within loop body
+ providers/src/airflow/providers/google/cloud/hooks/bigquery.py:3201:32: B007 Loop control variable `param_default` not used within loop body
+ providers/tests/amazon/aws/hooks/test_hooks_signature.py:104:9: B007 Loop control variable `k` not used within loop body

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

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

+ superset/commands/tag/create.py:91:23: B007 Loop control variable `obj_id` not used within loop body

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

+ ibis/backends/sql/dialects.py:291:25: B007 Loop control variable `e` not used within loop body

milvus-io/pymilvus (+1 -0 violations, +0 -0 fixes)

+ examples/collection.py:131:9: B007 Loop control variable `i` not used within loop body

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


pypa/pip (+1 -0 violations, +0 -0 fixes)

+ src/pip/_internal/metadata/pkg_resources.py:215:17: B007 Loop control variable `name` not used within loop body

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

+ reflex/state.py:818:13: B007 Loop control variable `name` not used within loop body

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
B007 13 13 0 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+13 -0 violations, +0 -0 fixes in 8 projects; 46 projects unchanged)

DisnakeDev/disnake (+1 -0 violations, +0 -0 fixes)

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

+ disnake/ext/commands/core.py:991:13: B007 Loop control variable `name` not used within loop body

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

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

+ tests/utils/decorators/test_checks.py:429:26: B007 Loop control variable `val` not used within loop body
+ tests/utils/decorators/test_validators.py:191:26: B007 Loop control variable `val` not used within loop body

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

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

+ airflow/api_fastapi/core_api/routes/public/dag_run.py:128:20: B007 Loop control variable `attr_value` not used within loop body
+ airflow/serialization/serialized_objects.py:973:16: B007 Loop control variable `v` not used within loop body
+ providers/src/airflow/providers/google/cloud/hooks/bigquery.py:2299:32: B007 Loop control variable `param_default` not used within loop body
+ providers/src/airflow/providers/google/cloud/hooks/bigquery.py:3201:32: B007 Loop control variable `param_default` not used within loop body
+ providers/tests/amazon/aws/hooks/test_hooks_signature.py:104:9: B007 Loop control variable `k` not used within loop body

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

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

+ superset/commands/tag/create.py:91:23: B007 Loop control variable `obj_id` not used within loop body

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

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

+ ibis/backends/sql/dialects.py:291:25: B007 Loop control variable `e` not used within loop body

milvus-io/pymilvus (+1 -0 violations, +0 -0 fixes)

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

+ examples/collection.py:131:9: B007 Loop control variable `i` not used within loop body

pypa/pip (+1 -0 violations, +0 -0 fixes)

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

+ src/pip/_internal/metadata/pkg_resources.py:215:17: B007 Loop control variable `name` not used within loop body

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

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

+ reflex/state.py:818:13: B007 Loop control variable `name` not used within loop body

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
B007 13 13 0 0 0

@dylwil3 dylwil3 marked this pull request as draft November 6, 2024 04:25
@dylwil3
Copy link
Collaborator Author

dylwil3 commented Nov 6, 2024

Those ecosystem results look like they have a few false positives - let me add some more test cases for those and fix up the implementation.

The issue appears to be that the control variables are explicitly deleted after or at the end of some loops, and the semantic model (correctly) removes the reference.

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.

B007 does not detect unused variables when they are shadowed or appear in inner scope
1 participant