Skip to content

Commit 22c7428

Browse files
cushonError Prone Team
authored andcommitted
Apply Optional cleanups to Error Prone
PiperOrigin-RevId: 830605360
1 parent c22fce9 commit 22c7428

30 files changed

+66
-90
lines changed

check_api/src/main/java/com/google/errorprone/dataflow/nullnesspropagation/NullnessAnnotations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public static Optional<Nullness> getUpperBound(TypeVariable typeVar) {
211211

212212
// If the type variable doesn't have an explicit bound, see if its declaration is in the scope
213213
// of a default and use that as the bound.
214-
return result.isPresent() ? result : fromDefaultAnnotations(typeVar.asElement());
214+
return result.or(() -> fromDefaultAnnotations(typeVar.asElement()));
215215
}
216216

217217
private static Optional<Nullness> fromAnnotationStream(

check_api/src/main/java/com/google/errorprone/dataflow/nullnesspropagation/inference/InferredNullability.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,18 @@ private Optional<Nullness> getNullness(InferenceVariable iv) {
8787
result =
8888
constraintGraph.predecessors(iv).stream()
8989
.map(this::getNullness)
90-
.filter(Optional::isPresent)
91-
.map(Optional::get)
90+
.flatMap(Optional::stream)
9291
.reduce(Nullness::leastUpperBound); // use least upper bound (lub) to combine
9392
// 2. If not, resolve successors and use them as upper bounds
94-
if (!result.isPresent()) {
93+
if (result.isEmpty()) {
9594
result =
9695
constraintGraph.successors(iv).stream()
9796
.map(this::getNullness)
98-
.filter(Optional::isPresent)
99-
.map(Optional::get)
97+
.flatMap(Optional::stream)
10098
.reduce(Nullness::greatestLowerBound); // use greatest lower bound (glb) to combine
10199
}
102100

103-
checkState(!inferredMemoTable.put(iv, result).isPresent());
101+
checkState(inferredMemoTable.put(iv, result).isEmpty());
104102
return result;
105103
}
106104
}

check_api/src/main/java/com/google/errorprone/dataflow/nullnesspropagation/inference/NullnessQualifierInference.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ private void generateConstraintsFromAnnotations(
153153

154154
Optional<Nullness> fromAnnotations =
155155
extractExplicitNullness(declaredType, argSelector.isEmpty() ? decl : null);
156-
if (!fromAnnotations.isPresent()) {
156+
if (fromAnnotations.isEmpty()) {
157157
// Check declared type before inferred type so that type annotations on the declaration take
158158
// precedence (just like declaration annotations) over annotations on the inferred type.
159159
// For instance, we want a @Nullable T m() to take precedence over annotations on T's inferred
160160
// type (e.g., @NotNull String), whether @Nullable is a declaration or type annotation.
161161
fromAnnotations = NullnessAnnotations.fromAnnotationsOn(inferredType);
162162
}
163-
if (!fromAnnotations.isPresent()) {
163+
if (fromAnnotations.isEmpty()) {
164164
if (declaredType instanceof TypeVariable typeVariable) {
165165
// Check bounds second so explicit annotations take precedence. Even for bounds we still use
166166
// equality constraint below since we have to assume the bound as the "worst" case.
@@ -359,7 +359,7 @@ private static ImmutableSet<InferenceVariable> findUnannotatedTypeVarRefs(
359359
new ArrayDeque<>(),
360360
null,
361361
(typeVarRef, selector, unused) -> {
362-
if (!extractExplicitNullness(typeVarRef, selector.isEmpty() ? decl : null).isPresent()) {
362+
if (extractExplicitNullness(typeVarRef, selector.isEmpty() ? decl : null).isEmpty()) {
363363
result.add(TypeArgInferenceVar.create(ImmutableList.copyOf(selector), sourceNode));
364364
}
365365
});
@@ -378,7 +378,7 @@ private void visitUnannotatedTypeVarRefsAndEquateInferredComponents(
378378
new ArrayDeque<>(),
379379
((JCExpression) sourceNode).type,
380380
(declaredType, selector, inferredType) -> {
381-
if (!extractExplicitNullness(type, selector.isEmpty() ? decl : null).isPresent()) {
381+
if (extractExplicitNullness(type, selector.isEmpty() ? decl : null).isEmpty()) {
382382
consumer.accept(TypeArgInferenceVar.create(ImmutableList.copyOf(selector), sourceNode));
383383
}
384384

@@ -482,7 +482,7 @@ private void generateConstraintsForWrite(
482482
boolean isBound = false;
483483
Optional<Nullness> fromAnnotations =
484484
extractExplicitNullness(lType, argSelector.isEmpty() ? decl : null);
485-
if (!fromAnnotations.isPresent()) {
485+
if (fromAnnotations.isEmpty()) {
486486
if (lType instanceof TypeVariable typeVariable) {
487487
fromAnnotations = NullnessAnnotations.getUpperBound(typeVariable);
488488
isBound = true;

check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ public static SuggestedFix.Builder addValuesToAnnotationArgument(
11321132
.replaceFirst("\\(\\)", "(" + parameterPrefix + newArgument(newValues) + ")"));
11331133
}
11341134
Optional<ExpressionTree> maybeExistingArgument = findArgument(annotation, parameterName);
1135-
if (!maybeExistingArgument.isPresent()) {
1135+
if (maybeExistingArgument.isEmpty()) {
11361136
return SuggestedFix.builder()
11371137
.prefixWith(
11381138
annotation.getArguments().getFirst(),
@@ -1192,7 +1192,7 @@ public static SuggestedFix.Builder updateAnnotationArgumentValues(
11921192
+ ')');
11931193
}
11941194
Optional<ExpressionTree> maybeExistingArgument = findArgument(annotation, parameterName);
1195-
if (!maybeExistingArgument.isPresent()) {
1195+
if (maybeExistingArgument.isEmpty()) {
11961196
return SuggestedFix.builder()
11971197
.prefixWith(
11981198
annotation.getArguments().getFirst(),

check_api/src/main/java/com/google/errorprone/matchers/method/MethodInvocationMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ public static Matcher<ExpressionTree> compile(Iterable<Rule> rules) {
483483
// First collect all the nodes that accept any token at all
484484
for (Node node : curr) {
485485
for (Map.Entry<Optional<Token>, Node> entry : nfa.row(node).entrySet()) {
486-
if (!entry.getKey().isPresent()) {
486+
if (entry.getKey().isEmpty()) {
487487
acceptsAny.add(entry.getValue());
488488
}
489489
}

check_api/src/main/java/com/google/errorprone/util/Comments.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static ImmutableList<Commented<ExpressionTree>> findCommentsForArguments
111111

112112
CharSequence sourceCode = state.getSourceCode();
113113
Optional<Integer> endPosition = computeEndPosition(tree, sourceCode, state);
114-
if (!endPosition.isPresent()) {
114+
if (endPosition.isEmpty()) {
115115
return noComments(arguments);
116116
}
117117

check_api/src/test/java/com/google/errorprone/util/CommentsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static class ComputeEndPosition extends BugChecker implements MethodInvoc
5454
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
5555
CharSequence sourceCode = state.getSourceCode();
5656
Optional<Integer> endPosition = Comments.computeEndPosition(tree, sourceCode, state);
57-
if (!endPosition.isPresent()) {
57+
if (endPosition.isEmpty()) {
5858
return Description.NO_MATCH;
5959
}
6060
int startPosition = endPosition.get();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public Description matchNewClass(NewClassTree newClassTree, VisitorState state)
161161
public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) {
162162
Description description =
163163
matcher.get().matches(tree, state) ? describeReturnValueIgnored(tree, state) : NO_MATCH;
164-
if (!lostType(state).isPresent() || !description.equals(NO_MATCH)) {
164+
if (lostType(state).isEmpty() || !description.equals(NO_MATCH)) {
165165
return description;
166166
}
167167
if (lostReferenceTreeMatcher.get().matches(tree, state)) {
@@ -335,7 +335,7 @@ && matchingMethods(
335335
* this a constructor call or build() call?"
336336
*/
337337
if (parent instanceof ExpressionStatementTree
338-
&& !constantExpressions.constantExpression(invocationTree, state).isPresent()
338+
&& constantExpressions.constantExpression(invocationTree, state).isEmpty()
339339
&& considerBlanketFixes) {
340340
ImmutableSet<String> identifiersInScope =
341341
findAllIdents(state).stream().map(v -> v.name.toString()).collect(toImmutableSet());
@@ -407,7 +407,7 @@ protected String getMessage(Name name) {
407407

408408
private Description checkLostType(MethodInvocationTree tree, VisitorState state) {
409409
Optional<Type> optionalType = lostType(state);
410-
if (!optionalType.isPresent()) {
410+
if (optionalType.isEmpty()) {
411411
return NO_MATCH;
412412
}
413413

@@ -567,7 +567,7 @@ private static boolean isExemptedInterfaceMethod(MethodSymbol symbol, VisitorSta
567567
@Override
568568
public Description matchReturn(ReturnTree tree, VisitorState state) {
569569
Optional<Type> optionalType = lostType(state);
570-
if (!optionalType.isPresent()) {
570+
if (optionalType.isEmpty()) {
571571
return NO_MATCH;
572572
}
573573
Type objectType = state.getSymtab().objectType;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
9191
}
9292
Optional<JCCatch> maybeCatchTree =
9393
catchesType(tryStatement, state.getSymtab().assertionErrorType, state);
94-
if (!maybeCatchTree.isPresent()) {
94+
if (maybeCatchTree.isEmpty()) {
9595
return NO_MATCH;
9696
}
9797
JCCatch catchTree = maybeCatchTree.get();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) {
229229
Arrays.stream(CollectionTypes.values())
230230
.filter(type -> type.constructorMatcher.matches(tree, state))
231231
.findFirst();
232-
if (!collectionType.isPresent()) {
232+
if (collectionType.isEmpty()) {
233233
return NO_MATCH;
234234
}
235235
Description.Builder description = buildDescription(tree);

0 commit comments

Comments
 (0)