diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/FluxFlatMapUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/FluxFlatMapUsage.java index 4485527e17..16e55d4cdd 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/FluxFlatMapUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/FluxFlatMapUsage.java @@ -82,10 +82,8 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState SuggestedFix serializationFix = SuggestedFixes.renameMethodInvocation(tree, "concatMap", state); SuggestedFix concurrencyCapFix = - SuggestedFix.builder() - .postfixWith( - Iterables.getOnlyElement(tree.getArguments()), ", " + MAX_CONCURRENCY_ARG_NAME) - .build(); + SuggestedFix.postfixWith( + Iterables.getOnlyElement(tree.getArguments()), ", " + MAX_CONCURRENCY_ARG_NAME); Description.Builder description = buildDescription(tree); diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/SuggestedFixRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/SuggestedFixRules.java new file mode 100644 index 0000000000..801417ee5a --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/SuggestedFixRules.java @@ -0,0 +1,106 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.errorprone.fixes.SuggestedFix; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.sun.source.tree.Tree; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +/** Refaster rules related to expressions dealing with {@link SuggestedFix}es. */ +@OnlineDocumentation +final class SuggestedFixRules { + private SuggestedFixRules() {} + + /** Prefer {@link SuggestedFix#delete(Tree)} over more contrived alternatives. */ + static final class SuggestedFixDelete { + @BeforeTemplate + SuggestedFix before(Tree tree) { + return SuggestedFix.builder().delete(tree).build(); + } + + @AfterTemplate + SuggestedFix after(Tree tree) { + return SuggestedFix.delete(tree); + } + } + + /** Prefer {@link SuggestedFix#replace(Tree, String)}} over more contrived alternatives. */ + static final class SuggestedFixReplaceTree { + @BeforeTemplate + SuggestedFix before(Tree tree, String replaceWith) { + return SuggestedFix.builder().replace(tree, replaceWith).build(); + } + + @AfterTemplate + SuggestedFix after(Tree tree, String replaceWith) { + return SuggestedFix.replace(tree, replaceWith); + } + } + + /** Prefer {@link SuggestedFix#replace(int, int, String)}} over more contrived alternatives. */ + static final class SuggestedFixReplaceStartEnd { + @BeforeTemplate + SuggestedFix before(int start, int end, String replaceWith) { + return SuggestedFix.builder().replace(start, end, replaceWith).build(); + } + + @AfterTemplate + SuggestedFix after(int start, int end, String replaceWith) { + return SuggestedFix.replace(start, end, replaceWith); + } + } + + /** + * Prefer {@link SuggestedFix#replace(Tree, String, int, int)}} over more contrived alternatives. + */ + static final class SuggestedFixReplaceTreeStartEnd { + @BeforeTemplate + SuggestedFix before(Tree tree, String replaceWith, int start, int end) { + return SuggestedFix.builder().replace(tree, replaceWith, start, end).build(); + } + + @AfterTemplate + SuggestedFix after(Tree tree, String replaceWith, int start, int end) { + return SuggestedFix.replace(tree, replaceWith, start, end); + } + } + + /** Prefer {@link SuggestedFix#swap(Tree, Tree)} over more contrived alternatives. */ + static final class SuggestedFixSwap { + @BeforeTemplate + SuggestedFix before(Tree tree1, Tree tree2) { + return SuggestedFix.builder().swap(tree1, tree2).build(); + } + + @AfterTemplate + SuggestedFix after(Tree tree1, Tree tree2) { + return SuggestedFix.swap(tree1, tree2); + } + } + + /** Prefer {@link SuggestedFix#prefixWith(Tree, String)} over more contrived alternatives. */ + static final class SuggestedFixPrefixWith { + @BeforeTemplate + SuggestedFix before(Tree tree, String prefix) { + return SuggestedFix.builder().prefixWith(tree, prefix).build(); + } + + @AfterTemplate + SuggestedFix after(Tree tree, String prefix) { + return SuggestedFix.prefixWith(tree, prefix); + } + } + + /** Prefer {@link SuggestedFix#postfixWith(Tree, String)}} over more contrived alternatives. */ + static final class SuggestedFixPostfixWith { + @BeforeTemplate + SuggestedFix before(Tree tree, String postfix) { + return SuggestedFix.builder().postfixWith(tree, postfix).build(); + } + + @AfterTemplate + SuggestedFix after(Tree tree, String postfix) { + return SuggestedFix.postfixWith(tree, postfix); + } + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/SuggestedFixRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/SuggestedFixRulesTestInput.java new file mode 100644 index 0000000000..5641bb0c2e --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/SuggestedFixRulesTestInput.java @@ -0,0 +1,35 @@ +package tech.picnic.errorprone.refasterrules; + +import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.Tree; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class SuggestFixRulesTest implements RefasterRuleCollectionTestCase { + SuggestedFix testSuggestedFixDelete() { + return SuggestedFix.builder().delete(null).build(); + } + + SuggestedFix testSuggestedFixReplaceTree() { + return SuggestedFix.builder().replace(null, "foo").build(); + } + + SuggestedFix testSuggestedFixReplaceStartEnd() { + return SuggestedFix.builder().replace(1, 2, "foo").build(); + } + + SuggestedFix testSuggestedFixReplaceTreeStartEnd() { + return SuggestedFix.builder().replace(null, "foo", 1, 2).build(); + } + + SuggestedFix testSuggestedFixSwap() { + return SuggestedFix.builder().swap((Tree) null, (ExpressionTree) null).build(); + } + + SuggestedFix testSuggestedFixPrefixWith() { + return SuggestedFix.builder().prefixWith(null, "foo").build(); + } + + SuggestedFix testSuggestedFixPostfixWith() { + return SuggestedFix.builder().postfixWith(null, "foo").build(); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/SuggestedFixRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/SuggestedFixRulesTestOutput.java new file mode 100644 index 0000000000..e49929b52e --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/SuggestedFixRulesTestOutput.java @@ -0,0 +1,36 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.errorprone.fixes.SuggestedFix; +import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.Tree; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class SuggestFixRulesTest implements RefasterRuleCollectionTestCase { + SuggestedFix testSuggestedFixDelete() { + return SuggestedFix.delete(null); + } + + SuggestedFix testSuggestedFixReplaceTree() { + return Suggestions.replace(null, "foo"); + } + + SuggestedFix testSuggestedFixReplaceStartEnd() { + return Suggestions.replace(1, 2, "foo"); + } + + SuggestedFix testSuggestedFixReplaceTreeStartEnd() { + return Suggestions.replace(null, "foo", 1, 2); + } + + SuggestedFix testSuggestedFixSwap() { + return Suggestions.swap((Tree) null, (ExpressionTree) null); + } + + SuggestedFix testSuggestedFixPrefixWith() { + return Suggestions.prefixWith(null, "foo"); + } + + SuggestedFix testSuggestedFixPostfixWith() { + return Suggestions.postfixWith(null, "foo"); + } +} diff --git a/pom.xml b/pom.xml index c8d5024660..d9e7e736ea 100644 --- a/pom.xml +++ b/pom.xml @@ -1266,7 +1266,7 @@ org.jacoco jacoco-maven-plugin - 0.8.8 + 0.8.9 org.pitest