Skip to content

Commit 94a93e9

Browse files
graememorganError Prone Team
authored and
Error Prone Team
committed
Fix ImmutableChecker to allow wildcard @ImmutableTypeParameters which aren't provably immutable.
This is safe, as any instantiated object must have immutable type parameters. Note: this is only true for `@ImmutableTypeParameter`, not `containerOf`! The latter is conditionally immutable, so we really do need to check the bound. (And the corresponding change for ThreadSafe.) PiperOrigin-RevId: 530601585
1 parent b482675 commit 94a93e9

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ public Violation threadSafeInstantiation(
429429
getPrettyName(type.tsym), typaram));
430430
}
431431
Type tyarg = type.getTypeArguments().get(i);
432+
// Don't check whether wildcard types' erasure is safe. Wildcards can only be used where
433+
// the type isn't being instantiated, and we can rely on instantiations all being checked.
434+
if (immutableTypeParameter && tyarg instanceof WildcardType) {
435+
continue;
436+
}
432437
if (suppressAnnotation != null
433438
&& tyarg.getAnnotationMirrors().stream()
434439
.anyMatch(

core/src/test/java/com/google/errorprone/bugpatterns/threadsafety/ImmutableCheckerTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,26 @@ public void mutableWildInstantiation() {
811811
.doTest();
812812
}
813813

814+
@Test
815+
public void mutableWildcardInstantiation_immutableTypeParameter() {
816+
compilationHelper
817+
.addSourceLines(
818+
"A.java",
819+
"import com.google.errorprone.annotations.Immutable;",
820+
"import com.google.errorprone.annotations.ImmutableTypeParameter;",
821+
"@Immutable",
822+
"class A<@ImmutableTypeParameter T> {}")
823+
.addSourceLines(
824+
"B.java",
825+
"import com.google.errorprone.annotations.Immutable;",
826+
"@Immutable",
827+
"class B {",
828+
" private final A<?> a;",
829+
" public B(A<?> a) { this.a = a; }",
830+
"}")
831+
.doTest();
832+
}
833+
814834
@Test
815835
public void mutableRawType() {
816836
compilationHelper

0 commit comments

Comments
 (0)