Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrictUnusedVariable handles Java 14 records #1412

Merged
merged 5 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -768,6 +768,14 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {

@Override
public Void visitMethod(MethodTree tree, Void unused) {
// From the perspective of an errorprone rule there are two standalone trees for a single `record`
// definition; A MethodTree which looks like a void function and a ClassTree which has the record fields.
//
// Its unclear why both trees are emitted, but we can identify and ignore a record's MethodTree by checking
// if it does not have any associated source.
if (state.getEndPosition(tree) < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slick

return null;
}
return isSuppressed(tree) ? null : super.visitMethod(tree, unused);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.palantir.baseline.errorprone;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

public class StrictUnusedVariableTest {

Expand Down Expand Up @@ -295,6 +298,22 @@ public void fixes_previously_suppressed_variables() {
.doTest(TestMode.TEXT_MATCH);
}

@Test
@DisabledForJreRange(max = JRE.JAVA_13)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a nifty annotation!

public void testRecord() {
compilationHelper = CompilationTestHelper.newInstance(StrictUnusedVariable.class, getClass())
.setArgs(ImmutableList.of("--enable-preview", "--release", "14"));

compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
"@SuppressWarnings(\"StrictUnusedVariable\")",
" record Foo(int foo) {}",
"}")
.doTest();
}

@Test
public void testSuppression() {
refactoringTestHelper
Expand Down