    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Rule;
    8   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    9   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    10
    11  public class UnusedLocalVariableTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("unusedcode", "UnusedLocalVariable");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "unused local with assignment", 1, rule),
    22              new TestDescriptor(TEST2, "unused local w/o assignment", 1, rule),
    23              new TestDescriptor(TEST3, "unused local in constructor", 1, rule),
    24              new TestDescriptor(TEST4, "local used on rhs", 0, rule),
    25              new TestDescriptor(TEST5, "unused local in static initializer", 1, rule),
    26              new TestDescriptor(TEST6, "unused field", 0, rule),
    27              new TestDescriptor(TEST7, "loop indexes are not unused locals", 0, rule),
    28              new TestDescriptor(TEST8, "local used in anonymous inner class", 0, rule),
    29              new TestDescriptor(TEST9, "two unused locals of same name, one in nested class", 2, rule),
    30              new TestDescriptor(TEST10, "two locals declared on same line", 2, rule),
    31              new TestDescriptor(TEST11, "an assignment does not a usage make", 1, rule),
    32              new TestDescriptor(TEST12, "a compound assignment operator doth a usage make", 0, rule),
    33              new TestDescriptor(TEST13, "assignment to a member field means used", 0, rule),
    34              new TestDescriptor(TEST14, "make sure scopes are working", 3, rule),
    35              new TestDescriptor(TEST15, "another scope test", 1, rule),
    36              new TestDescriptor(TEST16, "assignment to an array member will be treated as a usage", 0, rule),
    37              new TestDescriptor(TEST17, "local variable used in postfix expression as child of StatementExpression", 1, rule),
    38              new TestDescriptor(TEST18, "local variable used in postfix expression on right hand side", 0, rule),
    39              new TestDescriptor(TEST19, "local variable, object ref, public field of which is incremented via in postfix expression", 0, rule),
    40              new TestDescriptor(TEST20, "local used in right shift", 0, rule),
    41          });
    42      }
    43
    44      private static final String TEST1 =
    45              "public class Foo {" + PMD.EOL +
    46              " void foo() {" + PMD.EOL +
    47              "   String fr = new String();" + PMD.EOL +
    48              " }" + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST2 =
    52              "public class Foo {" + PMD.EOL +
    53              " void foo() {" + PMD.EOL +
    54              "  int x;" + PMD.EOL +
    55              " }" + PMD.EOL +
    56              "}" + PMD.EOL +
    57              "";
    58
    59      private static final String TEST3 =
    60              "import java.util.*;" + PMD.EOL +
    61              "public class Foo {" + PMD.EOL +
    62              "    public Foo() {" + PMD.EOL +
    63              "       List a = new ArrayList();" + PMD.EOL +
    64              "    }" + PMD.EOL +
    65              "}" + PMD.EOL +
    66              "";
    67
    68      private static final String TEST4 =
    69              "import java.util.*;" + PMD.EOL +
    70              "public class Foo {" + PMD.EOL +
    71              "    public Foo() {" + PMD.EOL +
    72              "       List a = new ArrayList();" + PMD.EOL +
    73              "       if (true) {" + PMD.EOL +
    74              "               a.size();" + PMD.EOL +
    75              "       }" + PMD.EOL +
    76              "    }" + PMD.EOL +
    77              "}";
    78
    79      private static final String TEST5 =
    80              "import java.util.*;" + PMD.EOL +
    81              "public class Foo {" + PMD.EOL +
    82              " static {" + PMD.EOL +
    83              "  String x;" + PMD.EOL +
    84              " }" + PMD.EOL +
    85              "}";
    86
    87      private static final String TEST6 =
    88              "public class Foo {" + PMD.EOL +
    89              " public int x;" + PMD.EOL +
    90              "}";
    91
    92      private static final String TEST7 =
    93              "public class Foo {" + PMD.EOL +
    94              " void foo() {" + PMD.EOL +
    95              "  for (int i=0;i<10; i++);" + PMD.EOL +
    96              "  for (int i=0;i<10; i++);" + PMD.EOL +
    97              " }" + PMD.EOL +
    98              "}";
    99
    100
    101     private static final String TEST8 =
    102             "public class Foo {" + PMD.EOL +
    103             " public void foo() {" + PMD.EOL +
    104             "  final int x = 2;" + PMD.EOL +
    105             "   new Runnable() {" + PMD.EOL +
    106             "    public void run() {" + PMD.EOL +
    107             "     System.out.println(x);" + PMD.EOL +
    108             "    }" + PMD.EOL +
    109             "   };  " + PMD.EOL +
    110             " }" + PMD.EOL +
    111             "}";
    112
    113     private static final String TEST9 =
    114             "public class Foo {" + PMD.EOL +
    115             " void foo() {" + PMD.EOL +
    116             "  int x = 2;" + PMD.EOL +
    117             "  class Bar {" + PMD.EOL +
    118             "   void buz() {" + PMD.EOL +
    119             "    int x = 4;" + PMD.EOL +
    120             "   }" + PMD.EOL +
    121             "  }" + PMD.EOL +
    122             " }" + PMD.EOL +
    123             "}";
    124
    125     private static final String TEST10 =
    126             "public class Foo {" + PMD.EOL +
    127             " void foo() {" + PMD.EOL +
    128             "  int x,y=0;" + PMD.EOL +
    129             " }" + PMD.EOL +
    130             "}";
    131
    132     private static final String TEST11 =
    133             "public class Foo {" + PMD.EOL +
    134             " void bar() {" + PMD.EOL +
    135             "   int x;" + PMD.EOL +
    136             "   x = 4;" + PMD.EOL +
    137             " }" + PMD.EOL +
    138             "}";
    139
    140     private static final String TEST12 =
    141             "public class Foo {" + PMD.EOL +
    142             " void bar() {" + PMD.EOL +
    143             "   int x = 0;" + PMD.EOL +
    144             "   x += 2;" + PMD.EOL +
    145             " }" + PMD.EOL +
    146             "}";
    147
    148     private static final String TEST13 =
    149             "public class Foo {" + PMD.EOL +
    150             " void bar() {" + PMD.EOL +
    151             "   Bar b = new Bar();" + PMD.EOL +
    152             "   b.buz = 2;" + PMD.EOL +
    153             " }" + PMD.EOL +
    154             "}";
    155
    156     private static final String TEST14 =
    157             "public class Foo {" + PMD.EOL +
    158             " void bar() {" + PMD.EOL +
    159             "   int x = 2;" + PMD.EOL +
    160             "   if (true) {int y =2;int j =3;} " + PMD.EOL +
    161             " }" + PMD.EOL +
    162             "}";
    163
    164     private static final String TEST15 =
    165             "public class Foo {" + PMD.EOL +
    166             " void bar() {" + PMD.EOL +
    167             "   int x = 2;" + PMD.EOL +
    168             "   if (true) {int y =2;foo(y);int j =3;foo(j);} " + PMD.EOL +
    169             " }" + PMD.EOL +
    170             " void bar2() {" + PMD.EOL +
    171             "   int x = 2;" + PMD.EOL +
    172             "   fiddle(x);" + PMD.EOL +
    173             " }" + PMD.EOL +
    174             "}";
    175
    176     private static final String TEST16 =
    177             "public class Foo {" + PMD.EOL +
    178             " void bar() {" + PMD.EOL +
    179             "   int[] x = {2};" + PMD.EOL +
    180             "   x[1] = 2; " + PMD.EOL +
    181             " }" + PMD.EOL +
    182             "}";
    183
    184     private static final String TEST17 =
    185             "public class Foo {" + PMD.EOL +
    186             " void bar() {" + PMD.EOL +
    187             "   int x = 2;" + PMD.EOL +
    188             "   x++; " + PMD.EOL +
    189             " }" + PMD.EOL +
    190             "}";
    191
    192     private static final String TEST18 =
    193             "public class Foo {" + PMD.EOL +
    194             " void bar() {" + PMD.EOL +
    195             "   int x = 2;" + PMD.EOL +
    196             "   foo(x++); " + PMD.EOL +
    197             " }" + PMD.EOL +
    198             "}";
    199
    200     private static final String TEST19 =
    201             "public class Foo {" + PMD.EOL +
    202             " void bar() {" + PMD.EOL +
    203             "   Baz b = getBaz();" + PMD.EOL +
    204             "   b.x++; " + PMD.EOL +
    205             " }" + PMD.EOL +
    206             "}";
    207
    208     private static final String TEST20 =
    209             "public class Foo {" + PMD.EOL +
    210             " void bar() {" + PMD.EOL +
    211             "   int x = 2;" + PMD.EOL +
    212             "   int y = 4 >> x;" + PMD.EOL +
    213             "   foo(y);" + PMD.EOL +
    214             " }" + PMD.EOL +
    215             "}";
    216 }