summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2019-01-05 13:32:22 -0800
committerGeorge Fraser <george@fivetran.com>2019-01-05 13:32:22 -0800
commit0d86af4b0fe9ccc4f0a5bb25669ec5906a2c5ed0 (patch)
tree6a832952b06692bc631869d8e6db4b5f88589da6
parent7005490c456bbd24ebb9115ed8f78bc3fc8d11e8 (diff)
downloadjava-language-server-0d86af4b0fe9ccc4f0a5bb25669ec5906a2c5ed0.zip
Don't warn unused lambda params
-rw-r--r--src/main/java/org/javacs/CompileBatch.java14
-rw-r--r--src/main/java/org/javacs/WarnUnused.java4
-rw-r--r--src/test/java/org/javacs/WarningsTest.java8
-rw-r--r--src/test/test-project/workspace/src/org/javacs/warn/Unused.java6
4 files changed, 16 insertions, 16 deletions
diff --git a/src/main/java/org/javacs/CompileBatch.java b/src/main/java/org/javacs/CompileBatch.java
index 0a27170..db62004 100644
--- a/src/main/java/org/javacs/CompileBatch.java
+++ b/src/main/java/org/javacs/CompileBatch.java
@@ -114,12 +114,7 @@ public class CompileBatch {
var refs = new ArrayList<TreePath>();
class FindDefinitions extends TreePathScanner<Void, Void> {
boolean sameSymbol(Element found) {
- if (el.equals(found)) {
- var uri = getCurrentPath().getCompilationUnit().getSourceFile().toUri();
- var fileName = Parser.fileName(uri);
- return true;
- }
- return false;
+ return el.equals(found);
}
boolean isSubMethod(Element found) {
@@ -195,12 +190,7 @@ public class CompileBatch {
var refs = new HashMap<Element, List<TreePath>>();
class FindReferences extends TreePathScanner<Void, Void> {
boolean sameSymbol(Element from, Element to) {
- if (to.equals(from)) {
- var uri = getCurrentPath().getCompilationUnit().getSourceFile().toUri();
- var fileName = Parser.fileName(uri);
- return true;
- }
- return false;
+ return to.equals(from);
}
boolean isSuperMethod(Element from, Element to) {
diff --git a/src/main/java/org/javacs/WarnUnused.java b/src/main/java/org/javacs/WarnUnused.java
index 22f97e0..8263f4a 100644
--- a/src/main/java/org/javacs/WarnUnused.java
+++ b/src/main/java/org/javacs/WarnUnused.java
@@ -28,7 +28,9 @@ class WarnUnused extends TreePathScanner<Void, Void> {
boolean isLocal(VariableTree t) {
var parent = getCurrentPath().getParentPath().getLeaf();
- return !(parent instanceof ClassTree) && !(parent instanceof MethodTree);
+ return !(parent instanceof ClassTree)
+ && !(parent instanceof MethodTree)
+ && !(parent instanceof LambdaExpressionTree);
}
boolean isPrivate(MethodTree t) {
diff --git a/src/test/java/org/javacs/WarningsTest.java b/src/test/java/org/javacs/WarningsTest.java
index f9e54d4..c82c80f 100644
--- a/src/test/java/org/javacs/WarningsTest.java
+++ b/src/test/java/org/javacs/WarningsTest.java
@@ -31,9 +31,11 @@ public class WarningsTest {
@Test
public void unusedLocal() {
server.reportErrors(List.of(FindResource.uri("org/javacs/warn/Unused.java")));
- assertThat(errors, hasItem("unused(5)")); // int unusedLocal
- assertThat(errors, hasItem("unused(8)")); // int unusedPrivate
- assertThat(errors, not(hasItem("unused(4)")));
+ assertThat(errors, hasItem("unused(7)")); // int unusedLocal
+ assertThat(errors, hasItem("unused(10)")); // int unusedPrivate
+ assertThat(errors, hasItem("unused(13)")); // int unusedLocalInLambda
+ assertThat(errors, not(hasItem("unused(6)"))); // test(int unusedParam)
+ assertThat(errors, not(hasItem("unused(12)"))); // unusedLambdaParam -> {};
}
// TODO warn on type.equals(otherType)
diff --git a/src/test/test-project/workspace/src/org/javacs/warn/Unused.java b/src/test/test-project/workspace/src/org/javacs/warn/Unused.java
index 94e04b7..7852379 100644
--- a/src/test/test-project/workspace/src/org/javacs/warn/Unused.java
+++ b/src/test/test-project/workspace/src/org/javacs/warn/Unused.java
@@ -1,9 +1,15 @@
package org.javacs.warn;
+import java.util.function.Consumer;
+
class Unused {
void test(int unusedParam) {
int unusedLocal = 1;
}
private int unusedPrivate;
+
+ Consumer<Integer> lambda = unusedLambdaParam -> {
+ int unusedLocalInLambda;
+ };
} \ No newline at end of file