Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ where
f.getVariable().getVariable() = v and
f.getExpr().getType().(RefType).hasQualifiedName("kotlin.ranges", ["IntRange", "LongRange"])
) and
// Unnamed variables (`_`) cannot be read.
not v.getDeclExpr().isAnonymous() and
not readImplicitly(v)
select v, "Variable '" + v + "' is never read."
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `java/local-variable-is-never-read` query no longer reports unnamed variables (`_`), for example in `case Foo _ ->` patterns, `for (var _ : items)` loops, and `var _ = ...;` declarations.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package test;

import java.util.List;

public class UnnamedVariables
{
sealed interface Shape permits Circle, Square {}
record Circle(double radius) implements Shape {}
record Square(double side) implements Shape {}
record Pair(Object left, Object right) {}

// Not flagged: unnamed variables cannot be read
static int side(Shape shape)
{
return switch (shape) {
case Circle _ -> 0;
case Square s -> (int) s.side();
};
}

static Object left(Object o)
{
if (o instanceof Pair(var left, _)) {
return left;
}
return switch (o) {
case Pair(Object left, Object _) -> left;
default -> null;
};
}

static int count(List<String> items)
{
int n = 0;
for (String _ : items) {
n++;
}
return n;
}

static void removeFirst(List<String> items)
{
var _ = items.remove(0);
}
}
1 change: 1 addition & 0 deletions java/ql/test/query-tests/UnreadLocal/options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args --release 22
Loading