Skip to content

Commit

Permalink
Fix a crash in UnnecessaryStringBuilder
Browse files Browse the repository at this point in the history
Fixes external #4195

PiperOrigin-RevId: 601397633
  • Loading branch information
graememorgan authored and Error Prone Team committed Jan 26, 2024
1 parent 417b935 commit a62e58c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) {
}
}
ASTHelpers.TargetType target = ASTHelpers.targetType(state.withPath(path));
if (target == null) {
return NO_MATCH;
}
if (!isUsedAsStringBuilder(state, target)) {
return describeMatch(
path.getLeaf(), SuggestedFix.replace(path.getLeaf(), replacement(state, parts)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,17 @@ public void varType() {
"}")
.doTest();
}

@Test
public void unusedReturnValue() {
testHelper
.addSourceLines(
"Test.java",
"class Test {",
" void f() {",
" new StringBuilder().append(\"foo\");",
" }",
"}")
.doTest();
}
}

0 comments on commit a62e58c

Please sign in to comment.