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

Add more Optional methods to ReturnValueIgnored #2282

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -30,6 +30,7 @@

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers;
Expand Down Expand Up @@ -204,8 +205,27 @@ private static boolean functionalMethod(ExpressionTree tree, VisitorState state)
private static final Matcher<ExpressionTree> OPTIONAL_METHODS =
anyOf(
staticMethod().onClass("java.util.Optional"),
instanceMethod().onExactClass("java.util.Optional").named("isPresent"),
instanceMethod().onExactClass("java.util.Optional").named("isEmpty"));
instanceMethod().onExactClass("java.util.Optional")
.namedAnyOf(
"isEmpty",
"isPresent"));

/**
* The return values of {@link java.util.Optional} static methods and some instance methods should
* always be checked.
*/
private static final Matcher<ExpressionTree> MORE_OPTIONAL_METHODS =
anyOf(
instanceMethod().onExactClass("java.util.Optional")
.namedAnyOf(
"filter",
"flatMap",
"get",
"map",
"or",
"orElse",
"orElseGet",
"orElseThrow"));

/**
* The return values of {@link java.util.concurrent.TimeUnit} methods should always be checked.
Expand Down Expand Up @@ -251,8 +271,16 @@ private static boolean functionalMethod(ExpressionTree tree, VisitorState state)
.namedAnyOf("containsKey", "containsValue")
.withParameters("java.lang.Object"));

private final Matcher<? super ExpressionTree> matcher;

public ReturnValueIgnored(ErrorProneFlags flags) {
boolean checkOptional = flags.getBoolean("ReturnValueIgnored:MoreOptional").orElse(true);
this.matcher =
checkOptional ? anyOf(SPECIALIZED_MATCHER, MORE_OPTIONAL_METHODS) : SPECIALIZED_MATCHER;
}

@Override
public Matcher<? super ExpressionTree> specializedMatcher() {
return SPECIALIZED_MATCHER;
return matcher;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package com.google.errorprone.bugpatterns;

import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.junit.Assume.assumeTrue;

/** @author alexeagle@google.com (Alex Eagle) */
@RunWith(JUnit4.class)
public class ReturnValueIgnoredTest {
Expand Down Expand Up @@ -169,9 +172,72 @@ public void optionalInstanceMethods() {
" void optional() {",
" Optional<Integer> optional = Optional.of(42);",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.isPresent();",
" optional.filter(v -> v > 40);",
" optional.map(v -> Integer.toString(v));",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.flatMap(v -> Optional.of(v + 1));",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.get();",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.isPresent();",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.map(v -> v + 1);",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.orElse(40);",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.orElseGet(() -> 40);",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.orElseThrow(() -> new RuntimeException());",
" }",
"}")
.doTest();
}

@Test
public void optionalInstanceMethods_jdk9() {
assumeTrue(RuntimeVersion.isAtLeast9());
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.Optional;",
"class Test {",
" void optional() {",
" Optional<Integer> optional = Optional.of(42);",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.or(() -> Optional.empty());",
" }",
"}")
.doTest();
}

@Test
public void optionalInstanceMethods_jdk10() {
assumeTrue(RuntimeVersion.isAtLeast10());
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.Optional;",
"class Test {",
" void optional() {",
" Optional<Integer> optional = Optional.of(42);",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.orElseThrow();",
" }",
"}")
.doTest();
}

@Test
public void optionalInstanceMethods_jdk11() {
assumeTrue(RuntimeVersion.isAtLeast11());
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.Optional;",
"class Test {",
" void optional() {",
" Optional<Integer> optional = Optional.of(42);",
" // BUG: Diagnostic contains: ReturnValueIgnored",
" optional.isEmpty();",
" }",
"}")
.doTest();
Expand Down