From d0502e88db741a52c9ab7f5d16a3722ea5498476 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sun, 22 Dec 2024 13:09:01 -0500 Subject: [PATCH] Fix issue with annotations in `module-info.java` files (#1109) --- .../test/java/com/uber/nullaway/jdk17/ModuleInfoTests.java | 1 + nullaway/src/main/java/com/uber/nullaway/NullAway.java | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/ModuleInfoTests.java b/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/ModuleInfoTests.java index a8dcbc9449..7fd1d84cf6 100644 --- a/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/ModuleInfoTests.java +++ b/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/ModuleInfoTests.java @@ -35,6 +35,7 @@ public void testModuleInfo() { defaultCompilationHelper .addSourceLines( "module-info.java", + "@SuppressWarnings(\"some-warning-name\")", "module com.uber.mymodule {", " // Important: two-level deep module tests matching of identifier `java` as base expression;", " // see further discussion at https://github.com/uber/NullAway/pull/544#discussion_r780829467", diff --git a/nullaway/src/main/java/com/uber/nullaway/NullAway.java b/nullaway/src/main/java/com/uber/nullaway/NullAway.java index 9e27d42c0a..d381e11909 100644 --- a/nullaway/src/main/java/com/uber/nullaway/NullAway.java +++ b/nullaway/src/main/java/com/uber/nullaway/NullAway.java @@ -486,6 +486,12 @@ public Description matchAssignment(AssignmentTree tree, VisitorState state) { doUnboxingCheck(state, tree.getExpression()); } Symbol assigned = ASTHelpers.getSymbol(tree.getVariable()); + if (assigned instanceof Symbol.MethodSymbol) { + // javac generates an AssignmentTree for setting an annotation attribute value. E.g., for + // `@SuppressWarnings("foo")`, javac generates an AssignmentTree of the form `value() = + // "foo"`, where the LHS is a MethodSymbol. We don't want to analyze these. + return Description.NO_MATCH; + } if (assigned != null && codeAnnotationInfo.isSymbolUnannotated(assigned, config, handler)) { // assigning to symbol that is unannotated return Description.NO_MATCH;