diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NestedOptionals.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NestedOptionals.java index b736d5a050..8fe464fd09 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NestedOptionals.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NestedOptionals.java @@ -41,8 +41,11 @@ public NestedOptionals() {} @Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { - return state.getTypes().isSubtype(ASTHelpers.getType(tree), OPTIONAL_OF_OPTIONAL.get(state)) - ? describeMatch(tree) - : Description.NO_MATCH; + Type type = OPTIONAL_OF_OPTIONAL.get(state); + if (type == null || !state.getTypes().isSubtype(ASTHelpers.getType(tree), type)) { + return Description.NO_MATCH; + } + + return describeMatch(tree); } } diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NestedOptionalsTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NestedOptionalsTest.java index d5154cf554..5cebca69e5 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NestedOptionalsTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NestedOptionalsTest.java @@ -39,4 +39,19 @@ void identification() { "}") .doTest(); } + + @Test + void identificationOptionalTypeNotLoaded() { + compilationTestHelper + .addSourceLines( + "A.java", + "import java.time.Duration;", + "", + "class A {", + " void m() {", + " Duration.ofSeconds(1);", + " }", + "}") + .doTest(); + } }