From 886ed1ccf38715dbae587ce6d6e736809bf73d4f Mon Sep 17 00:00:00 2001 From: David Schlosnagle Date: Fri, 8 Jan 2021 12:16:36 -0800 Subject: [PATCH] Handle diagnostics without source In some cases, compiler diagnostics may not have an associated source, and for testing whether suggested fixes compile these should be treated as originating from the same compilation unit of the suggested fix. Fixes #1873 Fixes #2064 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/error-prone/pull/2064 from schlosna:ds/1873-SuggestedFixes-NPE ddd042bafb598c84561cd27dddc9a25eef5c7051 PiperOrigin-RevId: 350812718 --- .../main/java/com/google/errorprone/fixes/SuggestedFixes.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java b/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java index 439b60676f6..5626d92f8f8 100644 --- a/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java +++ b/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java @@ -1309,8 +1309,10 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept boolean warningInSameCompilationUnit = false; for (Diagnostic diagnostic : diagnosticListener.getDiagnostics()) { warningIsError |= diagnostic.getCode().equals("compiler.err.warnings.and.werror"); + JavaFileObject diagnosticSource = diagnostic.getSource(); + // If the source's origin is unknown, assume that new diagnostics are due to a modification. boolean diagnosticInSameCompilationUnit = - diagnostic.getSource().toUri().equals(modifiedFileUri); + diagnosticSource == null || diagnosticSource.toUri().equals(modifiedFileUri); switch (diagnostic.getKind()) { case ERROR: ++countErrors;