Skip to content

Commit 90b8efb

Browse files
graememorganError Prone Team
authored and
Error Prone Team
committed
Allow binding to BINDING_VARIABLEs in GuardedByBinder.
PiperOrigin-RevId: 748301805
1 parent 62b8d6e commit 90b8efb

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,14 @@ public GuardedByExpression visitIdentifier(IdentifierTree node, BinderContext co
264264
Symbol symbol = context.resolver.resolveIdentifier(node);
265265
checkGuardedBy(symbol != null, "Could not resolve %s", node);
266266
if (symbol instanceof Symbol.VarSymbol varSymbol) {
267-
switch (varSymbol.getKind()) {
268-
case LOCAL_VARIABLE, PARAMETER -> {
269-
return F.localVariable(varSymbol);
270-
}
271-
case FIELD -> {
272-
if (symbol.name.contentEquals("this")) {
273-
return F.thisliteral();
274-
}
275-
return F.select(computeBase(context, varSymbol), varSymbol);
276-
}
267+
return switch (varSymbol.getKind()) {
268+
case LOCAL_VARIABLE, PARAMETER, BINDING_VARIABLE -> F.localVariable(varSymbol);
269+
case FIELD ->
270+
symbol.name.contentEquals("this")
271+
? F.thisliteral()
272+
: F.select(computeBase(context, varSymbol), varSymbol);
277273
default -> throw new IllegalGuardedBy(varSymbol.getKind().toString());
278-
}
274+
};
279275
} else if (symbol instanceof Symbol.MethodSymbol methodSymbol) {
280276
return F.select(computeBase(context, symbol), methodSymbol);
281277
} else if (symbol instanceof Symbol.ClassSymbol) {

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

+26
Original file line numberDiff line numberDiff line change
@@ -2485,4 +2485,30 @@ public synchronized void test(List<?> xs) {
24852485
""")
24862486
.doTest();
24872487
}
2488+
2489+
@Test
2490+
public void bindingVariable() {
2491+
compilationHelper
2492+
.addSourceLines(
2493+
"I.java",
2494+
"""
2495+
import com.google.errorprone.annotations.concurrent.GuardedBy;
2496+
2497+
interface I {
2498+
class Impl implements I {
2499+
@GuardedBy("this")
2500+
private int number = 42;
2501+
}
2502+
2503+
public static void t(I other) {
2504+
if (other instanceof Impl otherImpl) {
2505+
synchronized (otherImpl) {
2506+
int a = otherImpl.number;
2507+
}
2508+
}
2509+
}
2510+
}
2511+
""")
2512+
.doTest();
2513+
}
24882514
}

0 commit comments

Comments
 (0)