From 7be2c43796341d9998bb79883d7f2a455d5a1631 Mon Sep 17 00:00:00 2001 From: bhagwani Date: Fri, 15 May 2020 10:46:49 -0700 Subject: [PATCH] Don't flag module-info.java for not having default package RELNOTES: Don't flag module-info.java for not having default package ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=311760553 --- .../errorprone/bugpatterns/DefaultPackage.java | 5 +++++ .../errorprone/bugpatterns/DefaultPackageTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/DefaultPackage.java b/core/src/main/java/com/google/errorprone/bugpatterns/DefaultPackage.java index b66df303ce3..051f4ee290f 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/DefaultPackage.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/DefaultPackage.java @@ -18,6 +18,7 @@ import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; +import com.google.common.io.Files; import com.google.errorprone.BugPattern; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher; @@ -53,6 +54,10 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s if (tree.getPackageName() != null) { return Description.NO_MATCH; } + // module-info.* is a special file name so whitelisting it. + if (Files.getNameWithoutExtension(ASTHelpers.getFileName(tree)).equals("module-info")) { + return Description.NO_MATCH; + } return describeMatch(tree); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/DefaultPackageTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/DefaultPackageTest.java index 50cc81b6fc4..e3b078faed9 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/DefaultPackageTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/DefaultPackageTest.java @@ -16,6 +16,8 @@ package com.google.errorprone.bugpatterns; +import static org.junit.Assume.assumeTrue; + import com.google.errorprone.CompilationTestHelper; import com.google.errorprone.util.RuntimeVersion; import org.junit.Test; @@ -89,4 +91,16 @@ public void trailingComma() { "class T {};") .doTest(); } + + @Test + public void moduleInfo() { + assumeTrue(RuntimeVersion.isAtLeast9()); + + compilationHelper + .addSourceLines( + "module-info.java", // + "module testmodule {", + "}") + .doTest(); + } }