Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.getLast;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.errorprone.fixes.SuggestedFixes.renameVariableUsages;
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -112,6 +111,10 @@ public static Optional<Fix> tryFailToAssertThrows(
resources.stream().map(state::getSourceForNode).collect(joining("\n", "try (", ") {\n")));
fixSuffix = "\n}";
}
// Hoist all but the last statement out of the lambda to narrow the exception scope.
for (StatementTree statement : throwingStatements.subList(0, throwingStatements.size() - 1)) {
fixPrefix.append(state.getSourceForNode(statement)).append("\n");
}
if (!catchStatements.isEmpty()) {
String name = catchTree.getParameter().getName().toString();
String newName = namer.avoidShadowing(name);
Expand All @@ -133,30 +136,22 @@ public static Optional<Fix> tryFailToAssertThrows(
.map(t -> state.getSourceForNode(t) + ", ")
.orElse(""),
state.getSourceForNode(catchTree.getParameter().getType())));
boolean useExpressionLambda =
throwingStatements.size() == 1
&& getOnlyElement(throwingStatements) instanceof ExpressionStatementTree;
StatementTree lastStatement = getLast(throwingStatements);
boolean useExpressionLambda = lastStatement instanceof ExpressionStatementTree;
if (!useExpressionLambda) {
fixPrefix.append("{");
}
fix.replace(
getStartPosition(tryTree),
getStartPosition(throwingStatements.iterator().next()),
fixPrefix.toString());
fix.replace(getStartPosition(tryTree), getStartPosition(lastStatement), fixPrefix.toString());
if (useExpressionLambda) {
fix.postfixWith(
((ExpressionStatementTree) throwingStatements.iterator().next()).getExpression(), ")");
fix.postfixWith(((ExpressionStatementTree) lastStatement).getExpression(), ")");
} else {
fix.postfixWith(getLast(throwingStatements), "});");
fix.postfixWith(lastStatement, "});");
}
if (catchStatements.isEmpty()) {
fix.replace(
state.getEndPosition(getLast(throwingStatements)),
state.getEndPosition(tryTree),
fixSuffix);
fix.replace(state.getEndPosition(lastStatement), state.getEndPosition(tryTree), fixSuffix);
} else {
fix.replace(
/* startPos= */ state.getEndPosition(getLast(throwingStatements)),
/* startPos= */ state.getEndPosition(lastStatement),
/* endPos= */ getStartPosition(catchStatements.getFirst()),
"\n");
fix.replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,13 +948,8 @@ class ExceptionTest {
@Test
public void f() throws Exception {
Path p = Paths.get("NOSUCH");
IOException e =
assertThrows(
IOException.class,
() -> {
Files.readAllBytes(p);
Files.readAllBytes(p);
});
Files.readAllBytes(p);
IOException e = assertThrows(IOException.class, () -> Files.readAllBytes(p));
assertThat(e).hasMessageThat().contains("NOSUCH");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public void catchBlock() {

class ExceptionTest {
@Test
public void f(String message) throws Exception {
public void f(String msg) throws Exception {
Path p = Paths.get("NOSUCH");
try {
Files.readAllBytes(p);
Files.readAllBytes(p);
fail(message);
fail(msg);
} catch (IOException e) {
assertThat(e).hasMessageThat().contains("NOSUCH");
}
Expand Down Expand Up @@ -81,16 +81,10 @@ public void g() throws Exception {

class ExceptionTest {
@Test
public void f(String message) throws Exception {
public void f(String msg) throws Exception {
Path p = Paths.get("NOSUCH");
IOException e =
assertThrows(
message,
IOException.class,
() -> {
Files.readAllBytes(p);
Files.readAllBytes(p);
});
Files.readAllBytes(p);
IOException e = assertThrows(msg, IOException.class, () -> Files.readAllBytes(p));
assertThat(e).hasMessageThat().contains("NOSUCH");
}

Expand Down Expand Up @@ -169,11 +163,11 @@ public void tryWithResources() {

class ExceptionTest {
@Test
public void f(String message, CharSource cs) throws IOException {
public void f(String msg, CharSource cs) throws IOException {
try (BufferedReader buf = cs.openBufferedStream();
PushbackReader pbr = new PushbackReader(buf)) {
pbr.read();
fail(message);
fail(msg);
} catch (IOException e) {
assertThat(e).hasMessageThat().contains("NOSUCH");
}
Expand All @@ -195,10 +189,10 @@ public void f(String message, CharSource cs) throws IOException {

class ExceptionTest {
@Test
public void f(String message, CharSource cs) throws IOException {
public void f(String msg, CharSource cs) throws IOException {
try (BufferedReader buf = cs.openBufferedStream();
PushbackReader pbr = new PushbackReader(buf)) {
IOException e = assertThrows(message, IOException.class, () -> pbr.read());
IOException e = assertThrows(msg, IOException.class, () -> pbr.read());
assertThat(e).hasMessageThat().contains("NOSUCH");
}
}
Expand Down
Loading