19
19
import static com .google .common .collect .ImmutableSet .toImmutableSet ;
20
20
import static com .google .common .collect .Iterables .getOnlyElement ;
21
21
import static com .google .errorprone .BugPattern .SeverityLevel .SUGGESTION ;
22
- import static com .google .errorprone .bugpatterns .ImmutableSetForContains .UsageState .ALLOWED_USAGE ;
23
22
import static com .google .errorprone .bugpatterns .ImmutableSetForContains .UsageState .DISALLOWED_USAGE ;
24
23
import static com .google .errorprone .bugpatterns .ImmutableSetForContains .UsageState .NEVER_USED ;
24
+ import static com .google .errorprone .bugpatterns .ImmutableSetForContains .UsageState .ONLY_ALLOWED_USAGE ;
25
25
import static com .google .errorprone .matchers .Matchers .allOf ;
26
26
import static com .google .errorprone .matchers .Matchers .anyOf ;
27
27
import static com .google .errorprone .matchers .Matchers .hasAnnotationWithSimpleName ;
@@ -125,7 +125,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
125
125
if (isSuppressed (var , state )) {
126
126
continue ;
127
127
}
128
- if (usageScanner .varUsages .get (getSymbol (var )) == UsageState . ALLOWED_USAGE ) {
128
+ if (usageScanner .varUsages .get (getSymbol (var )). shouldReport () ) {
129
129
firstReplacement = Optional .of (var );
130
130
fix .merge (convertListToSetInit (var , state ));
131
131
}
@@ -228,8 +228,9 @@ private boolean allowedFuncOnImmutableVar(MethodInvocationTree methodTree, Visit
228
228
return false ;
229
229
}
230
230
boolean allowed = ALLOWED_FUNCTIONS_ON_LIST .matches (methodTree , state );
231
- if (allowed && (varUsages .get (getSymbol (receiver )) == NEVER_USED )) {
232
- varUsages .put (getSymbol (receiver ), ALLOWED_USAGE );
231
+ if (allowed ) {
232
+ varUsages .computeIfPresent (
233
+ getSymbol (receiver ), (sym , oldVal ) -> oldVal .markAllowedUsageDetected ());
233
234
}
234
235
return allowed ;
235
236
}
@@ -247,13 +248,33 @@ public Void visitMemberSelect(MemberSelectTree memberSelectTree, VisitorState vi
247
248
}
248
249
249
250
private void recordDisallowedUsage (Symbol symbol ) {
250
- varUsages .computeIfPresent (symbol , (sym , oldVal ) -> DISALLOWED_USAGE );
251
+ varUsages .computeIfPresent (symbol , (sym , oldVal ) -> oldVal . markDisallowedUsageDetected () );
251
252
}
252
253
}
253
254
254
255
enum UsageState {
256
+ /** No usage detected. */
255
257
NEVER_USED ,
256
- ALLOWED_USAGE ,
257
- DISALLOWED_USAGE
258
+
259
+ /** At least one allowed usage detected. No disallowed usage detected. */
260
+ ONLY_ALLOWED_USAGE ,
261
+
262
+ /** Disallowed usage detected. Allowed usage is ignored. */
263
+ DISALLOWED_USAGE ;
264
+
265
+ UsageState markAllowedUsageDetected () {
266
+ if (this == DISALLOWED_USAGE ) {
267
+ return DISALLOWED_USAGE ;
268
+ }
269
+ return ONLY_ALLOWED_USAGE ;
270
+ }
271
+
272
+ UsageState markDisallowedUsageDetected () {
273
+ return DISALLOWED_USAGE ;
274
+ }
275
+
276
+ boolean shouldReport () {
277
+ return this == ONLY_ALLOWED_USAGE ;
278
+ }
258
279
}
259
280
}
0 commit comments