Skip to content

Commit

Permalink
Permit lambda parameters to have type Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst committed Dec 12, 2023
1 parent 6d86197 commit 4fcedc1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.UnaryTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -536,7 +537,13 @@ public Void visitVariable(VariableTree tree, Void p) {
if (ekind.isField()) {
checker.reportWarning(tree, "optional.field");
} else if (ekind == ElementKind.PARAMETER) {
checker.reportWarning(tree, "optional.parameter");
TreePath paramPath = getCurrentPath();
Tree parent = paramPath.getParentPath().getLeaf();
if (parent.getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
// Exception to rule: lambda parameters can have type Optional.
} else {
checker.reportWarning(tree, "optional.parameter");
}
}
}
return super.visitVariable(tree, p);
Expand Down
4 changes: 1 addition & 3 deletions checker/tests/optional/IfPresentRefinement.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import java.util.Optional;

@SuppressWarnings("optional.parameter")
public class IfPresentRefinement {

@SuppressWarnings("optional.parameter")
void m1(Optional<String> o) {
o.ifPresent(s -> o.get());
}

@SuppressWarnings("optional.parameter")
void m2(Optional<String> o) {
o.ifPresentOrElse(s -> o.get(), () -> {});
}

@SuppressWarnings("optional.parameter")
void m3(Optional<String> o) {
// :: error: (method.invocation)
o.ifPresentOrElse(s -> o.get(), () -> o.get());
Expand Down
13 changes: 13 additions & 0 deletions checker/tests/optional/OptionalParameterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class OptionalParameterTest {
public void findDatesByIds2(List<Integer> ids) {
ids.stream()
.map(Optional::ofNullable)
.flatMap(optional -> optional.map(Stream::of).orElseGet(Stream::empty))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3730,6 +3730,8 @@ public void setVisitorTreePath(@Nullable TreePath visitorTreePath) {
* <p>Note that the given Tree has to be within the current compilation unit, otherwise null will
* be returned.
*
* <p>Within a subclass of BaseTypeVisitor, use {@code getCurrentPath()} rather than this method.
*
* @param tree the {@link Tree} to get the path for
* @return the path for {@code tree} under the current root. Returns null if {@code tree} is not
* within the current compilation unit.
Expand Down

0 comments on commit 4fcedc1

Please sign in to comment.