diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index cd4016dfe8..d04e1b5ef5 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -181,6 +181,11 @@ mongodb-driver-core test + + org.projectlombok + lombok + test + org.reactivestreams reactive-streams diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/DirectReturn.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/DirectReturn.java index 212d0b1245..98799bec80 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/DirectReturn.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/DirectReturn.java @@ -12,6 +12,7 @@ import static com.google.errorprone.matchers.Matchers.staticMethod; import static com.google.errorprone.matchers.Matchers.toType; import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; +import static tech.picnic.errorprone.bugpatterns.util.MoreMatchers.HAS_LOMBOK_DATA; import com.google.auto.service.AutoService; import com.google.common.collect.Streams; @@ -25,6 +26,7 @@ import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.AssignmentTree; import com.sun.source.tree.BlockTree; +import com.sun.source.tree.ClassTree; import com.sun.source.tree.ExpressionStatementTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.IdentifierTree; @@ -67,7 +69,8 @@ public DirectReturn() {} @Override public Description matchBlock(BlockTree tree, VisitorState state) { List statements = tree.getStatements(); - if (statements.size() < 2) { + ClassTree enclosingClass = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class); + if (statements.size() < 2 || HAS_LOMBOK_DATA.matches(enclosingClass, state)) { return Description.NO_MATCH; } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java index eaa683c650..77bb015594 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java @@ -7,6 +7,7 @@ import com.google.errorprone.matchers.Matchers; import com.google.errorprone.suppliers.Supplier; import com.sun.source.tree.AnnotationTree; +import com.sun.source.tree.ClassTree; import com.sun.source.tree.Tree; import com.sun.tools.javac.code.Type; @@ -16,6 +17,9 @@ *

These methods are additions to the ones found in {@link Matchers}. */ public final class MoreMatchers { + /** Matches classes annotated with Lombok's `@Data` annotation. */ + public static final Matcher HAS_LOMBOK_DATA = Matchers.hasAnnotation("lombok.Data"); + private MoreMatchers() {} /** diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/DirectReturnTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/DirectReturnTest.java index 42e1889414..1d8c5fe8cc 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/DirectReturnTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/DirectReturnTest.java @@ -223,4 +223,19 @@ void replacement() { "}") .doTest(TestMode.TEXT_MATCH); } + + @Test + void ignoreClassesAnnotatedWithLombokData() { + CompilationTestHelper.newInstance(DirectReturn.class, getClass()) + .setArgs("-processor", "lombok.launch.AnnotationProcessorHider$AnnotationProcessor") + .addSourceLines( + "A.java", + "import lombok.Data;", + "", + "@Data", + "class A {", + " private String field;", + "}") + .doTest(); + } } diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java index faed5900f3..d3bd03b4d7 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java @@ -11,16 +11,40 @@ import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher; +import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Matcher; import com.google.errorprone.suppliers.Supplier; import com.sun.source.tree.AnnotationTree; +import com.sun.source.tree.ClassTree; import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.Tree; import org.junit.jupiter.api.Test; final class MoreMatchersTest { + @Test + void hasLombokDataAnnotation() { + CompilationTestHelper.newInstance(HasLombokDataTestChecker.class, getClass()) + .addSourceLines( + "A.java", + "import lombok.Data;", + "", + "@Data", + "// BUG: Diagnostic contains:", + "public class A {", + " private String field;", + "", + " static class B {}", + "", + " @Data", + " // BUG: Diagnostic contains:", + " static class C {}", + "}") + .addSourceLines("D.java", "public class D {}") + .doTest(); + } + @Test void hasMetaAnnotation() { CompilationTestHelper.newInstance(HasMetaAnnotationTestChecker.class, getClass()) @@ -104,6 +128,19 @@ void isSubTypeOfBoundTypeUnknown() { .doTest(); } + /** A {@link BugChecker} that delegates to {@link MoreMatchers#HAS_LOMBOK_DATA}. */ + @BugPattern(summary = "Interacts with `MoreMatchers` for testing purposes", severity = ERROR) + public static final class HasLombokDataTestChecker extends BugChecker + implements ClassTreeMatcher { + private static final long serialVersionUID = 1L; + private static final Matcher DELEGATE = MoreMatchers.HAS_LOMBOK_DATA; + + @Override + public Description matchClass(ClassTree tree, VisitorState state) { + return DELEGATE.matches(tree, state) ? describeMatch(tree) : Description.NO_MATCH; + } + } + /** A {@link BugChecker} that delegates to {@link MoreMatchers#hasMetaAnnotation(String)}. */ @BugPattern(summary = "Interacts with `MoreMatchers` for testing purposes", severity = ERROR) public static final class HasMetaAnnotationTestChecker extends BugChecker diff --git a/pom.xml b/pom.xml index f073c04cc1..232d622c4e 100644 --- a/pom.xml +++ b/pom.xml @@ -456,6 +456,11 @@ + + org.projectlombok + lombok + 1.18.28 + org.slf4j slf4j-api