@@ -13,7 +13,6 @@ class UnusedLocalVariableCheck {
1313
1414 {
1515 int unused = 42 ; // Noncompliant
16- int _ = 42 ; // Compliant
1716 int used = 23 ; // Compliant
1817 System .out .println (used );
1918 }
@@ -41,18 +40,6 @@ public void f(int unusedParameter, Object o) {
4140 } catch (Exception e ) {
4241 }
4342
44- try (Stream stream1 = Stream .of (); // Noncompliant {{Remove this unused "stream1" local variable.}} [[quickfixes=qf_tr1]]
45- // ^^^^^^^
46- // fix@qf_tr1 {{Replace unused local variable with _}}
47- // edit@qf_tr1 [[sc=10;ec=24]] {{var _}}
48- Stream _ = Stream .of ();
49- Stream stream3 = Stream .of ()) { // Noncompliant {{Remove this unused "stream3" local variable.}} [[quickfixes=qf_tr2]]
50- // ^^^^^^^
51- // fix@qf_tr2 {{Replace unused local variable with _}}
52- // edit@qf_tr2 [[sc=10;ec=24]] {{var _}}
53- } catch (Exception _) {
54- }
55-
5643 for (int i = 0 ; condition (); i ++) { // Noncompliant
5744 }
5845
@@ -260,149 +247,5 @@ private void doNotOfferQuickFixes() {
260247// ^^^^^^^
261248 }
262249 }
263-
264- void test () {
265- record Bar (int used ) { } // Compliant
266- System .out .println (new Bar (42 ).used );
267- }
268- }
269-
270- sealed interface Shape permits Box , Circle {}
271- record Box () implements Shape { }
272- record Circle () implements Shape {}
273-
274- static void switchOnSealedClass (Shape shape ) {
275- switch (shape ) {
276- case Box unused -> { } // Noncompliant
277- case Circle circle -> circle .toString ();
278- }
279- }
280-
281- static void switchWithTypePattern (Object o ) {
282- switch (o ) {
283- case Number used -> used .longValue ();
284- case Shape unused -> { } // Noncompliant
285- default -> System .out .println ();
286- }
287- }
288-
289- record MyRecord (int x , int y ) { }
290-
291- static void switchRecordGuardedPattern (Object o ) {
292- switch (o ) {
293- case MyRecord (int x , int y ) when x > 42 -> { } // Noncompliant
294- // ^
295- // fix@qfswitch1 {{Replace unused local variable with _}}
296- // edit@qfswitch2 [[sc=28;ec=33]]{{var _}}
297- case MyRecord (int x , int y ) when y < 42 -> { } // Noncompliant
298- // ^
299- // fix@qfswitch2 {{Replace unused local variable with _}}
300- // edit@qfswitch2 [[sc=21;ec=26]]{{var _}}
301- case MyRecord m when m .x > 42 -> { }
302- case MyRecord m when o .toString ().length () > 42 -> { } // Noncompliant
303- case MyRecord (int x , _) -> { } // Noncompliant
304- case MyRecord m -> { } // Noncompliant
305- case Object object -> {
306- object .toString ();
307- var x = 42 ; // Noncompliant
308- System .out .println ();
309- }
310- }
311- }
312-
313- abstract class Ball {}
314- final class RedBall extends Ball {}
315- final class BlueBall extends Ball {}
316- final class GreenBall extends Ball {}
317-
318- record BallHolder <T extends Ball >(T ball ) { }
319-
320- record Point (int x , int y ) { }
321- record ColoredPoint (Point p , String color ) { }
322-
323- sealed interface Tree {}
324- record Node (Tree left , Tree right ) implements Tree {}
325- record Leaf () implements Tree {}
326-
327- void unnamedVariablesUseCases (Queue <Ball > queue , BallHolder <? extends Ball > ballHolder , ColoredPoint coloredPoint ) {
328- int total = 0 ;
329- int _ = 1 + 1 ;
330- java .util .function .IntUnaryOperator _ = (int _) -> 0 ;
331- java .util .function .IntUnaryOperator _ = _ -> 0 ;
332- java .util .function .IntBinaryOperator _ = (_ ,_ ) -> 0 ;
333- java .util .function .IntBinaryOperator _ = (int _, int _) -> 0 ;
334- for (Object _ : queue ) { // Compliant
335- total ++;
336- }
337- System .out .println (total );
338- for (int i = 0 , _ = 1 + 1 ; i < 2 ; i ++) {
339- System .out .println (i );
340- }
341- while (queue .size () > 2 ) {
342- var a = queue .remove ();
343- var _ = queue .remove (); // Compliant
344- System .out .println (a );
345- }
346-
347- try (var _ = new java .io .FileInputStream ("foo.txt" )) {
348- queue .remove ();
349- } catch (Exception _) { // Compliant
350- System .out .println ("Exception" );
351- }
352-
353- queue .stream ()
354- .collect (Collectors .toMap (Function .identity (), _ -> 42 )); // Compliant
355-
356- var ball = queue .remove ();
357- switch (ball ) {
358- case RedBall _ -> System .out .println ("Red" ); // Compliant
359- case BlueBall _ -> System .out .println ("Blue" ); // Compliant
360- default -> throw new IllegalStateException ("Unexpected value: " + ball );
361- }
362-
363- switch (ballHolder ) {
364- case BallHolder (RedBall _ ) -> System .out .println ("One Red" ); // Compliant
365- // FIXME: the following line is commented because ECJ 3.39.0 is not able to parse it, Syntax error on the second _.
366- // case BallHolder(BlueBall _), BallHolder(GreenBall _) -> System.out.println("Blue or Green Ball"); // Compliant
367- case BallHolder (var _ ) -> System .out .println ("Other" ); // Compliant
368- }
369-
370- switch (ballHolder ) {
371- // FIXME: the following line is commented because ECJ 3.39.0 is not able to parse it, Syntax error on the second _.
372- // case BallHolder(RedBall _), BallHolder(BlueBall _) -> System.out.println("Red or Blue Ball"); // Compliant
373- case BallHolder (_) -> System .out .println ("Other Ball" ); // Compliant
374- default -> System .out .println ("Other Ball" );
375- }
376-
377- if (ballHolder instanceof BallHolder (RedBall _ )) { // Compliant
378- System .out .println ("BallHolder with RedBall" );
379- }
380-
381- if (coloredPoint instanceof ColoredPoint (Point (_, _), _)) { // Compliant
382- System .out .println ("Point (_:_) with color not important" );
383- }
384-
385- if (coloredPoint instanceof ColoredPoint (Point (int x , int y ), _)) { // Compliant
386- System .out .println ("Point (" + x + ":" + y + ") with color not important" );
387- }
388-
389- if (coloredPoint instanceof ColoredPoint (Point (int x , int _ ), _)) { // Compliant
390- System .out .println ("Point (" + x + ":_) with color not important" );
391- }
392-
393- if (coloredPoint instanceof ColoredPoint (Point (_, int y ), _)) { // Compliant
394- System .out .println ("Point (_:" + y + ") with color not important" );
395- }
396-
397- if (coloredPoint instanceof ColoredPoint (Point (int x , _), _)) { // Noncompliant
398- }
399- }
400- void RecordPatternUnusedVars (Tree tree ){
401- if (tree instanceof Node (Leaf l , _)) { // Noncompliant
402- }
403- if (tree instanceof Node (Node (Leaf l , _), _)) { // Noncompliant
404- }
405- if (tree instanceof Node (Node (Leaf l1 , _), Node (_, _))) { // Noncompliant
406- }
407250 }
408251}
0 commit comments