Skip to content

Commit 6cbafd3

Browse files
graememorganError Prone Team
authored andcommitted
Remove the TypeCompatibility:UseCapture flag.
PiperOrigin-RevId: 865408499
1 parent 2b5284f commit 6cbafd3

File tree

9 files changed

+86
-195
lines changed

9 files changed

+86
-195
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibility.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,11 @@ public final class TypeCompatibility {
5858
private static final String WITHOUT_EQUALS_REASON =
5959
". Though these types are the same, the type doesn't implement equals.";
6060
private final boolean treatBuildersAsIncomparable;
61-
private final boolean useCapture;
6261

6362
@Inject
6463
TypeCompatibility(ErrorProneFlags flags) {
6564
this.treatBuildersAsIncomparable =
6665
flags.getBoolean("TypeCompatibility:TreatBuildersAsIncomparable").orElse(true);
67-
this.useCapture = flags.getBoolean("TypeCompatibility:UseCapture").orElse(true);
68-
}
69-
70-
public boolean useCapture() {
71-
return useCapture;
7266
}
7367

7468
public TypeCompatibilityReport compatibilityOfTypes(
@@ -350,9 +344,9 @@ private TypeCompatibilityReport checkForGenericsMismatch(
350344
.orElse(TypeCompatibilityReport.compatible());
351345
}
352346

353-
private List<Type> typeArgsAsSuper(Type baseType, Type superType, VisitorState state) {
354-
Type toSuper = useCapture ? state.getTypes().capture(baseType) : baseType;
355-
Type projectedType = state.getTypes().asSuper(toSuper, superType.tsym);
347+
private static List<Type> typeArgsAsSuper(Type baseType, Type superType, VisitorState state) {
348+
Type projectedType =
349+
state.getTypes().asSuper(state.getTypes().capture(baseType), superType.tsym);
356350
if (projectedType != null) {
357351
return projectedType.getTypeArguments();
358352
}

core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/AbstractCollectionIncompatibleTypeMatcher.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@ public abstract class AbstractCollectionIncompatibleTypeMatcher {
5656
*
5757
* @return the source type or null if not available
5858
*/
59-
abstract @Nullable Type extractSourceType(
60-
MethodInvocationTree tree, VisitorState state, boolean useCapture);
59+
abstract @Nullable Type extractSourceType(MethodInvocationTree tree, VisitorState state);
6160

62-
abstract @Nullable Type extractSourceType(
63-
MemberReferenceTree tree, VisitorState state, boolean useCapture);
61+
abstract @Nullable Type extractSourceType(MemberReferenceTree tree, VisitorState state);
6462

6563
/**
6664
* Returns the AST node from which the source type was extracted. Needed to produce readable error
@@ -93,11 +91,9 @@ public abstract class AbstractCollectionIncompatibleTypeMatcher {
9391
*
9492
* @return the target type or null if not available
9593
*/
96-
abstract @Nullable Type extractTargetType(
97-
MethodInvocationTree tree, VisitorState state, boolean useCapture);
94+
abstract @Nullable Type extractTargetType(MethodInvocationTree tree, VisitorState state);
9895

99-
abstract @Nullable Type extractTargetType(
100-
MemberReferenceTree tree, VisitorState state, boolean useCapture);
96+
abstract @Nullable Type extractTargetType(MemberReferenceTree tree, VisitorState state);
10197

10298
/**
10399
* Encapsulates the result of matching a {@link Collection#contains}-like call, including the
@@ -125,8 +121,7 @@ public Optional<Fix> buildFix() {
125121
}
126122
}
127123

128-
public final @Nullable MatchResult matches(
129-
ExpressionTree tree, VisitorState state, boolean useCapture) {
124+
public final @Nullable MatchResult matches(ExpressionTree tree, VisitorState state) {
130125
if (!methodMatcher().matches(tree, state)) {
131126
return null;
132127
}
@@ -137,17 +132,17 @@ public MatchResult visitMethodInvocation(
137132
MethodInvocationTree methodInvocationTree, Void unused) {
138133
return getMatchResult(
139134
extractSourceTree(methodInvocationTree, state),
140-
extractSourceType(methodInvocationTree, state, useCapture),
141-
extractTargetType(methodInvocationTree, state, useCapture));
135+
extractSourceType(methodInvocationTree, state),
136+
extractTargetType(methodInvocationTree, state));
142137
}
143138

144139
@Override
145140
public MatchResult visitMemberReference(
146141
MemberReferenceTree memberReferenceTree, Void unused) {
147142
return getMatchResult(
148143
extractSourceTree(memberReferenceTree, state),
149-
extractSourceType(memberReferenceTree, state, useCapture),
150-
extractTargetType(memberReferenceTree, state, useCapture));
144+
extractSourceType(memberReferenceTree, state),
145+
extractTargetType(memberReferenceTree, state));
151146
}
152147
}.visit(tree, null);
153148
}
@@ -173,9 +168,8 @@ public MatchResult visitMemberReference(
173168
* @return the type argument, if defined, or null otherwise
174169
*/
175170
protected static @Nullable Type extractTypeArgAsMemberOfSupertype(
176-
Type type, Symbol superTypeSym, int typeArgIndex, Types types, boolean useCapture) {
177-
Type toSuper = useCapture ? types.capture(type) : type;
178-
Type collectionType = types.asSuper(toSuper, superTypeSym);
171+
Type type, Symbol superTypeSym, int typeArgIndex, Types types) {
172+
Type collectionType = types.asSuper(types.capture(type), superTypeSym);
179173
if (collectionType == null) {
180174
return null;
181175
}

core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/BinopMatcher.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,22 @@ Matcher<ExpressionTree> methodMatcher() {
4343
}
4444

4545
@Override
46-
@Nullable Type extractSourceType(
47-
MethodInvocationTree tree, VisitorState state, boolean useCapture) {
46+
@Nullable Type extractSourceType(MethodInvocationTree tree, VisitorState state) {
4847
return extractTypeArgAsMemberOfSupertype(
4948
getType(tree.getArguments().get(0)),
5049
state.getSymbolFromString(collectionType),
5150
0,
52-
state.getTypes(),
53-
useCapture);
51+
state.getTypes());
5452
}
5553

5654
@Override
57-
@Nullable Type extractSourceType(
58-
MemberReferenceTree tree, VisitorState state, boolean useCapture) {
55+
@Nullable Type extractSourceType(MemberReferenceTree tree, VisitorState state) {
5956
Type descriptorType = state.getTypes().findDescriptorType(getType(tree));
6057
return extractTypeArgAsMemberOfSupertype(
6158
descriptorType.getParameterTypes().get(0),
6259
state.getSymbolFromString(collectionType),
6360
0,
64-
state.getTypes(),
65-
useCapture);
61+
state.getTypes());
6662
}
6763

6864
@Override
@@ -76,26 +72,22 @@ Matcher<ExpressionTree> methodMatcher() {
7672
}
7773

7874
@Override
79-
@Nullable Type extractTargetType(
80-
MethodInvocationTree tree, VisitorState state, boolean useCapture) {
75+
@Nullable Type extractTargetType(MethodInvocationTree tree, VisitorState state) {
8176
return extractTypeArgAsMemberOfSupertype(
8277
getType(tree.getArguments().get(1)),
8378
state.getSymbolFromString(collectionType),
8479
0,
85-
state.getTypes(),
86-
useCapture);
80+
state.getTypes());
8781
}
8882

8983
@Override
90-
@Nullable Type extractTargetType(
91-
MemberReferenceTree tree, VisitorState state, boolean useCapture) {
84+
@Nullable Type extractTargetType(MemberReferenceTree tree, VisitorState state) {
9285
Type descriptorType = state.getTypes().findDescriptorType(getType(tree));
9386
return extractTypeArgAsMemberOfSupertype(
9487
descriptorType.getParameterTypes().get(1),
9588
state.getSymbolFromString(collectionType),
9689
0,
97-
state.getTypes(),
98-
useCapture);
90+
state.getTypes());
9991
}
10092

10193
@Override

core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/CollectionIncompatibleType.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,12 @@ private enum FixType {
7373

7474
private final FixType fixType;
7575
private final TypeCompatibility typeCompatibility;
76-
private final boolean useCapture;
7776

7877
@Inject
7978
CollectionIncompatibleType(TypeCompatibility typeCompatibility, ErrorProneFlags flags) {
8079
this.fixType =
8180
flags.getEnum("CollectionIncompatibleType:FixType", FixType.class).orElse(FixType.NONE);
8281
this.typeCompatibility = typeCompatibility;
83-
this.useCapture = flags.getBoolean("TypeCompatibility:UseCapture").orElse(true);
8482
}
8583

8684
@Override
@@ -94,7 +92,7 @@ public Description matchMemberReference(MemberReferenceTree tree, VisitorState s
9492
}
9593

9694
public Description match(ExpressionTree tree, VisitorState state) {
97-
MatchResult result = ContainmentMatchers.firstNonNullMatchResult(tree, state, useCapture);
95+
MatchResult result = ContainmentMatchers.firstNonNullMatchResult(tree, state);
9896
if (result == null) {
9997
return NO_MATCH;
10098
}

core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/ContainmentMatchers.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,19 @@ public final class ContainmentMatchers {
112112
.build();
113113

114114
public static @Nullable MatchResult firstNonNullMatchResult(
115-
ExpressionTree tree, VisitorState state, boolean useCapture) {
115+
ExpressionTree tree, VisitorState state) {
116116
if (!FIRST_ORDER_MATCHER.matches(tree, state)) {
117117
return null;
118118
}
119119

120120
for (AbstractCollectionIncompatibleTypeMatcher matcher : ContainmentMatchers.ALL_MATCHERS) {
121-
MatchResult result = matcher.matches(tree, state, useCapture);
121+
MatchResult result = matcher.matches(tree, state);
122122
if (result != null) {
123123
return result;
124124
}
125125
}
126126
return null;
127127
}
128128

129-
/** Backwards compatibility overload for external callers. */
130-
public static @Nullable MatchResult firstNonNullMatchResult(
131-
ExpressionTree tree, VisitorState state) {
132-
return firstNonNullMatchResult(tree, state, /* useCapture= */ true);
133-
}
134-
135129
private ContainmentMatchers() {}
136130
}

core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/IncompatibleArgumentType.java

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,12 @@ public Description matchMethodInvocation(
9898

9999
Types types = state.getTypes();
100100

101-
boolean useCapture = typeCompatibility.useCapture();
102101
if (!populateTypesToEnforce(
103-
declaredMethod,
104-
calledMethodType,
105-
calledClazzType,
106-
requiredTypesAtCallSite,
107-
state,
108-
useCapture)) {
102+
declaredMethod, calledMethodType, calledClazzType, requiredTypesAtCallSite, state)) {
109103
// No annotations on this method, try the supers;
110104
for (MethodSymbol method : ASTHelpers.findSuperMethods(declaredMethod, types)) {
111105
if (populateTypesToEnforce(
112-
method,
113-
calledMethodType,
114-
calledClazzType,
115-
requiredTypesAtCallSite,
116-
state,
117-
useCapture)) {
106+
method, calledMethodType, calledClazzType, requiredTypesAtCallSite, state)) {
118107
break;
119108
}
120109
}
@@ -177,8 +166,7 @@ private static boolean populateTypesToEnforce(
177166
Type calledMethodType,
178167
Type calledReceiverType,
179168
List<RequiredType> argumentTypeRequirements,
180-
VisitorState state,
181-
boolean useCapture) {
169+
VisitorState state) {
182170
boolean foundAnyTypeToEnforce = false;
183171
List<VarSymbol> params = declaredMethod.params();
184172
for (int i = 0; i < params.size(); i++) {
@@ -191,12 +179,7 @@ private static boolean populateTypesToEnforce(
191179
// method call's projection of this generic type.
192180
RequiredType requiredType =
193181
resolveRequiredTypeForThisCall(
194-
state,
195-
calledMethodType,
196-
calledReceiverType,
197-
declaredMethod,
198-
anno.value(),
199-
useCapture);
182+
state, calledMethodType, calledReceiverType, declaredMethod, anno.value());
200183

201184
// @CW is on the varags parameter
202185
if (declaredMethod.isVarArgs() && i == params.size() - 1) {
@@ -226,19 +209,14 @@ private static boolean populateTypesToEnforce(
226209
Type calledMethodType,
227210
Type calledReceiverType,
228211
MethodSymbol declaredMethod,
229-
String typeArgName,
230-
boolean useCapture) {
212+
String typeArgName) {
231213
RequiredType requiredType =
232214
resolveTypeFromGenericMethod(calledMethodType, declaredMethod, typeArgName);
233215

234216
if (requiredType == null) {
235217
requiredType =
236218
resolveTypeFromClass(
237-
calledReceiverType,
238-
(ClassSymbol) declaredMethod.owner,
239-
typeArgName,
240-
state,
241-
useCapture);
219+
calledReceiverType, (ClassSymbol) declaredMethod.owner, typeArgName, state);
242220
}
243221
return requiredType;
244222
}
@@ -270,17 +248,12 @@ private static boolean populateTypesToEnforce(
270248
// class Foo<X> { void something(@CW("X") Object x); }
271249
// new Foo<String>().something(123);
272250
private static @Nullable RequiredType resolveTypeFromClass(
273-
Type calledType,
274-
ClassSymbol clazzSymbol,
275-
String typeArgName,
276-
VisitorState state,
277-
boolean useCapture) {
251+
Type calledType, ClassSymbol clazzSymbol, String typeArgName, VisitorState state) {
278252
// Try on the class
279253
int tyargIndex = findTypeArgInList(clazzSymbol, typeArgName);
280254
if (tyargIndex != -1) {
281255
return RequiredType.create(
282-
extractTypeArgAsMemberOfSupertype(
283-
calledType, clazzSymbol, tyargIndex, state.getTypes(), useCapture));
256+
extractTypeArgAsMemberOfSupertype(calledType, clazzSymbol, tyargIndex, state.getTypes()));
284257
}
285258

286259
while (clazzSymbol.isInner()) {

core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/MethodArgMatcher.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,31 @@ ExpressionTree extractSourceTree(MethodInvocationTree tree, VisitorState state)
8585
}
8686

8787
@Override
88-
Type extractSourceType(MethodInvocationTree tree, VisitorState state, boolean useCapture) {
88+
Type extractSourceType(MethodInvocationTree tree, VisitorState state) {
8989
return getType(extractSourceTree(tree, state));
9090
}
9191

9292
@Override
93-
@Nullable Type extractSourceType(
94-
MemberReferenceTree tree, VisitorState state, boolean useCapture) {
93+
@Nullable Type extractSourceType(MemberReferenceTree tree, VisitorState state) {
9594
return state.getTypes().findDescriptorType(getType(tree)).getParameterTypes().getFirst();
9695
}
9796

9897
@Override
99-
Type extractTargetType(MethodInvocationTree tree, VisitorState state, boolean useCapture) {
98+
Type extractTargetType(MethodInvocationTree tree, VisitorState state) {
10099
return extractTypeArgAsMemberOfSupertype(
101100
ASTHelpers.getReceiverType(tree),
102101
state.getSymbolFromString(typeName),
103102
typeArgIndex,
104-
state.getTypes(),
105-
useCapture);
103+
state.getTypes());
106104
}
107105

108106
@Override
109-
@Nullable Type extractTargetType(
110-
MemberReferenceTree tree, VisitorState state, boolean useCapture) {
107+
@Nullable Type extractTargetType(MemberReferenceTree tree, VisitorState state) {
111108
return extractTypeArgAsMemberOfSupertype(
112109
ASTHelpers.getReceiverType(tree),
113110
state.getSymbolFromString(typeName),
114111
typeArgIndex,
115-
state.getTypes(),
116-
useCapture);
112+
state.getTypes());
117113
}
118114

119115
@Override

0 commit comments

Comments
 (0)