summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-12-18 00:03:57 +0100
committerGitHub <noreply@github.com>2021-12-18 00:03:57 +0100
commit57916d57df0c6eb9a91f61bc029675f3433a4294 (patch)
treecad12f12324151901c2250e9e380fde05c8b60b1
parentffede32040511d342272f8de31bb09c2e7b7ae2b (diff)
downloadpylint-git-57916d57df0c6eb9a91f61bc029675f3433a4294.tar.gz
Remove unnecessary `if` statement in variable consumption checker (#5531)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r--pylint/checkers/variables.py30
1 files changed, 13 insertions, 17 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 82b99a805..315698c58 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -1389,23 +1389,19 @@ class VariablesChecker(BaseChecker):
# class A:
# x = lambda attr: f + attr
# f = 42
- if isinstance(frame, nodes.ClassDef) and node.name in frame.locals:
- if isinstance(node.parent, nodes.Arguments):
- if stmt.fromlineno <= defstmt.fromlineno:
- # Doing the following is fine:
- # class A:
- # x = 42
- # y = lambda attr=x: attr
- self.add_message(
- "used-before-assignment",
- args=node.name,
- node=node,
- )
- else:
- self.add_message(
- "undefined-variable", args=node.name, node=node
- )
- return (VariableVisitConsumerAction.CONSUME, found_nodes)
+ # We check lineno because doing the following is fine:
+ # class A:
+ # x = 42
+ # y = lambda attr: x + attr
+ if (
+ isinstance(frame, nodes.ClassDef)
+ and node.name in frame.locals
+ and stmt.fromlineno <= defstmt.fromlineno
+ ):
+ self.add_message(
+ "used-before-assignment", args=node.name, node=node
+ )
+
elif current_consumer.scope_type == "lambda":
self.add_message("undefined-variable", args=node.name, node=node)
return (VariableVisitConsumerAction.CONSUME, found_nodes)