diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/skylark/java/com/google/devtools/skylark/skylint/ControlFlowChecker.java | 9 | ||||
-rw-r--r-- | src/tools/skylark/javatests/com/google/devtools/skylark/skylint/ControlFlowCheckerTest.java | 13 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/tools/skylark/java/com/google/devtools/skylark/skylint/ControlFlowChecker.java b/src/tools/skylark/java/com/google/devtools/skylark/skylint/ControlFlowChecker.java index e11c3d2a5f..821749ca3d 100644 --- a/src/tools/skylark/java/com/google/devtools/skylark/skylint/ControlFlowChecker.java +++ b/src/tools/skylark/java/com/google/devtools/skylark/skylint/ControlFlowChecker.java @@ -18,6 +18,7 @@ import com.google.common.base.Preconditions; import com.google.devtools.build.lib.syntax.BuildFileAST; import com.google.devtools.build.lib.syntax.Expression; import com.google.devtools.build.lib.syntax.ExpressionStatement; +import com.google.devtools.build.lib.syntax.ForStatement; import com.google.devtools.build.lib.syntax.FuncallExpression; import com.google.devtools.build.lib.syntax.FunctionDefStatement; import com.google.devtools.build.lib.syntax.Identifier; @@ -27,6 +28,7 @@ import com.google.devtools.build.lib.syntax.ReturnStatement; import com.google.devtools.build.lib.syntax.Statement; import com.google.devtools.build.lib.syntax.SyntaxTreeVisitor; import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.stream.Stream; @@ -83,6 +85,13 @@ public class ControlFlowChecker extends SyntaxTreeVisitor { } @Override + public void visit(ForStatement node) { + ControlFlowInfo noIteration = ControlFlowInfo.copy(cfi); + super.visit(node); + cfi = ControlFlowInfo.join(Arrays.asList(noIteration, cfi)); + } + + @Override public void visit(ReturnStatement node) { // Should be rejected by parser, but we may have been fed a bad AST. Preconditions.checkState(cfi != null, "AST has illegal top-level return statement"); diff --git a/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/ControlFlowCheckerTest.java b/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/ControlFlowCheckerTest.java index 0b00745ff2..6266f6f9b9 100644 --- a/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/ControlFlowCheckerTest.java +++ b/src/tools/skylark/javatests/com/google/devtools/skylark/skylint/ControlFlowCheckerTest.java @@ -127,6 +127,19 @@ public class ControlFlowCheckerTest { } @Test + public void testForAndFallOffEnd() throws Exception { + Truth.assertThat( + findIssues( + "def some_function():", + " for x in []:", + " return x", + " print('foo')", + " # return missing here") + .toString()) + .contains("some but not all execution paths of 'some_function' return a value"); + } + + @Test public void testAlwaysReturnButSometimesWithoutValue() throws Exception { String messages = findIssues( |