diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2022-12-12 03:23:25 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-12 09:23:25 +0100 |
commit | bc9f15fda2e85facb230b3372cddb9ae40b4f3d5 (patch) | |
tree | b80217029f211c096d2fd4035fdd491aa398fc6c /pylint | |
parent | e7ef1fd364825e9b0bf4e6a5668b2e24ce058303 (diff) | |
download | pylint-git-bc9f15fda2e85facb230b3372cddb9ae40b4f3d5.tar.gz |
Prevent `used-before-assignment` in pattern matching with a guard (#7922)
Diffstat (limited to 'pylint')
-rw-r--r-- | pylint/checkers/variables.py | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 58a2216c0..602dd5414 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2139,6 +2139,7 @@ class VariablesChecker(BaseChecker): nodes.AugAssign, nodes.Expr, nodes.Return, + nodes.Match, ), ) and VariablesChecker._maybe_used_and_assigned_at_once(defstmt) @@ -2239,6 +2240,8 @@ class VariablesChecker(BaseChecker): """Check if `defstmt` has the potential to use and assign a name in the same statement. """ + if isinstance(defstmt, nodes.Match): + return any(case.guard for case in defstmt.cases) if isinstance(defstmt.value, nodes.BaseContainer) and defstmt.value.elts: # The assignment must happen as part of the first element # e.g. "assert (x:= True), x" |