Skip to content

Commit

Permalink
NearbyCallers: scan the body of expression lambdas.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 624922054
  • Loading branch information
graememorgan authored and Error Prone Team committed Apr 15, 2024
1 parent 53d787c commit 5a7b8d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ private static ImmutableList<Tree> getNearbyTreesToScan(VisitorState state) {
return ImmutableList.of(parent);

case LAMBDA_EXPRESSION:
// if we reach a lambda tree, then don't scan anything since we don't know where/when that
// lambda will actually be executed.
// if we reach a lambda tree, just scan the lambda body itself
// TODO(glorioso): for simple expression lambdas, consider looking for use sites and scan
// *those* sites, but binding the lambda variable to its use site might be rough :(

Expand All @@ -179,7 +178,7 @@ private static ImmutableList<Tree> getNearbyTreesToScan(VisitorState state) {
// long nanos = NANOS.apply(myDuration) + SECONDS.apply(myDuration) * 1_000_000L;
//
// how do we track myDuration through both layers?
return ImmutableList.of();
return ImmutableList.of(((LambdaExpressionTree) parent).getBody());

case CLASS:
// if we get all the way up to the class tree, then _only_ scan the other class-level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,22 @@ public void getNanoInLambda() {
"}")
.doTest();
}

@Test
public void bothUsedWithinALambda() {
compilationHelper
.addSourceLines(
"test/TestCase.java",
"package test;",
"import com.google.common.collect.ImmutableMap;",
"import java.time.Duration;",
"import java.util.function.Supplier;",
"public class TestCase {",
" public static Supplier<ImmutableMap<String, Object>> foo(Duration duration) {",
" return () -> ImmutableMap.of(",
" \"seconds\", duration.getSeconds(), \"nanos\", duration.getNano());",
" }",
"}")
.doTest();
}
}

0 comments on commit 5a7b8d9

Please sign in to comment.