Skip to content

Commit

Permalink
Add UUID.fromString to AlwaysThrows.
Browse files Browse the repository at this point in the history
Fixes external #3006

PiperOrigin-RevId: 437203105
  • Loading branch information
graememorgan authored and Error Prone Team committed Mar 25, 2022
1 parent f8185d1 commit e81b5df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Expand Up @@ -38,7 +38,6 @@
import com.google.errorprone.bugpatterns.threadsafety.ConstantExpressions;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.method.MethodMatchers;
import com.google.errorprone.util.ASTHelpers;
import com.google.protobuf.ByteString;
import com.sun.source.tree.ExpressionTree;
Expand All @@ -47,6 +46,7 @@
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID;
import java.util.function.Consumer;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
Expand Down Expand Up @@ -88,9 +88,9 @@ public class AlwaysThrows extends BugChecker implements MethodInvocationTreeMatc
.namedAnyOf("put")
.withParameters("java.lang.Object", "java.lang.Object");

enum Apis {
enum Api {
PARSE_TIME(
MethodMatchers.staticMethod()
staticMethod()
.onClassAny(VALIDATORS.keySet())
.named("parse")
.withParameters("java.lang.CharSequence")) {
Expand All @@ -101,7 +101,7 @@ void validate(MethodInvocationTree tree, String argument) {
}
},
BYTE_STRING(
MethodMatchers.staticMethod()
staticMethod()
.onClass("com.google.protobuf.ByteString")
.named("fromHex")
.withParameters("java.lang.String")) {
Expand All @@ -115,9 +115,15 @@ void validate(MethodInvocationTree tree, String argument) {
throw Throwables.getCauseAs(e.getCause(), NumberFormatException.class);
}
}
},
UUID_PARSE(staticMethod().onClass("java.util.UUID").named("fromString")) {
@Override
void validate(MethodInvocationTree tree, String argument) {
var unused = UUID.fromString(argument);
}
};

Apis(Matcher<ExpressionTree> matcher) {
Api(Matcher<ExpressionTree> matcher) {
this.matcher = matcher;
}

Expand All @@ -128,9 +134,11 @@ void validate(MethodInvocationTree tree, String argument) {
}

private final ConstantExpressions constantExpressions;
private final boolean matchUuidParse;

public AlwaysThrows(ErrorProneFlags flags) {
this.constantExpressions = ConstantExpressions.fromFlags(flags);
this.matchUuidParse = flags.getBoolean("AlwaysThrows:MatchUuidParse").orElse(true);
}

@Override
Expand Down Expand Up @@ -161,8 +169,12 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return checkImmutableMapOf(tree, /* index= */ 1, state);
}
}
Apis api =
stream(Apis.values()).filter(m -> m.matcher.matches(tree, state)).findAny().orElse(null);
Api api =
stream(Api.values())
.filter(a -> matchUuidParse || !a.equals(Api.UUID_PARSE))
.filter(m -> m.matcher.matches(tree, state))
.findAny()
.orElse(null);
if (api == null) {
return NO_MATCH;
}
Expand Down
Expand Up @@ -169,4 +169,17 @@ public void immutableMapWithComplexKeys() {
"}")
.doTest();
}

@Test
public void uuidFromString() {
testHelper
.addSourceLines(
"Test.java",
"import java.util.UUID;",
"class Test {",
" // BUG: Diagnostic contains:",
" private final UUID uuid = UUID.fromString(\"foo\");",
"}")
.doTest();
}
}

0 comments on commit e81b5df

Please sign in to comment.