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 @@ -75,6 +75,12 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
if (!result.isPresent()) {
return Description.NO_MATCH;
}
// Avoid rewriting code that removes comments.
if (ASTHelpers.containsComments(tree, state)) {
return buildDescription(tree)
.setMessage(MESSAGE)
.build();
}
List<ExpressionTree> arguments = result.get();
Stream<String> prefixStream = arguments.stream().findFirst()
.map(ASTHelpers::getType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,49 @@ public void shouldWarnOnNoParams() {
).doTest();
}

@Test
public void shouldWarnWhenCommentsArePresent() {
compilationHelper.addSourceLines(
"Test.java",
"class Test {",
" String f() {",
" return new StringBuilder()",
" .append(\"foo\") // comment",
" .append(\"bar\")",
" // BUG: Diagnostic contains: StringBuilder with a constant number of parameters",
" .toString();",
" }",
"}"
).doTest();
}

@Test
public void doesNotRemoveComments() {
BugCheckerRefactoringTestHelper.newInstance(new StringBuilderConstantParameters(), getClass())
.addInputLines(
"Test.java",
"class Test {",
" String f() {",
// Fails validation, but the tool prefers not to remove existing comments
" return new StringBuilder()",
" .append(\"foo\") // comment",
" .append(\"bar\")",
" .toString();",
" }",
"}")
.addOutputLines(
"Test.java",
"class Test {",
" String f() {",
" return new StringBuilder()",
" .append(\"foo\") // comment",
" .append(\"bar\")",
" .toString();",
" }",
"}")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

@Test
public void shouldWarnOnNoParams_fix() {
BugCheckerRefactoringTestHelper.newInstance(new StringBuilderConstantParameters(), getClass())
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-877.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: StringBuilderConstantParameters suggested fix doesn't remove comments
links:
- https://github.com/palantir/gradle-baseline/pull/877