eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py

changeset 8202
df194f43119c
parent 8195
db7f2badd374
child 8204
fd477cded1c1
diff -r a1149b1b4456 -r df194f43119c eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py	Mon Apr 05 10:55:02 2021 +0200
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyNodeVisitor.py	Tue Apr 06 17:53:39 2021 +0200
@@ -78,6 +78,17 @@
         
         self.generic_visit(node)
     
+    def visit_Assign(self, node):
+        """
+        Public method to process an Assign node.
+        
+        @param node reference to the Assign node
+        @type ast.Assign
+        """
+        self.__check121(node)
+        
+        self.generic_visit(node)
+    
     def visit_BoolOp(self, node):
         """
         Public method to process a BoolOp node.
@@ -1057,6 +1068,28 @@
             self.__error(node.lineno - 1, node.col_offset, "Y120",
                          node.name)
     
+    def __check121(self, node):
+        """
+        Private method to check for assignments that could be converted into
+        an augmented assignment
+        
+        @param node reference to the AST node to be checked
+        @type ast.Assign
+        """
+        # a = a - b
+        if (
+            len(node.targets) == 1 and
+            isinstance(node.targets[0], ast.Name) and
+            isinstance(node.value, ast.BinOp) and
+            isinstance(node.value.left, ast.Name) and
+            node.value.left.id == node.targets[0].id and
+            not isinstance(node.value.right, ast.Tuple)
+        ):
+            newNode = ast.AugAssign(node.targets[0], node.value.op,
+                                    node.value.right)
+            self.__error(node.lineno - 1, node.col_offset, "Y121",
+                         unparse(newNode), unparse(node))
+    
     def __check201(self, node):
         """
         Private method to check for calls where an unary 'not' is used for

eric ide

mercurial