Skip to content

Commit 181f991

Browse files
cpovirkError Prone Team
authored andcommitted
Use ASTHelpers.enclosingClass.
(followup to 941974e) I'm not sure why I was reflexively resistant to this. Probably I was just in a hurry to submit? :) It's not as if the logic in `enclosingClass` is arcane. While I still don't think we need to worry that any of our calls to `enclClass` are happening on a module or a class itself—and if we do, we'll likely have bigger problems with the other nearby operations on `sym.owner`—I think it's probably better to "default" to using `enclosingClass`, which at least squirrels away this one particular use of internal APIs in a single location. That's especially true when it would not _quite_ be safe for us to mechanically replace every `enclClass` call with an `enclosingClass` call, thanks to the slightly differences in behavior. But here, not only do I claim that there should be no difference in behavior, but I actually tested. PiperOrigin-RevId: 500171678
1 parent 360ed99 commit 181f991

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/nullness/NullArgumentForNonNullParameter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.google.errorprone.bugpatterns.nullness.NullnessUtils.nullnessChecksShouldBeConservative;
2525
import static com.google.errorprone.matchers.Description.NO_MATCH;
2626
import static com.google.errorprone.suppliers.Suppliers.typeFromString;
27+
import static com.google.errorprone.util.ASTHelpers.enclosingClass;
2728
import static com.google.errorprone.util.ASTHelpers.enclosingPackage;
2829
import static com.google.errorprone.util.ASTHelpers.getSymbol;
2930
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
@@ -175,7 +176,7 @@ && isParameterOfMethodOnType(sym, ARGUMENT_CAPTOR_CLASS, state)) {
175176
}
176177

177178
// Hardcoding #3: Immutable*.Builder.*
178-
if (sym.enclClass().name.equals(BUILDER_NAME.get(state))
179+
if (enclosingClass(sym).name.equals(BUILDER_NAME.get(state))
179180
&& (isParameterOfMethodOnTypeStartingWith(sym, GUAVA_COLLECT_IMMUTABLE_PREFIX, state)
180181
|| isParameterOfMethodOnTypeStartingWith(sym, GUAVA_GRAPH_IMMUTABLE_PREFIX, state))) {
181182
return true;
@@ -224,12 +225,12 @@ && isParameterOfMethodOnType(sym, ARGUMENT_CAPTOR_CLASS, state)) {
224225
private static boolean isParameterOfMethodOnType(
225226
VarSymbol sym, Supplier<Type> typeSupplier, VisitorState state) {
226227
Type target = typeSupplier.get(state);
227-
return target != null && state.getTypes().isSameType(sym.enclClass().type, target);
228+
return target != null && state.getTypes().isSameType(enclosingClass(sym).type, target);
228229
}
229230

230231
private static boolean isParameterOfMethodOnTypeStartingWith(
231232
VarSymbol sym, Supplier<Name> nameSupplier, VisitorState state) {
232-
return sym.enclClass().fullname.startsWith(nameSupplier.get(state));
233+
return enclosingClass(sym).fullname.startsWith(nameSupplier.get(state));
233234
}
234235

235236
private boolean enclosingAnnotationDefaultsNonTypeVariablesToNonNull(

0 commit comments

Comments
 (0)