From 31d420b282603d92ad105defed2f47c57ffd1691 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 16 May 2023 14:24:04 -0700 Subject: [PATCH] Fix a bug in AlreadyChecked involving if statements where the clauses aren't block statements Fixes https://github.com/google/error-prone/issues/3914 PiperOrigin-RevId: 532573468 --- .../errorprone/bugpatterns/AlreadyChecked.java | 2 +- .../bugpatterns/AlreadyCheckedTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/AlreadyChecked.java b/core/src/main/java/com/google/errorprone/bugpatterns/AlreadyChecked.java index 1a7a73ed396..c547f950461 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/AlreadyChecked.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/AlreadyChecked.java @@ -115,7 +115,7 @@ public Void visitThrow(ThrowTree tree, Void unused) { private void handleMethodExitingStatement() { TreePath ifPath = getCurrentPath().getParentPath(); - Tree previous = null; + Tree previous = getCurrentPath().getLeaf(); while (ifPath != null && ifPath.getLeaf() instanceof BlockTree) { previous = ifPath.getLeaf(); ifPath = ifPath.getParentPath(); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/AlreadyCheckedTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/AlreadyCheckedTest.java index d0acacfe3f5..770f093263a 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/AlreadyCheckedTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/AlreadyCheckedTest.java @@ -498,4 +498,22 @@ public void equalsCalledTwiceOnMutableType_noFinding() { "}") .doTest(); } + + @Test + public void i3914() { + helper + .addSourceLines( + "Test.java", + "import java.util.List;", + "class Test {", + " void test(boolean a, boolean b) {", + " if (a && b)", + " return;", + " if (a) {", + " } else if (b) {", + " }", + " }", + "}") + .doTest(); + } }