Skip to content

Commit 51b5c4d

Browse files
cushonError Prone Team
authored and
Error Prone Team
committed
Fix some crashes involving records
Fixes #2324, #2323, #2322 PiperOrigin-RevId: 373452319
1 parent cafdc69 commit 51b5c4d

File tree

7 files changed

+79
-1
lines changed

7 files changed

+79
-1
lines changed

check_api/src/main/java/com/google/errorprone/matchers/Matchers.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1311,11 +1311,15 @@ public static Matcher<Tree> hasAnyAnnotation(List<? extends TypeMirror> mirrors)
13111311
private static final ImmutableSet<Kind> DECLARATION =
13121312
Sets.immutableEnumSet(Kind.LAMBDA_EXPRESSION, Kind.CLASS, Kind.ENUM, Kind.INTERFACE);
13131313

1314+
private static boolean isDeclaration(Kind kind) {
1315+
return DECLARATION.contains(kind) || kind.name().equals("RECORD");
1316+
}
1317+
13141318
public static boolean methodCallInDeclarationOfThrowingRunnable(VisitorState state) {
13151319
return stream(state.getPath())
13161320
// Find the nearest definitional context for this method invocation
13171321
// (i.e.: the nearest surrounding class or lambda)
1318-
.filter(t -> DECLARATION.contains(t.getKind()))
1322+
.filter(t -> isDeclaration(t.getKind()))
13191323
.findFirst()
13201324
.map(t -> isThrowingFunctionalInterface(getType(t), state))
13211325
.orElseThrow(VerifyException::new);

check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public static boolean isAtLeast15() {
8585
return MAJOR >= 15;
8686
}
8787

88+
/** Returns true if the current runtime is JDK 16 or newer. */
89+
public static boolean isAtLeast16() {
90+
return MAJOR >= 16;
91+
}
92+
8893
/**
8994
* Returns the latest {@code --release} version.
9095
*

core/src/main/java/com/google/errorprone/bugpatterns/PrivateConstructorForUtilityClass.java

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ private static boolean isInstance(Tree tree) {
107107
case INTERFACE:
108108
return false;
109109
default:
110+
if (tree.getKind().name().equals("RECORD")) {
111+
return false;
112+
}
110113
throw new AssertionError("unknown member type:" + tree.getKind());
111114
}
112115
}

core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafety.java

+3
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ public Violation visitType(Type type, Void s) {
562562
case CLASS:
563563
break;
564564
default:
565+
if (type.tsym.getKind().name().equals("RECORD")) {
566+
break;
567+
}
565568
throw new AssertionError(String.format("Unexpected type kind %s", type.tsym.getKind()));
566569
}
567570
if (WellKnownMutability.isAnnotation(state, type)) {

core/src/test/java/com/google/errorprone/bugpatterns/FieldCanBeStaticTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package com.google.errorprone.bugpatterns;
1818

19+
import static org.junit.Assume.assumeTrue;
20+
1921
import com.google.errorprone.BugCheckerRefactoringTestHelper;
2022
import com.google.errorprone.CompilationTestHelper;
2123
import com.google.errorprone.ErrorProneFlags;
24+
import com.google.errorprone.util.RuntimeVersion;
2225
import org.junit.Test;
2326
import org.junit.runner.RunWith;
2427
import org.junit.runners.JUnit4;
@@ -286,4 +289,24 @@ public void inner() {
286289
"}")
287290
.doTest();
288291
}
292+
293+
@Test
294+
public void record() {
295+
assumeTrue(RuntimeVersion.isAtLeast16());
296+
compilationHelper
297+
.addSourceLines(
298+
"ExampleClass.java",
299+
"package example;",
300+
"public final class ExampleClass {",
301+
" public record OtherRecord(String value) {}",
302+
" public record SomeRecord(OtherRecord value) {",
303+
" public static SomeRecord fromValue(final OtherRecord value) {",
304+
" return new SomeRecord(value);",
305+
" }",
306+
" }",
307+
" private ExampleClass() {",
308+
" }",
309+
"}")
310+
.doTest();
311+
}
289312
}

core/src/test/java/com/google/errorprone/bugpatterns/PrivateConstructorForUtilityClassTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package com.google.errorprone.bugpatterns;
1717

18+
import static org.junit.Assume.assumeTrue;
19+
1820
import com.google.errorprone.BugCheckerRefactoringTestHelper;
1921
import com.google.errorprone.CompilationTestHelper;
22+
import com.google.errorprone.util.RuntimeVersion;
2023
import org.junit.Test;
2124
import org.junit.runner.RunWith;
2225
import org.junit.runners.JUnit4;
@@ -345,4 +348,18 @@ public void abstractClass_noPrivateConstructor() {
345348
.expectUnchanged()
346349
.doTest();
347350
}
351+
352+
@Test
353+
public void record() {
354+
assumeTrue(RuntimeVersion.isAtLeast16());
355+
testHelper
356+
.addInputLines(
357+
"ExampleUtilityClass.java",
358+
"package example;",
359+
"public final class ExampleUtilityClass {",
360+
" public record SomeRecord(String value) {}",
361+
"}")
362+
.expectUnchanged()
363+
.doTest();
364+
}
348365
}

core/src/test/java/com/google/errorprone/bugpatterns/StreamResourceLeakTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package com.google.errorprone.bugpatterns;
1818

19+
import static org.junit.Assume.assumeTrue;
20+
1921
import com.google.errorprone.BugCheckerRefactoringTestHelper;
2022
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
2123
import com.google.errorprone.CompilationTestHelper;
24+
import com.google.errorprone.util.RuntimeVersion;
2225
import org.junit.Test;
2326
import org.junit.runner.RunWith;
2427
import org.junit.runners.JUnit4;
@@ -285,4 +288,24 @@ public void defaultMethod() {
285288
"}")
286289
.doTest();
287290
}
291+
292+
@Test
293+
public void record() {
294+
assumeTrue(RuntimeVersion.isAtLeast16());
295+
testHelper
296+
.addSourceLines(
297+
"ExampleRecord.java",
298+
"package example;",
299+
"import java.io.IOException;",
300+
"import java.nio.file.Files;",
301+
"import java.nio.file.Path;",
302+
"import java.util.stream.Stream;",
303+
"record ExampleRecord(Path path) {",
304+
" public Stream<Path> list() throws IOException {",
305+
" // BUG: Diagnostic contains: should be closed",
306+
" return Files.list(path);",
307+
" }",
308+
"}")
309+
.doTest();
310+
}
288311
}

0 commit comments

Comments
 (0)