diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java b/core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java index a929c256dbd..403771a5d31 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java @@ -75,7 +75,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState } catch (FormatterException exception) { return NO_MATCH; } - if (formattedSource.equals(sourceValue)) { + if (formattedSource.trim().equals(((String) sourceValue).trim())) { return NO_MATCH; } // This is a bit crude: but tokenize between the comma and the 2nd argument in order to work out diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java index fd0a702173f..e11c1542aae 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java @@ -61,6 +61,34 @@ void method() { .doTest(); } + @Test + public void onlyDiffersByFinalNewline_noFinding() { + assume().that(Runtime.version().feature()).isAtLeast(14); + + compilationHelper + .addSourceLines( + "Test.java", + """ + import com.google.errorprone.BugCheckerRefactoringTestHelper; + + class Test { + void method(BugCheckerRefactoringTestHelper h) { + h.addInputLines( + "Test.java", + \""" + package foo; + + class Test { + void method() { + int a = 1; + } + }\"""); + } + } + """) + .doTest(); + } + @Test public void misformatted_suggestsFix() { assume().that(Runtime.version().feature()).isAtLeast(14);