From 2f3264e148b9cf31e32c3137feb0f5ce9532d85b Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Thu, 4 Jul 2024 22:09:31 +0200 Subject: [PATCH] fix(rules): skip dummy variables for `PLR1704` (#12190) ## Summary Resolves #12157. ## Test Plan Snapshot tests. --- .../pylint/redefined_argument_from_local.py | 5 + .../checkers/ast/analyze/deferred_scopes.rs | 3 + ...1704_redefined_argument_from_local.py.snap | 120 +++++++++--------- 3 files changed, 67 insertions(+), 61 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/redefined_argument_from_local.py b/crates/ruff_linter/resources/test/fixtures/pylint/redefined_argument_from_local.py index 5a76cee04715c..d25413738f573 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/redefined_argument_from_local.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/redefined_argument_from_local.py @@ -21,6 +21,11 @@ def func(a): print(a) +def func(_): + for _ in range(1): + ... + + # Errors def func(a): for a in range(1): diff --git a/crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs b/crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs index c4d0ee7944b78..1a2c1c18ff858 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs @@ -136,6 +136,9 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) { if !shadowed.kind.is_argument() { continue; } + if checker.settings.dummy_variable_rgx.is_match(name) { + continue; + } checker.diagnostics.push(Diagnostic::new( pylint::rules::RedefinedArgumentFromLocal { name: name.to_string(), diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1704_redefined_argument_from_local.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1704_redefined_argument_from_local.py.snap index 7bc66ab545846..ce4a2efaa62b0 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1704_redefined_argument_from_local.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1704_redefined_argument_from_local.py.snap @@ -1,115 +1,113 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs --- -redefined_argument_from_local.py:26:9: PLR1704 Redefining argument with the local name `a` +redefined_argument_from_local.py:31:9: PLR1704 Redefining argument with the local name `a` | -24 | # Errors -25 | def func(a): -26 | for a in range(1): +29 | # Errors +30 | def func(a): +31 | for a in range(1): | ^ PLR1704 -27 | ... +32 | ... | -redefined_argument_from_local.py:31:9: PLR1704 Redefining argument with the local name `i` +redefined_argument_from_local.py:36:9: PLR1704 Redefining argument with the local name `i` | -30 | def func(i): -31 | for i in range(10): +35 | def func(i): +36 | for i in range(10): | ^ PLR1704 -32 | print(i) +37 | print(i) | -redefined_argument_from_local.py:38:25: PLR1704 Redefining argument with the local name `e` +redefined_argument_from_local.py:43:25: PLR1704 Redefining argument with the local name `e` | -36 | try: -37 | ... -38 | except Exception as e: +41 | try: +42 | ... +43 | except Exception as e: | ^ PLR1704 -39 | print(e) +44 | print(e) | -redefined_argument_from_local.py:43:24: PLR1704 Redefining argument with the local name `f` +redefined_argument_from_local.py:48:24: PLR1704 Redefining argument with the local name `f` | -42 | def func(f): -43 | with open('', ) as f: +47 | def func(f): +48 | with open('', ) as f: | ^ PLR1704 -44 | print(f) +49 | print(f) | -redefined_argument_from_local.py:48:24: PLR1704 Redefining argument with the local name `a` +redefined_argument_from_local.py:53:24: PLR1704 Redefining argument with the local name `a` | -47 | def func(a, b): -48 | with context() as (a, b, c): +52 | def func(a, b): +53 | with context() as (a, b, c): | ^ PLR1704 -49 | print(a, b, c) +54 | print(a, b, c) | -redefined_argument_from_local.py:48:27: PLR1704 Redefining argument with the local name `b` +redefined_argument_from_local.py:53:27: PLR1704 Redefining argument with the local name `b` | -47 | def func(a, b): -48 | with context() as (a, b, c): +52 | def func(a, b): +53 | with context() as (a, b, c): | ^ PLR1704 -49 | print(a, b, c) +54 | print(a, b, c) | -redefined_argument_from_local.py:53:24: PLR1704 Redefining argument with the local name `a` +redefined_argument_from_local.py:58:24: PLR1704 Redefining argument with the local name `a` | -52 | def func(a, b): -53 | with context() as [a, b, c]: +57 | def func(a, b): +58 | with context() as [a, b, c]: | ^ PLR1704 -54 | print(a, b, c) +59 | print(a, b, c) | -redefined_argument_from_local.py:53:27: PLR1704 Redefining argument with the local name `b` +redefined_argument_from_local.py:58:27: PLR1704 Redefining argument with the local name `b` | -52 | def func(a, b): -53 | with context() as [a, b, c]: +57 | def func(a, b): +58 | with context() as [a, b, c]: | ^ PLR1704 -54 | print(a, b, c) +59 | print(a, b, c) | -redefined_argument_from_local.py:58:51: PLR1704 Redefining argument with the local name `a` +redefined_argument_from_local.py:63:51: PLR1704 Redefining argument with the local name `a` | -57 | def func(a): -58 | with open('foo.py', ) as f, open('bar.py') as a: +62 | def func(a): +63 | with open('foo.py', ) as f, open('bar.py') as a: | ^ PLR1704 -59 | ... +64 | ... | -redefined_argument_from_local.py:64:13: PLR1704 Redefining argument with the local name `a` +redefined_argument_from_local.py:69:13: PLR1704 Redefining argument with the local name `a` | -62 | def func(a): -63 | def bar(b): -64 | for a in range(1): +67 | def func(a): +68 | def bar(b): +69 | for a in range(1): | ^ PLR1704 -65 | print(a) +70 | print(a) | -redefined_argument_from_local.py:70:13: PLR1704 Redefining argument with the local name `b` +redefined_argument_from_local.py:75:13: PLR1704 Redefining argument with the local name `b` | -68 | def func(a): -69 | def bar(b): -70 | for b in range(1): +73 | def func(a): +74 | def bar(b): +75 | for b in range(1): | ^ PLR1704 -71 | print(b) +76 | print(b) | -redefined_argument_from_local.py:76:13: PLR1704 Redefining argument with the local name `a` +redefined_argument_from_local.py:81:13: PLR1704 Redefining argument with the local name `a` | -74 | def func(a=1): -75 | def bar(b=2): -76 | for a in range(1): +79 | def func(a=1): +80 | def bar(b=2): +81 | for a in range(1): | ^ PLR1704 -77 | print(a) -78 | for b in range(1): +82 | print(a) +83 | for b in range(1): | -redefined_argument_from_local.py:78:13: PLR1704 Redefining argument with the local name `b` +redefined_argument_from_local.py:83:13: PLR1704 Redefining argument with the local name `b` | -76 | for a in range(1): -77 | print(a) -78 | for b in range(1): +81 | for a in range(1): +82 | print(a) +83 | for b in range(1): | ^ PLR1704 -79 | print(b) +84 | print(b) | - -