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.ArrayList;
10  import java.util.function.Predicate;
11  
12  public class InputUnusedLocalVariable2 {
13      int a = 12;
14  
15      public void testStatementsWithoutSlistToken() {
16          int k = 1;
17          for (int i = 0; k < 1; i++) // violation, 'Unused local variable'
18              i++;
19          for (int j = 0; ; ) { // violation, 'Unused local variable'
20              j++;
21              break;
22          }
23          ArrayList<String> obj = new ArrayList<>();
24          for (String s : obj) {
25              System.out.println(s);
26          }
27      }
28  
29      public void testChainedCallsWithNewKeyword() {
30          Object a; // violation, 'Unused local variable'
31          Object b; // violation, 'Unused local variable'
32          a = testDifferentAssignments(new c.b(new d.a()));
33          a = testDifferentAssignments(new b().new a());
34          b = testDifferentAssignments(new a.d());
35      }
36  
37      static class Test implements a {
38          public void testMethodReference() {
39              int b = 12; // violation, 'Unused local variable'
40              int Test = 12; // violation, 'Unused local variable'
41              a obj = Test::new; // violation, 'Unused local variable'
42              Test ab = new Test();
43              Predicate<String> a = ab::b;
44              a.test("abc");
45          }
46  
47          private boolean b(String s) {
48              return true;
49          }
50  
51          public static boolean ba(String s) {
52              return true;
53          }
54  
55          @Override
56          public void method() {
57          }
58      }
59  
60      public Object testDifferentAssignments(Object obj) {
61          int a = 12; // violation, 'Unused local variable'
62          a = 13;
63          int b = 13;
64          b /= 12;
65          int c = 1;
66          c -= this.a;
67          int h = 1;
68          h <<= 12;
69          return null;
70      }
71  
72      public void testDoWhileLoops(int s) {
73          do {
74              int a = 12;
75              do {
76                  int b = 1; // violation, 'Unused local variable'
77                  b = a;
78              }
79              while (s > 1);
80          }
81          while (s > 0);
82      }
83  
84      interface a {
85          void method();
86  
87          class d {
88          }
89      }
90  
91      static class c {
92          static class b {
93              public b(Object a) {
94              }
95          }
96      }
97  
98      static class d {
99          public static class a {
100         }
101     }
102 
103     class b {
104         class a {
105         }
106     }
107 }