Skip to content
Open
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
Expand Up @@ -17,15 +17,16 @@
package com.google.errorprone.bugpatterns;

import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.bugpatterns.nullness.NullnessUtils.hasDefinitelyNullBranch;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.isSubtypeOf;
import static com.google.errorprone.matchers.Matchers.methodReturns;
import static com.google.errorprone.util.ASTHelpers.findEnclosingMethod;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.sun.source.tree.Tree.Kind.NULL_LITERAL;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.ReturnTreeMatcher;
Expand Down Expand Up @@ -62,7 +63,12 @@ private static boolean methodWithoutNullable(MethodTree tree, VisitorState state

@Override
public final Description matchReturn(ReturnTree tree, VisitorState state) {
if (tree.getExpression() == null || tree.getExpression().getKind() != NULL_LITERAL) {
if (tree.getExpression() == null
|| !hasDefinitelyNullBranch(
tree.getExpression(),
/* definitelyNullVars= */ ImmutableSet.of(),
/* varsProvenNullByParentIf= */ ImmutableSet.of(),
state)) {
return NO_MATCH;
}
MethodTree methodTree = findEnclosingMethod(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.google.errorprone.bugpatterns.inject.dagger;

import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.bugpatterns.nullness.NullnessUtils.hasDefinitelyNullBranch;
import static com.google.errorprone.util.ASTHelpers.findEnclosingMethod;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
Expand All @@ -31,7 +33,6 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ReturnTree;
import com.sun.source.tree.Tree.Kind;
import com.sun.tools.javac.code.Symbol.MethodSymbol;

/**
Expand All @@ -53,7 +54,12 @@ public class ProvidesNull extends BugChecker implements ReturnTreeMatcher {
@Override
public Description matchReturn(ReturnTree returnTree, VisitorState state) {
ExpressionTree returnExpression = returnTree.getExpression();
if (returnExpression == null || returnExpression.getKind() != Kind.NULL_LITERAL) {
if (returnExpression == null
|| !hasDefinitelyNullBranch(
returnExpression,
/* definitelyNullVars= */ ImmutableSet.of(),
/* varsProvenNullByParentIf= */ ImmutableSet.of(),
state)) {
return Description.NO_MATCH;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
*
* @author awturner@google.com (Andy Turner)
*/
final class NullnessUtils {
public final class NullnessUtils {
private NullnessUtils() {}

private static final Matcher<ExpressionTree> OPTIONAL_OR_NULL =
Expand Down Expand Up @@ -472,7 +472,7 @@ enum Polarity {
}
}

static boolean hasDefinitelyNullBranch(
public static boolean hasDefinitelyNullBranch(
ExpressionTree tree,
Set<VarSymbol> definitelyNullVars,
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ public void ternary_b536946282_shouldBeFlagged() {

class Test {
List<String> methodReturnsNullTernary(boolean foo, List<String> bar) {
// TODO(b/536946282): should be flagged by ReturnsNullCollection
// BUG: Diagnostic contains: ReturnsNullCollection
return foo ? bar : null;
}

List<String> methodReturnsNullTernaryReversed(boolean foo, List<String> bar) {
// TODO(b/536946282): should be flagged by ReturnsNullCollection
// BUG: Diagnostic contains: ReturnsNullCollection
return foo ? null : bar;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@ public void ternary_b536946282_shouldBeFlagged() {
public class Test {
@Provides
public Object providesObject(boolean foo, Object bar) {
// TODO(b/536946282): should be flagged by ProvidesNull
// BUG: Diagnostic contains: Did you mean '@Nullable' or 'throw new RuntimeException();'
return foo ? bar : null;
}

@Provides
public Object providesObjectReversed(boolean foo, Object bar) {
// TODO(b/536946282): should be flagged by ProvidesNull
// BUG: Diagnostic contains: Did you mean '@Nullable' or 'throw new RuntimeException();'
return foo ? null : bar;
}
}
Expand Down
Loading