summaryrefslogtreecommitdiff
path: root/pylint/test/functional/simplifiable_if_statement.py
blob: 48d6dbb5c012e2440779458503940c530bdacff7 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Test that some if statement tests can be simplified."""

# pylint: disable=missing-docstring, invalid-name


def test_simplifiable_1(arg):
    # Simple test that can be replaced by bool(arg)
    if arg: # [simplifiable-if-statement]
        return True
    else:
        return False


def test_simplifiable_2(arg, arg2):
    # Can be reduced to bool(arg and not arg2)
    if arg and not arg2: # [simplifiable-if-statement]
        return True
    else:
        return False


def test_simplifiable_3(arg, arg2):
    # Can be reduced to bool(arg and not arg2)
    if arg and not arg2: # [simplifiable-if-statement]
        var = True
    else:
        var = False
    return var


def test_simplifiable_4(arg):
    if arg:
        var = True
    else:
        if arg == "arg1": # [simplifiable-if-statement]
            return True
        else:
            return False
    return var


def test_not_necessarily_simplifiable_1(arg, arg2):
    # Can be reduced to bool(not arg and not arg2) or to
    # `not all(N)`, which is a bit harder to understand
    # than `any(N)` when var should be False.
    if arg or arg2:
        var = False
    else:
        var = True
    return var


def test_not_necessarily_simplifiabile_2(arg):
    # This could theoretically be reduced to `not arg or arg > 3`
    # but the net result is that now the condition is harder to understand,
    # because it requires understanding of an extra clause:
    #   * first, there is the negation of truthness with `not arg`
    #   * the second clause is `arg > 3`, which occurs when arg has a
    #     a truth value, but it implies that `arg > 3` is equivalent
    #     with `arg and arg > 3`, which means that the user must
    #     think about this assumption when evaluating `arg > 3`.
    #     The original form is easier to grasp.
    if arg and arg <= 3:
        return False
    else:
        return True


def test_not_simplifiable_3(arg):
    if arg:
        test_not_necessarily_simplifiabile_2(arg)
        test_not_necessarily_simplifiable_1(arg, arg)
        return False
    else:
        if arg < 3:
            test_simplifiable_3(arg, 42)
        return True


def test_not_simplifiable_4(arg):
    # Not interested in multiple elifs
    if arg == "any":
        return True
    elif test_not_simplifiable_3(arg) == arg:
        return True
    else:
        return False


def test_not_simplifiable_5(arg):
    # Different actions in each branch
    if arg == "any":
        return True
    else:
        var = 42
    return var


def test_not_simplifiable_6(arg):
    # Different actions in each branch
    if arg == "any":
        var = 42
    else:
        return True
    return var

def test_not_simplifiable_7(arg):
    # Returning something different
    if arg == "any":
        return 4
    else:
        return 5


def test_not_simplifiable_8(arg):
    # Only one of the branch returns something boolean
    if arg == "any":
        return True
    else:
        return 0