File: ConditionalChecks.py

package info (click to toggle)
spe 0.8.2a%2Brepack-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,812 kB
  • ctags: 6,555
  • sloc: python: 45,491; makefile: 146; sh: 2
file content (44 lines) | stat: -rwxr-xr-x 1,365 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from pychecker2.Check import Check
from pychecker2.util import BaseVisitor

from compiler import ast, walk

class ConstantFinder(BaseVisitor):

    def __init__(self):
        self.result = []

    def visitConst(self, node):
        if isinstance(node.parent, (ast.Or, ast.Not, ast.And)):
            self.result.append((node, `node.value`))

    def visitName(self, node):
        if node.name == 'None':
            if isinstance(node.parent, (ast.Or, ast.Not, ast.And)):
                self.result.append((node, 'None'))

class GetConditionalConstants(BaseVisitor):

    def __init__(self):
        self.result = []

    def visitIf(self, node):
        for test, code in node.tests:
            self.result.extend(walk(test, ConstantFinder()).result)

    def visitWhile(self, node):
        self.result.extend(walk(node.test, ConstantFinder()).result)
    visitListCompIf = visitWhile
    visitAssert = visitWhile

class ConstantCheck(Check):

    constantInConditional = \
                          Warning('Report constants used in conditionals',
                                  'Constant used in conditional %s')

    def check(self, file, unused_checker):
        if file.parseTree:
            v = GetConditionalConstants()
            for n, value in walk(file.parseTree, v).result:
                file.warning(n, self.constantInConditional, value)