root / trunk / JavaChecker / etc / checkers_elvis.def @ 15107

1
Checkers(
2
[
3
4
 #@Show(false)
5
 define(Elvis1,coin,"elvis1",
6
        BT_TYPE_RULESET,
7
        ruleset(
8
           # a==null ? b : a
9
          ConditionalExpression(
10
           EqualityExpression($a,"==",NullLiteral()), $b, $a ) -> true
11
                 [ violationDiscovered(Elvis1, "elvis1", s($a,$b)) ],
12
           # a!=null ? a : b
13
          ConditionalExpression(
14
                EqualityExpression($a,"!=",NullLiteral()),  $a, $b ) -> true
15
                 [ violationDiscovered(Elvis1, "elvis1", s($a,$b)) ],
16
           # null==a ? b: a
17
          ConditionalExpression(
18
           EqualityExpression(NullLiteral(),"==",$a), $b, $a) -> true
19
                 [ violationDiscovered(Elvis1, "elvis1", s($a,$b)) ],
20
           # null!=a ? a: b
21
          ConditionalExpression(
22
           EqualityExpression(NullLiteral(),"!=",$a), $a, $b ) -> true
23
                 [ violationDiscovered(Elvis1, "elvis1", s($a,$b)) ],
24
25
           Block($x) -> false
26
27
        ),false),
28
          
29
30
 #@Show(false)
31
 define(NullSafe,coin,"nullsafe call of fields and methods",
32
        BT_TYPE_RULESET,
33
        ruleset(
34
35
          import(general, apply),
36
          import(List, car),
37
38
          # we don not want write same conditions exceptions 4 times, so
39
          # transform all to one form.
40
          EqualityExpression($a,"!=",NullLiteral()) -> IsNotNullExpression($a),
41
          EqualityExpression(NullLiteral(),"!=",$a) -> IsNotNullExpression($a),
42
          EqualityExpression($a,"==",NullLiteral()) -> IsNullExpression($a),
43
          EqualityExpression(NullLiteral(),"==",$a) -> IsNullExpression($a),
44
45
           # if (foo!=null) { foo.process(); }
46
          IfStatement(IsNotNullExpression($foo),
47
              Block([StatementExpressionStatement(StatementExpression(
48
                     MethodCall($foo,$m,$args)
49
                    ))]))
50
                -> true
51
                 [ violationDiscovered(NullSafe, "null safe method call in statement", s($foo,$m,$args)) ],
52
           # if (foo!=null) { bar=foo.process(); }
53
          IfStatement(IsNotNullExpression($foo),
54
              Block([StatementExpressionStatement(StatementExpression(
55
                     $bar,"=",MethodCall($foo,$m,$args)
56
                    ))]))
57
                -> true
58
                 [ violationDiscovered(NullSafe, "null safe methd call in statement", s($foo,$m,$args)) ],
59
60
61
          ConditionalExpression(IsNotNullExpression($a),$x,$y)->
62
                                ConditionalExpressionNotNull($a,$x,$y), 
63
          ConditionalExpression(IsNullExpression($a),$x,$y)->
64
                                ConditionalExpressionNotNull($a,$y,$x), 
65
66
           # a!=null ? a.method() : null
67
          ConditionalExpressionNotNull($a,
68
                    MethodCall($a,$m,$args), NullLiteral() ) -> true
69
                 [ violationDiscovered(NullSafe, 
70
                   "null safe method call in condition", s($m,$args)) ],
71
72
          ConditionalExpressionNotNull($a,
73
                    Name([$a:$x]), NullLiteral() ) -> true
74
                 [ violationDiscovered(NullSafe, 
75
                   "null safe field access in condition", s($a,$x)) ],
76
77
          ConditionalExpressionNotNull($a,
78
                    Field($a,$x), NullLiteral() ) -> true
79
                 [ violationDiscovered(NullSafe, 
80
                   "null safe field access in condition", s($a,$x)) ],
81
82
          ConditionalExpressionNotNull($a,ArrayIndex($a,$x),NullLiteral())
83
                  -> true
84
                 [ violationDiscovered(NullSafe, 
85
                   "null safe array access ", s($a,$x)) ],
86
87
           # now try to handle complex If expressions. 
88
           # interesting if-s are those, who consists only from
89
           # IsNotNullExpression
90
           ConditionalAndExpression(IsNotNullExpression($a),
91
                                    IsNotNullExpression($b)) 
92
             ->
93
              ConditionalAndExpressions([$a,$b]),
94
95
           ConditionalAndExpression(IsNotNullExpression($a),
96
                               ConditionalAndExpressions($list))
97
            -> ConditionalAndExpressions(cons($a,$list)),
98
99
           IfStatement(ConditionalAndExpressions($cn),$block) 
100
                -> CHECK(FindSubterm.find(car($cn),$block),$block),
101
102
           IfStatement($other,$x) -> false,
103
104
           IfStatement(ConditionalAndExpressions($cn),$block, $elsblock) 
105
                -> CHECK(FindSubterm.find(car($cn),$block),$block),
106
107
           IfStatement($other,$x,$y) -> false,
108
109
           CHECK(true,$block) -> true
110
                 [ violationDiscovered(NullSafe, 
111
                            "multiple test for null access", $block) ],
112
113
           CHECK(false,$block) -> false,
114
115
           ## all other can be reduced.
116
           Block($x) -> false,
117
                                                            
118
           p($x) -> $x [ println($x) ]
119
120
        ),false)
121
122
]
123
124
);
125