Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamOptionalGetWithoutFilter #2949

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.baseline.errorprone;

import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.matchers.ChildMultiMatcher.MatchType;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

@AutoService(BugChecker.class)
@BugPattern(
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks",
linkType = BugPattern.LinkType.CUSTOM,
severity = SeverityLevel.WARNING,
summary = "Stream<Optional<?>> should call filter(Optional::isPresent) before "
+ "map(Optional::get) or map(Optional::orElseThrow)",
explanation = "Calling map(Optional::get) on a Stream<Optional<?>> without first calling "
+ "filter(Optional::isPresent) can cause NoSuchElementException.")
public final class StreamOptionalGetWithoutFilter extends BugChecker implements BugChecker.MethodInvocationTreeMatcher {

private static final Matcher<ExpressionTree> OPTIONAL_GET_METHOD = Matchers.staticMethod()
.onClass(Optional.class.getName())
.named("get")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably also want to include orElseThrow here.

.withNoParameters();
private static final Matcher<ExpressionTree> OPTIONAL_OR_ELSE_THROW_METHOD = Matchers.staticMethod()
.onClass(Optional.class.getName())
.named("orElseThrow")
.withNoParameters();

private static final Matcher<ExpressionTree> OPTIONAL_UNWRAP_METHOD =
Matchers.anyOf(OPTIONAL_GET_METHOD, OPTIONAL_OR_ELSE_THROW_METHOD);

private static final Matcher<ExpressionTree> STREAM_MAP_METHOD = Matchers.instanceMethod()
.onDescendantOf(Stream.class.getName())
.named("map")
.withParameters(Function.class.getName());

private static final Matcher<ExpressionTree> STREAM_FILTER_METHOD = Matchers.instanceMethod()
.onDescendantOf(Stream.class.getName())
.named("filter")
.withParameters(Predicate.class.getName());

private static final Matcher<ExpressionTree> OPTIONAL_IS_PRESENT_METHOD = Matchers.instanceMethod()
.onDescendantOf(Optional.class.getName())
.named("isPresent")
.withNoParameters();

private static final Matcher<ExpressionTree> STREAM_FILTER_IS_PRESENT =
Matchers.methodInvocation(STREAM_FILTER_METHOD, MatchType.LAST, OPTIONAL_IS_PRESENT_METHOD);

private static final Matcher<ExpressionTree> STREAM_MAP_OPTIONAL_GET =
Matchers.methodInvocation(STREAM_MAP_METHOD, MatchType.LAST, OPTIONAL_UNWRAP_METHOD);

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (STREAM_MAP_OPTIONAL_GET.matches(tree, state)) {
ExpressionTree receiver = ASTHelpers.getReceiver(tree);
if (receiver != null && !hasFilterIsPresent(receiver, state)) {
return describeMatch(tree);
}
}
return Description.NO_MATCH;
}

private static boolean hasFilterIsPresent(ExpressionTree receiver, VisitorState state) {
if (receiver instanceof MethodInvocationTree) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) receiver;
return STREAM_FILTER_IS_PRESENT.matches(methodInvocationTree, state)
|| hasFilterIsPresent(methodInvocationTree.getMethodSelect(), state);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.baseline.errorprone;

import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;

class StreamOptionalGetWithoutFilterTest {
@Test
public void testPositive() {
compile()
.addSourceLines(
"Test.java",
"import java.util.Optional;",
"import java.util.stream.Stream;",
"public class Test {",
" Stream<String> test1(Stream<Optional<String>> stream) {",
" // BUG: Diagnostic contains filter(Optional::isPresent) before map(Optional::get)",
" return stream.map(Optional::get);",
" }",
" Stream<String> test2(Stream<Optional<String>> stream) {",
" // BUG: Diagnostic contains filter(Optional::isPresent) before map(Optional::get)",
" return stream.map(opt -> opt.get());",
" }",
" Stream<String> test3(Stream<Optional<String>> stream) {",
" // BUG: Diagnostic contains filter(Optional::isPresent) before map(Optional::get)"
+ " or map(Optional::orElseThrow)",
" return stream.map(opt -> opt.orElseThrow());",
" }",
" Stream<String> test4(Stream<Optional<String>> stream) {",
" // BUG: Diagnostic contains filter(Optional::isPresent) before map(Optional::get)"
+ " or map(Optional::orElseThrow)",
" return stream.map(Optional::orElseThrow);",
" }",
"}")
.doTest();
}

@Test
public void testNegative() {
compile()
.addSourceLines(
"Test.java",
"import java.util.Optional;",
"import java.util.stream.Stream;",
"public class Test {",
" Stream<String> test1(Stream<Optional<String>> stream) {",
" return stream.filter(Optional::isPresent)",
" .map(Optional::get);",
" }",
" Stream<String> test2(Stream<Optional<String>> stream) {",
" return stream.filter(opt -> opt.isPresent())",
" .map(opt -> opt.get());",
" }",
" Stream<String> test3(Stream<Optional<String>> stream) {",
" return stream.filter(opt -> opt.isPresent())",
" .map(Optional::get);",
" }",
" Stream<String> test4(Stream<Optional<String>> stream) {",
" return stream.filter(opt -> opt.isPresent())",
" .map(opt -> opt.get())",
" .filter(s -> s.length() > 2);",
" }",
" Stream<String> test5(Stream<Optional<String>> stream) {",
" return stream.filter(opt -> opt.isPresent())",
" .map(Optional::orElseThrow);",
" }",
" Stream<String> test6(Stream<Optional<String>> stream) {",
" return stream.filter(opt -> opt.isPresent())",
" .map(opt -> opt.orElseThrow());",
" }",
"}")
.doTest();
}

private CompilationTestHelper compile() {
return CompilationTestHelper.newInstance(StreamOptionalGetWithoutFilter.class, getClass());
}
}