Skip to content

Commit

Permalink
Fix a crash in TimeUnitConversionChecker
Browse files Browse the repository at this point in the history
Motivated by

```
thread.join((timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS).toMillis(time));
```

https://github.com/apache/httpcomponents-client/blame/160bac0cbb2b388018c4e794c120ba719ddd2887/httpclient5/src/main/java/org/apache/hc/client5/http/impl/IdleConnectionEvictor.java#L98

PiperOrigin-RevId: 566977948
  • Loading branch information
cushon authored and Error Prone Team committed Sep 20, 2023
1 parent ac424d0 commit a4ab5a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol;
import java.util.concurrent.TimeUnit;

/** Check for problematic or suspicious TimeUnit conversion calls. */
Expand Down Expand Up @@ -73,7 +74,11 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
// TimeUnit SECONDS = TimeUnit.MINUTES;
// long about2500 = SECONDS.toSeconds(42);
// but... I think that's bad enough to ignore here :)
String timeUnitName = ASTHelpers.getSymbol(receiverOfConversion).getSimpleName().toString();
Symbol receiverOfConversionSymbol = ASTHelpers.getSymbol(receiverOfConversion);
if (receiverOfConversionSymbol == null) {
return Description.NO_MATCH;
}
String timeUnitName = receiverOfConversionSymbol.getSimpleName().toString();
Optional<TimeUnit> receiver = Enums.getIfPresent(TimeUnit.class, timeUnitName);
if (!receiver.isPresent()) {
return Description.NO_MATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,18 @@ public void largeUnitToSmallUnit_succeeds() {
"}")
.doTest();
}

@Test
public void receiverExpression() {
helper
.addSourceLines(
"TestClass.java",
"import java.util.concurrent.TimeUnit;",
"public class TestClass {",
" long f(TimeUnit timeUnit, long time) {",
" return (timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS).toMillis(time);",
" }",
"}")
.doTest();
}
}

0 comments on commit a4ab5a7

Please sign in to comment.