Skip to content

Commit

Permalink
Recognize conditional expressions in FilesLinesLeak
Browse files Browse the repository at this point in the history
MOE_MIGRATED_REVID=140898154
  • Loading branch information
cushon committed Dec 7, 2016
1 parent 8bcbdef commit bf5e9ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Expand Up @@ -34,6 +34,7 @@
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import javax.lang.model.element.ElementKind;
Expand Down Expand Up @@ -90,7 +91,11 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
}

private boolean inTWR(VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(state.getPath().getParentPath().getLeaf());
TreePath path = state.getPath().getParentPath();
while (path.getLeaf().getKind() == Tree.Kind.CONDITIONAL_EXPRESSION) {
path = path.getParentPath();
}
Symbol sym = ASTHelpers.getSymbol(path.getLeaf());
return sym != null && sym.getKind() == ElementKind.RESOURCE_VARIABLE;
}
}
Expand Up @@ -130,4 +130,24 @@ public void fixVariable() throws IOException {
"}")
.doTest();
}

@Test
public void ternary() {
testHelper
.addSourceLines(
"Test.java",
"import java.io.IOException;",
"import java.nio.file.Files;",
"import java.nio.file.Path;",
"import java.util.stream.Collectors;",
"import java.util.stream.Stream;",
"class Test {",
" String f(Path p) throws IOException {",
" try (Stream<String> stream = true ? Files.lines(p) : null) {",
" return stream.collect(Collectors.joining(\", \"));",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit bf5e9ab

Please sign in to comment.