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: 601777043
  • Loading branch information
graememorgan authored and Error Prone Team committed Jan 26, 2024
1 parent 417b935 commit 7d1ae83
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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
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 7d1ae83

Please sign in to comment.