diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ErrorProneTestHelperSourceFormat.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ErrorProneTestHelperSourceFormat.java index bfc0663aca0..85181e86a1a 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ErrorProneTestHelperSourceFormat.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ErrorProneTestHelperSourceFormat.java @@ -64,6 +64,8 @@ public final class ErrorProneTestHelperSourceFormat extends BugChecker private static final long serialVersionUID = 1L; private static final String FLAG_AVOID_TEXT_BLOCKS = "ErrorProneTestHelperSourceFormat:AvoidTextBlocks"; + private static final String FLAG_IGNORE_MALFORMED_CODE = + "ErrorProneTestHelperSourceFormat:IgnoreMalformedCode"; private static final Formatter FORMATTER = new Formatter(); private static final Matcher INPUT_SOURCE_ACCEPTING_METHOD = anyOf( @@ -82,6 +84,7 @@ public final class ErrorProneTestHelperSourceFormat extends BugChecker private static final String DEFAULT_TEXT_BLOCK_INDENTATION = " ".repeat(12); private final boolean avoidTextBlocks; + private final boolean ignoreMalformedCode; /** Instantiates the default {@link ErrorProneTestHelperSourceFormat}. */ public ErrorProneTestHelperSourceFormat() { @@ -95,6 +98,7 @@ public ErrorProneTestHelperSourceFormat() { */ public ErrorProneTestHelperSourceFormat(ErrorProneFlags flags) { avoidTextBlocks = flags.getBoolean(FLAG_AVOID_TEXT_BLOCKS).orElse(Boolean.FALSE); + ignoreMalformedCode = flags.getBoolean(FLAG_IGNORE_MALFORMED_CODE).orElse(Boolean.FALSE); } @Override @@ -128,9 +132,11 @@ private Description flagFormattingIssues( String gjfResult = formatSourceCode(source, retainUnusedImports); formatted = canUseTextBlocks(state) ? gjfResult : gjfResult.stripTrailing(); } catch (FormatterException e) { - return buildDescription(methodInvocation) - .setMessage(String.format("Source code is malformed: %s", e.getMessage())) - .build(); + return ignoreMalformedCode + ? Description.NO_MATCH + : buildDescription(methodInvocation) + .setMessage(String.format("Source code is malformed: %s", e.getMessage())) + .build(); } boolean isFormatted = source.equals(formatted); diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/ErrorProneTestHelperSourceFormatTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/ErrorProneTestHelperSourceFormatTest.java index de7716e7193..c3f89191a1a 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/ErrorProneTestHelperSourceFormatTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/ErrorProneTestHelperSourceFormatTest.java @@ -8,6 +8,7 @@ import org.junit.jupiter.api.condition.JRE; final class ErrorProneTestHelperSourceFormatTest { + // XXX: Add tests cases for `ErrorProneTestHelperSourceFormat:IgnoreMalformedCode`. private final CompilationTestHelper compilationTestHelper = CompilationTestHelper.newInstance(ErrorProneTestHelperSourceFormat.class, getClass()); private final CompilationTestHelper avoidTextBlocksCompilationTestHelper =