Turn many semantic errors into warnings

This commit is contained in:
Clément Fournier
2022-05-15 14:09:58 +02:00
parent fd6f70545f
commit f291a2917b
6 changed files with 19 additions and 9 deletions

View File

@ -34,7 +34,9 @@ public interface SemanticErrorReporter {
/**
* Report a warning at the given location. Warnings do not abort
* the analysis.
* the analysis. They are usually recoverable errors. They are used
* to warn the user that something wrong is going on, which may cause
* subsequent errors or inconsistent behavior.
*
* @param location Location where the warning should be reported
* @param message Message (rendered using a {@link MessageFormat})
@ -44,9 +46,9 @@ public interface SemanticErrorReporter {
/**
* Report an error at the given location. Errors abort subsequent analysis.
* The produced error can be thrown by the caller if it cannot be recovered
* from.
* Report an error at the given location. Errors abort subsequent analysis
* and cause a processing error to be put in the report. The produced error
* can be thrown by the caller if it cannot be recovered from.
*
* @param location Location where the error should be reported
* @param message Message (rendered using a {@link MessageFormat})

View File

@ -4,12 +4,15 @@
package net.sourceforge.pmd.lang.ast;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.contains;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
@ -36,6 +39,7 @@ public class SemanticErrorReporterTest {
@Before
public void setup() {
mockReporter = mock(MessageReporter.class);
when(mockReporter.isLoggable(Level.ERROR)).thenReturn(true);
mockLogger = spy(NOPLogger.class);
}
@ -44,11 +48,15 @@ public class SemanticErrorReporterTest {
SemanticErrorReporter reporter = SemanticErrorReporter.reportToLogger(mockReporter, mockLogger);
RootNode node = parseMockNode(reporter);
assertFalse(reporter.hasError());
String message = "an error occurred";
reporter.error(node, message);
verify(mockReporter).log(eq(Level.ERROR), contains(message));
verifyNoMoreInteractions(mockLogger);
assertTrue(reporter.hasError());
}
@Test

View File

@ -211,7 +211,7 @@ final class AstDisambiguationPass {
JTypeDeclSymbol sym = type.getReferencedSym();
if (type.getParent() instanceof ASTAnnotation) {
if (!(sym instanceof JClassSymbol && (sym.isUnresolved() || ((JClassSymbol) sym).isAnnotation()))) {
ctx.getLogger().error(type, JavaSemanticErrors.EXPECTED_ANNOTATION_TYPE);
ctx.getLogger().warning(type, JavaSemanticErrors.EXPECTED_ANNOTATION_TYPE);
}
return;
}
@ -219,7 +219,7 @@ final class AstDisambiguationPass {
int actualArity = ASTList.sizeOrZero(type.getTypeArguments());
int expectedArity = sym instanceof JClassSymbol ? ((JClassSymbol) sym).getTypeParameterCount() : 0;
if (actualArity != 0 && actualArity != expectedArity) {
ctx.getLogger().error(type, JavaSemanticErrors.MALFORMED_GENERIC_TYPE, expectedArity, actualArity);
ctx.getLogger().warning(type, JavaSemanticErrors.MALFORMED_GENERIC_TYPE, expectedArity, actualArity);
}
}

View File

@ -98,7 +98,7 @@ public final class InternalApiBridge {
try {
it.getTypeMirror();
} catch (Exception e) {
processor.getLogger().error(it, "Error during type resolution of node " + it.getXPathNodeName());
processor.getLogger().warning(it, "Error during type resolution of node " + it.getXPathNodeName());
}
});
}

View File

@ -90,7 +90,7 @@ public final class ReferenceCtx {
if (distinct.size() == 1) {
return distinct.iterator().next();
}
processor.getLogger().error(
processor.getLogger().warning(
errorLocation,
AMBIGUOUS_NAME_REFERENCE,
name,

View File

@ -564,7 +564,7 @@ public final class LazyTypeResolver extends JavaVisitorBase<TypingContext, @NonN
JMethodSig m = lambda.getFunctionalMethod(); // this forces resolution of the lambda
if (!isUnresolved(m)) {
if (m.getArity() != node.getOwner().getArity()) {
err.error(node.getOwner(), "Lambda shape does not conform to the functional method {0}", m);
err.warning(node.getOwner(), "Lambda shape does not conform to the functional method {0}", m);
return ts.ERROR;
}
return m.getFormalParameters().get(node.getIndexInParent());