diff options
author | George Fraser <george@fivetran.com> | 2019-01-05 13:57:10 -0800 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2019-01-05 13:57:10 -0800 |
commit | b0c116dced0be0ab8ea7b2def7ae244c03236d62 (patch) | |
tree | d2dcc2a97b156697b493a32f48047cf91cb3c07f | |
parent | 0d86af4b0fe9ccc4f0a5bb25669ec5906a2c5ed0 (diff) | |
download | java-language-server-b0c116dced0be0ab8ea7b2def7ae244c03236d62.zip |
More unused
-rw-r--r-- | src/main/java/org/javacs/JavaCompilerService.java | 10 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaLanguageServer.java | 3 | ||||
-rw-r--r-- | src/main/java/org/javacs/WarnUnused.java | 12 | ||||
-rw-r--r-- | src/main/java/org/javacs/Warning.java | 6 | ||||
-rw-r--r-- | src/test/java/org/javacs/WarningsTest.java | 3 | ||||
-rw-r--r-- | src/test/test-project/workspace/src/org/javacs/warn/Unused.java | 8 |
6 files changed, 37 insertions, 5 deletions
diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java index 3d47052..6c7dd8b 100644 --- a/src/main/java/org/javacs/JavaCompilerService.java +++ b/src/main/java/org/javacs/JavaCompilerService.java @@ -170,8 +170,14 @@ public class JavaCompilerService { warnUnused.scan(r, null); for (var unusedEl : warnUnused.notUsed()) { var path = trees.getPath(unusedEl); - diags.add( - new Warning(task, path, "unused", String.format("`%s` is not used", unusedEl.getSimpleName()))); + var message = String.format("`%s` is not used", unusedEl.getSimpleName()); + Diagnostic.Kind kind; + if (unusedEl instanceof ExecutableElement) { + kind = Diagnostic.Kind.OTHER; + } else { + kind = Diagnostic.Kind.WARNING; + } + diags.add(new Warning(task, path, kind, "unused", message)); } } diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index aaaa432..a8e79e6 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -67,9 +67,10 @@ class JavaLanguageServer extends LanguageServer { case MANDATORY_WARNING: return DiagnosticSeverity.Warning; case NOTE: + return DiagnosticSeverity.Information; case OTHER: default: - return DiagnosticSeverity.Information; + return DiagnosticSeverity.Hint; } } diff --git a/src/main/java/org/javacs/WarnUnused.java b/src/main/java/org/javacs/WarnUnused.java index 8263f4a..35c7165 100644 --- a/src/main/java/org/javacs/WarnUnused.java +++ b/src/main/java/org/javacs/WarnUnused.java @@ -37,6 +37,10 @@ class WarnUnused extends TreePathScanner<Void, Void> { return t.getModifiers().getFlags().contains(Modifier.PRIVATE); } + boolean isEmptyConstructor(MethodTree t) { + return t.getParameters().isEmpty() && trees.getElement(getCurrentPath()).getKind() == ElementKind.CONSTRUCTOR; + } + @Override public Void visitVariable(VariableTree t, Void __) { if (isPrivate(t) || isLocal(t)) { @@ -46,6 +50,14 @@ class WarnUnused extends TreePathScanner<Void, Void> { } @Override + public Void visitMethod(MethodTree t, Void __) { + if (isPrivate(t) && !isEmptyConstructor(t)) { + declared.add(current()); + } + return super.visitMethod(t, null); + } + + @Override public Void visitIdentifier(IdentifierTree t, Void __) { used.add(current()); return super.visitIdentifier(t, null); diff --git a/src/main/java/org/javacs/Warning.java b/src/main/java/org/javacs/Warning.java index a173d40..3dcaccf 100644 --- a/src/main/java/org/javacs/Warning.java +++ b/src/main/java/org/javacs/Warning.java @@ -11,21 +11,23 @@ class Warning implements Diagnostic<JavaFileObject> { private final JavaFileObject source; private final LineMap lines; private final long start, end; + private final Diagnostic.Kind kind; private final String code, message; - Warning(JavacTask task, TreePath path, String code, String message) { + Warning(JavacTask task, TreePath path, Diagnostic.Kind kind, String code, String message) { this.source = path.getCompilationUnit().getSourceFile(); this.lines = path.getCompilationUnit().getLineMap(); var pos = Trees.instance(task).getSourcePositions(); this.start = pos.getStartPosition(path.getCompilationUnit(), path.getLeaf()); this.end = pos.getEndPosition(path.getCompilationUnit(), path.getLeaf()); + this.kind = kind; this.code = code; this.message = message; } @Override public Kind getKind() { - return Kind.WARNING; + return kind; } @Override diff --git a/src/test/java/org/javacs/WarningsTest.java b/src/test/java/org/javacs/WarningsTest.java index c82c80f..09154c2 100644 --- a/src/test/java/org/javacs/WarningsTest.java +++ b/src/test/java/org/javacs/WarningsTest.java @@ -34,8 +34,11 @@ public class WarningsTest { assertThat(errors, hasItem("unused(7)")); // int unusedLocal assertThat(errors, hasItem("unused(10)")); // int unusedPrivate assertThat(errors, hasItem("unused(13)")); // int unusedLocalInLambda + assertThat(errors, hasItem("unused(16)")); // int unusedMethod() { ... } + assertThat(errors, hasItem("unused(22)")); // private Unused(int i) { } assertThat(errors, not(hasItem("unused(6)"))); // test(int unusedParam) assertThat(errors, not(hasItem("unused(12)"))); // unusedLambdaParam -> {}; + assertThat(errors, not(hasItem("unused(20)"))); // private Unused() { } } // 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 7852379..c8d0be3 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 @@ -12,4 +12,12 @@ class Unused { Consumer<Integer> lambda = unusedLambdaParam -> { int unusedLocalInLambda; }; + + private int unusedMethod() { + return 0; + } + + private Unused() { } + + private Unused(int i) { } }
\ No newline at end of file |