Skip to content

Commit

Permalink
Handle var in MustBeClosedChecker
Browse files Browse the repository at this point in the history
Fixes #4164

PiperOrigin-RevId: 658802144
  • Loading branch information
cushon authored and Error Prone Team committed Aug 2, 2024
1 parent ccd3ca6 commit ac7ebf5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.errorprone.bugpatterns;

import static com.google.errorprone.fixes.SuggestedFixes.prettyType;
import static com.google.errorprone.fixes.SuggestedFixes.qualifyType;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
Expand All @@ -28,6 +29,7 @@
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
import static com.google.errorprone.util.ASTHelpers.hasExplicitSource;
import static com.google.errorprone.util.ASTHelpers.isConsideredFinal;
import static com.google.errorprone.util.ASTHelpers.isSameType;
import static com.google.errorprone.util.ASTHelpers.isSubtype;
Expand Down Expand Up @@ -467,16 +469,29 @@ private static Optional<Change> extractToResourceInCurrentTry(
private static Optional<Change> splitVariableDeclarationAroundTry(
ExpressionTree tree, VariableTree var, VisitorState state, NameSuggester suggester) {
int initPos = getStartPosition(var.getInitializer());
int afterTypePos = state.getEndPosition(var.getType());
Tree type = var.getType();
String typePrefix;
int startPos;
if (hasExplicitSource(type, state)) {
startPos = state.getEndPosition(type);
typePrefix = "";
} else {
startPos = getStartPosition(var);
typePrefix = prettyType(getType(type), state);
}
String name = suggester.suggestName(tree);
return Change.builder(
SuggestedFix.builder()
.replace(
afterTypePos,
startPos,
initPos,
String.format(
" %s;\ntry (var %s = %s) {\n%s =",
var.getName(), name, state.getSourceForNode(tree), var.getName()))
"%s %s;\ntry (var %s = %s) {\n%s =",
typePrefix,
var.getName(),
name,
state.getSourceForNode(tree),
var.getName()))
.replace(tree, name)
.build())
.closeBraceAfter(var)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,47 @@ public void forLoopUnfixable() {
.expectUnchanged()
.doTest();
}

@Test
public void localVariableTypeInference() {
refactoringHelper
.addInputLines(
"Closeable.java",
"class Closeable implements AutoCloseable {",
" @Override",
" public void close() {}",
" public int method() {",
" return 1;",
" }",
" }")
.expectUnchanged()
.addInputLines(
"Foo.java",
"import com.google.errorprone.annotations.MustBeClosed;",
"class Foo {",
" @MustBeClosed",
" Closeable mustBeClosedMethod() {",
" return null;",
" }",
"}")
.expectUnchanged()
.addInputLines(
"Test.java",
"class Test {",
" void test(Foo foo) {",
" var bar = foo.mustBeClosedMethod().method();",
" }",
"}")
.addOutputLines(
"Test.java",
"class Test {",
" void test(Foo foo) {",
" int bar;",
" try (var closeable = foo.mustBeClosedMethod()) {",
" bar = closeable.method();",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit ac7ebf5

Please sign in to comment.