Skip to content

Commit

Permalink
Use ASTHelpers.enclosingClass.
Browse files Browse the repository at this point in the history
(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
  • Loading branch information
cpovirk authored and Error Prone Team committed Jan 6, 2023
1 parent 360ed99 commit 181f991
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.google.errorprone.bugpatterns.nullness.NullnessUtils.nullnessChecksShouldBeConservative;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.suppliers.Suppliers.typeFromString;
import static com.google.errorprone.util.ASTHelpers.enclosingClass;
import static com.google.errorprone.util.ASTHelpers.enclosingPackage;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
Expand Down Expand Up @@ -175,7 +176,7 @@ && isParameterOfMethodOnType(sym, ARGUMENT_CAPTOR_CLASS, state)) {
}

// Hardcoding #3: Immutable*.Builder.*
if (sym.enclClass().name.equals(BUILDER_NAME.get(state))
if (enclosingClass(sym).name.equals(BUILDER_NAME.get(state))
&& (isParameterOfMethodOnTypeStartingWith(sym, GUAVA_COLLECT_IMMUTABLE_PREFIX, state)
|| isParameterOfMethodOnTypeStartingWith(sym, GUAVA_GRAPH_IMMUTABLE_PREFIX, state))) {
return true;
Expand Down Expand Up @@ -224,12 +225,12 @@ && isParameterOfMethodOnType(sym, ARGUMENT_CAPTOR_CLASS, state)) {
private static boolean isParameterOfMethodOnType(
VarSymbol sym, Supplier<Type> typeSupplier, VisitorState state) {
Type target = typeSupplier.get(state);
return target != null && state.getTypes().isSameType(sym.enclClass().type, target);
return target != null && state.getTypes().isSameType(enclosingClass(sym).type, target);
}

private static boolean isParameterOfMethodOnTypeStartingWith(
VarSymbol sym, Supplier<Name> nameSupplier, VisitorState state) {
return sym.enclClass().fullname.startsWith(nameSupplier.get(state));
return enclosingClass(sym).fullname.startsWith(nameSupplier.get(state));
}

private boolean enclosingAnnotationDefaultsNonTypeVariablesToNonNull(
Expand Down

0 comments on commit 181f991

Please sign in to comment.