<commit_before>from pychecker2.Check import Check
from pychecker2.util import BaseVisitor
from pychecker2.Warning import Warning

from compiler import parseFile, walk
from compiler import ast

class ReachableCheck(Check):

    unreachable = Warning('Report unreachable code', 'Line is unreachable')
    
    def check(self, file, unused_checker):
        class ReturnsVisitor(BaseVisitor):
            def __init__(s):
                s.returns = 0
                s.inTryExcept = 0

            def visitAssert(s, node):
                if isinstance(node.test, ast.Const):
                    s.returns = not node.test.value
                if isinstance(node.test, ast.Name):
                    if node.test.name == 'None':
                        s.returns = 1

            def visitReturn(s, node):
                s.returns = 1

            def visitRaise(s, node):
                s.returns = 1

            def visitTryExcept(s, node):
                # no matter what happens in the try clause, it might
                # cause an exception, so just check the handlers and
                # else conditions for returning
                for exc, detail, code in node.handlers:
                    s.returns = 0                
                    s.visit(code)
                    if not s.returns:
                        return
                if node.else_:
                    s.visit(node.else_)

            def visitIf(s, node):
                for cond, code in node.tests:
                    s.returns = 0
                    s.visit(code)
                    if not s.returns:
                        return
                s.returns = 0
                if node.else_:
                    s.visit(node.else_)
                    
            def visitStmt(s, node):
                for n in range(len(node.nodes) - 1):
                    s.returns = 0
                    s.visit(node.nodes[n]) # icky: return by side-effect
                    if s.returns:
                        file.warning(node.nodes[n + 1], self.unreachable)
                s.returns = 0
                s.visit(node.nodes[-1])
                
            def visitFunction(s, node):
                tmp = s.returns
                s.returns = 0
                s.visit(node.code)
                s.returns = tmp

        if file.parseTree:
            walk(file.parseTree, ReturnsVisitor())
<commit_msg>cleanup:    put comment where it is more appropriate    sometimes statement nodes can be empty    remove unused member variable<commit_after>from pychecker2.Check import Check
from pychecker2.util import BaseVisitor
from pychecker2.Warning import Warning

from compiler import parseFile, walk
from compiler import ast

class ReachableCheck(Check):

    unreachable = Warning('Report unreachable code', 'Line is unreachable')
    
    def check(self, file, unused_checker):
        class ReturnsVisitor(BaseVisitor):
            def __init__(s):
                s.returns = 0           #  icky: result value by side-effect

            def visitAssert(s, node):
                if isinstance(node.test, ast.Const):
                    s.returns = not node.test.value
                if isinstance(node.test, ast.Name):
                    if node.test.name == 'None':
                        s.returns = 1

            def visitReturn(s, node):
                s.returns = 1

            def visitRaise(s, node):
                s.returns = 1

            def visitTryExcept(s, node):
                # no matter what happens in the try clause, it might
                # cause an exception, so just check the handlers and
                # else conditions for returning
                for exc, detail, code in node.handlers:
                    s.returns = 0                
                    s.visit(code)
                    if not s.returns:
                        return
                if node.else_:
                    s.visit(node.else_)

            def visitIf(s, node):
                for cond, code in node.tests:
                    s.returns = 0
                    s.visit(code)
                    if not s.returns:
                        return
                s.returns = 0
                if node.else_:
                    s.visit(node.else_)
                    
            def visitStmt(s, node):
                for n in range(len(node.nodes) - 1):
                    s.returns = 0
                    s.visit(node.nodes[n])
                    if s.returns:
                        file.warning(node.nodes[n + 1], self.unreachable)
                if node.nodes:
                    s.returns = 0
                    s.visit(node.nodes[-1])
                
            def visitFunction(s, node):
                tmp = s.returns
                s.returns = 0
                s.visit(node.code)
                s.returns = tmp

        if file.parseTree:
            walk(file.parseTree, ReturnsVisitor())

{'fraction_non_alphanumeric': 0.07550607287449393, 'fraction_numerical': 0.0066801619433198385, 'mean_word_length': 1.133419689119171, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7365246', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1171, 'n_words': 354}