Skip to content

Commit

Permalink
Support analyzing dead code after throw statement
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellansun committed Jan 1, 2024
1 parent 23af892 commit 9826eea
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.codehaus.groovy.ast.stmt.ContinueStatement;
import org.codehaus.groovy.ast.stmt.ReturnStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.ast.stmt.ThrowStatement;
import org.codehaus.groovy.control.SourceUnit;

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ private void analyzeDeadCode(BlockStatement block) {
if (statement instanceof ReturnStatement
|| statement instanceof BreakStatement
|| statement instanceof ContinueStatement
) {
|| statement instanceof ThrowStatement) {
foundCnt++;
if (1 == foundCnt) continue;
}
Expand Down
17 changes: 10 additions & 7 deletions src/test/groovy/ThrowTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import groovy.test.GroovyTestCase

class ThrowTest extends GroovyTestCase {
void testThrow() {
try {
throw new Exception("abcd")
fail("Should have thrown an exception by now")
}
catch (Exception e) {
assert e.message == "abcd"
}
def err = shouldFail '''\
try {
throw new Exception("abcd")
assert false: "Should have thrown an exception by now"
}
catch (Exception e) {
assert e.message == "abcd"
}
'''
assert err ==~ /(?s)(.+)\s+Unreachable statement found\s+@ line 3, column 17\.\s+assert false: "Should have thrown an exception by now"\s+(.+)/
}
}
49 changes: 48 additions & 1 deletion src/test/groovy/bugs/Groovy11263.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package bugs

import org.junit.Test

import static groovy.test.GroovyAssert.assertScript
import static groovy.test.GroovyAssert.shouldFail

final class Groovy11263 {
Expand Down Expand Up @@ -352,4 +351,52 @@ final class Groovy11263 {

assert err ==~ /(?s)(.+)\s+Unreachable statement found\s+@ line 3, column 17\.\s+return\s+(.+)/
}

@Test
void testUnreachableStatementAfterThrow1() {
def err = shouldFail '''\
for (var i in [1, 2, 3]) {
throw new RuntimeException("just for test")
throw new RuntimeException("just for test.")
}
'''

assert err ==~ /(?s)(.+)\s+Unreachable statement found\s+@ line 3, column 17\.\s+throw new RuntimeException\("just for test\."\)\s+(.+)/
}

@Test
void testUnreachableStatementAfterThrow2() {
def err = shouldFail '''\
for (var i in [1, 2, 3]) {
throw new RuntimeException("just for test")
return
}
'''

assert err ==~ /(?s)(.+)\s+Unreachable statement found\s+@ line 3, column 17\.\s+return\s+(.+)/
}

@Test
void testUnreachableStatementAfterThrow3() {
def err = shouldFail '''\
for (var i in [1, 2, 3]) {
throw new RuntimeException("just for test")
break
}
'''

assert err ==~ /(?s)(.+)\s+Unreachable statement found\s+@ line 3, column 17\.\s+break\s+(.+)/
}

@Test
void testUnreachableStatementAfterThrow4() {
def err = shouldFail '''\
for (var i in [1, 2, 3]) {
throw new RuntimeException("just for test")
continue
}
'''

assert err ==~ /(?s)(.+)\s+Unreachable statement found\s+@ line 3, column 17\.\s+continue\s+(.+)/
}
}

0 comments on commit 9826eea

Please sign in to comment.