| 1 | Checkers(
|
| 2 | [
|
| 3 |
|
| 4 | #@Show(false)
|
| 5 | define(ForAroundSizeConvertable,coin,"for loop (size),convertable to foreach",
|
| 6 | BT_TYPE_RULESET,
|
| 7 | ruleset(
|
| 8 | import(general,apply),
|
| 9 |
|
| 10 | # also PreIncrement and PostIncrement are same in ForUpdate statements
|
| 11 | StatementExpression(PreIncrementExpression($x)) -> Increment($x),
|
| 12 | StatementExpression($x,"++") -> Increment($x),
|
| 13 |
|
| 14 | ForInit(LocalVariableDeclaration($im,int,
|
| 15 | [VariableDeclarator(
|
| 16 | VariableDeclaratorId($index,0),
|
| 17 | IntegerLiteral(0)
|
| 18 | )]
|
| 19 | )) -> ZeroForInit($index),
|
| 20 |
|
| 21 | ForInit(StatementExpressionList([
|
| 22 | StatementExpression($index,"=",IntegerLiteral(0))
|
| 23 | ])) -> ZeroForInit($index),
|
| 24 |
|
| 25 |
|
| 26 | ForStatement(
|
| 27 | TraditionalForLoopHead(
|
| 28 | ZeroForInit($index),
|
| 29 | RelationalExpression($index,"<",
|
| 30 | MethodCall($x,Identifier("size"),Arguments([]))
|
| 31 | ),
|
| 32 | ForUpdate(StatementExpressionList(
|
| 33 | [Increment($index)]
|
| 34 | ))),
|
| 35 | $block ) -> CHECK(WithoutGet($x,$index,$block),
|
| 36 | s($x,$index,$block)),
|
| 37 |
|
| 38 | WithoutGet($x,$i,$block) ->
|
| 39 | let ($cleanBlock <- coin::EraseGet.EraseGet($block,$x,$i))
|
| 40 | FindSubterm.find($i,$cleanBlock),
|
| 41 |
|
| 42 | CHECK(true,$x) -> false,
|
| 43 | CHECK(false,$x) -> true
|
| 44 | [ violationDiscovered(ForAroundSizeConvertable,
|
| 45 | "for loop whith size which can be converted",
|
| 46 | $x) ],
|
| 47 |
|
| 48 | CHECK($x,$y) -> p($x),
|
| 49 |
|
| 50 | p($x) -> $x [ println($x) ]
|
| 51 |
|
| 52 | ),true),
|
| 53 |
|
| 54 |
|
| 55 | #@Show(false)
|
| 56 | define(ForAroundLengthConvertable,coin,
|
| 57 | "for loop (length),convertable to foreach",
|
| 58 | BT_TYPE_RULESET,
|
| 59 | ruleset(
|
| 60 | import(general,apply),
|
| 61 |
|
| 62 | StatementExpression(PreIncrementExpression($x)) -> Increment($x),
|
| 63 | StatementExpression($x,"++") -> Increment($x),
|
| 64 |
|
| 65 | Name([$x,Identifier("length")]) -> Length($x),
|
| 66 | FieldAccess([$x,Identifier("length")]) -> Length($x),
|
| 67 |
|
| 68 |
|
| 69 | ForInit(LocalVariableDeclaration($im,int,
|
| 70 | [VariableDeclarator(
|
| 71 | VariableDeclaratorId($index,0),
|
| 72 | IntegerLiteral(0)
|
| 73 | )]
|
| 74 | )) -> ZeroForInit($index),
|
| 75 |
|
| 76 | ForInit(StatementExpressionList([
|
| 77 | StatementExpression($index,"=",IntegerLiteral(0))
|
| 78 | ])) -> ZeroForInit($index),
|
| 79 |
|
| 80 |
|
| 81 | ForStatement(
|
| 82 | TraditionalForLoopHead(
|
| 83 | ZeroForInit($index),
|
| 84 | RelationalExpression($index,"<",Length($x)),
|
| 85 | ForUpdate(StatementExpressionList(
|
| 86 | [Increment($index)]
|
| 87 | ))),
|
| 88 | $block ) -> CHECK(WithoutIndexAccess($x,$index,$block),
|
| 89 | s($x,$index,$block)),
|
| 90 |
|
| 91 | WithoutIndexAccess($x,$i,$block) ->
|
| 92 | let ($cleanBlock <- coin::EraseIndexAccess.EraseIndexAccess($block,$x,$i))
|
| 93 | FindSubterm.find($i,$cleanBlock),
|
| 94 |
|
| 95 | CHECK(true,$x) -> false,
|
| 96 | CHECK(false,$x) -> true
|
| 97 | [ violationDiscovered(ForAroundLengthConvertable,
|
| 98 | "for array loop which can be converted to foreach",
|
| 99 | $x) ],
|
| 100 |
|
| 101 | CHECK($x,$y) -> p($x),
|
| 102 |
|
| 103 | p($x) -> $x [ println($x) ]
|
| 104 |
|
| 105 | ),true),
|
| 106 |
|
| 107 | calculate(ForConvertable,coin,
|
| 108 | "traditional for loop, convertable to foreach",
|
| 109 | ForAroundLengthConvertable+ForAroundSizeConvertable,
|
| 110 | true)
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 | ]
|
| 115 |
|
| 116 | );
|
| 117 |
|