View Javadoc
1   /*
2   UnusedLocalVariable
3   allowUnnamedVariables = false
4   
5   */
6   
7   package com.puppycrawl.tools.checkstyle.checks.coding.unusedlocalvariable;
8   
9   import java.util.function.Predicate;
10  
11  public class InputUnusedLocalVariable {
12  
13      int sameName = 20;
14  
15      static int a = 12;
16  
17      static int b = 12;
18  
19      String s;
20  
21      Obj obj = new Obj() {
22          int a = 12;
23          int b = 12;
24      };
25  
26      private void sameName(int unusedParameter) {
27          int sameName = 10; // violation, 'Unused local variable'
28          int b = 12; // violation, 'Unused local variable'
29          b = 23;
30          this.sameName /= 2;
31          int testInLambdas = 0; // violation, 'Unused local variable'
32          testInLambdas = 12;
33          int coding = 0; // violation, 'Unused local variable'
34          int InputUnusedLocalVariable = 1; // violation, 'Unused local variable'
35          com.puppycrawl.tools.checkstyle.checks
36                  .coding.unusedlocalvariable
37                  .InputUnusedLocalVariable.testInLambdas(a);
38      }
39  
40      public void testCallingMethods() {
41          int var = 21;
42          sameName(var);
43          var = sameName;
44      }
45  
46      public static void testInLambdas(int q) {
47          int test = 21;
48          int isInOuterScope = 32;
49          Predicate<Integer> foo = integer -> {
50              int b; // violation, 'Unused local variable'
51              int a = integer;
52              b = integer;
53              boolean ans = a > 12;
54              int c = 21; // violation, 'Unused local variable'
55              c = -isInOuterScope;
56              return ans;
57          };
58          foo.test(test);
59      }
60  
61      public void testMultipleIdentifiers() {
62          int[] arr = {1};
63          int a = 21;
64          arr[0] = a;
65          Class<Integer> b = int.class; // violation, 'Unused local variable'
66          @SuppressWarnings("unused")
67          int c = 41; // violation, 'Unused local variable'
68      }
69  
70      public void testChainedCalls() throws InterruptedException {
71          Obj p; // violation, 'Unused local variable'
72          Obj q = null;
73          p = q.foo().p;
74          if (s instanceof String) {
75          }
76          Boolean r = false;
77          while (r.booleanValue() != true) {
78          }
79          int a = 13, b = 21;
80          q.getClass().wait(a, b);
81          int f = 12; // violation, 'Unused local variable'
82          Predicate<String> obj = InputUnusedLocalVariable::f;
83          obj.test("test");
84          int foo = 12; // violation, 'Unused local variable'
85          foo(q);
86      }
87  
88      class SubClass extends InputUnusedLocalVariable {
89  
90          void testSuperKeyword() {
91              int a = 12; // violation, 'Unused local variable'
92              a = super.a;
93          }
94      }
95  
96      class Obj {
97          Obj p;
98  
99          Obj foo() {
100             return new Obj();
101         }
102     }
103 
104     Object foo(Object a) {
105         return a;
106     }
107 
108     public static boolean f(String a) {
109 
110         class k { // local class not to be added to "classes" stack
111             int a = 12;
112         }
113         return true;
114     }
115 
116 }