|
16 | 16 |
|
17 | 17 | package com.google.errorprone.bugpatterns; |
18 | 18 |
|
19 | | -import static com.google.common.collect.Iterables.getOnlyElement; |
20 | 19 | import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; |
21 | 20 | import static com.google.errorprone.matchers.Description.NO_MATCH; |
22 | 21 |
|
23 | 22 | import com.google.errorprone.BugPattern; |
| 23 | +import com.google.errorprone.ErrorProneFlags; |
24 | 24 | import com.google.errorprone.VisitorState; |
25 | 25 | import com.google.errorprone.matchers.Description; |
26 | 26 | import com.google.errorprone.util.ASTHelpers; |
|
37 | 37 | import com.sun.source.tree.TypeCastTree; |
38 | 38 | import com.sun.source.util.SimpleTreeVisitor; |
39 | 39 | import com.sun.tools.javac.code.Symbol.MethodSymbol; |
| 40 | +import javax.inject.Inject; |
40 | 41 | import org.checkerframework.checker.nullness.qual.Nullable; |
41 | 42 |
|
42 | 43 | /** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ |
43 | 44 | @BugPattern( |
44 | 45 | summary = "This method always recurses, and will cause a StackOverflowError", |
45 | 46 | severity = ERROR) |
46 | 47 | public class InfiniteRecursion extends BugChecker implements BugChecker.MethodTreeMatcher { |
| 48 | + private final boolean matchFirstOfMultipleStatements; |
| 49 | + |
| 50 | + @Inject |
| 51 | + public InfiniteRecursion(ErrorProneFlags flags) { |
| 52 | + // TODO(b/264529494): Remove flag. |
| 53 | + matchFirstOfMultipleStatements = |
| 54 | + flags.getBoolean("InfiniteRecursion:MatchFirstOfMultipleStatements").orElse(true); |
| 55 | + } |
| 56 | + |
47 | 57 | @Override |
48 | 58 | public Description matchMethod(MethodTree tree, VisitorState state) { |
49 | | - if (tree.getBody() == null || tree.getBody().getStatements().size() != 1) { |
| 59 | + if (tree.getBody() == null || tree.getBody().getStatements().isEmpty()) { |
| 60 | + return NO_MATCH; |
| 61 | + } |
| 62 | + if (!matchFirstOfMultipleStatements && tree.getBody().getStatements().size() > 1) { |
50 | 63 | return NO_MATCH; |
51 | 64 | } |
52 | | - Tree statement = getOnlyElement(tree.getBody().getStatements()); |
| 65 | + /* |
| 66 | + * TODO(b/264529494): Match statements after the first as long as they're executed |
| 67 | + * unconditionally. And match more than just `return` and method-invocation expression |
| 68 | + * statements, too. |
| 69 | + */ |
| 70 | + Tree statement = tree.getBody().getStatements().get(0); |
53 | 71 | MethodInvocationTree invocation = |
54 | 72 | statement.accept( |
55 | 73 | new SimpleTreeVisitor<MethodInvocationTree, Void>() { |
|
0 commit comments