diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/DirectReturnTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/DirectReturnTemplates.java new file mode 100644 index 0000000000..9213f3a676 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/DirectReturnTemplates.java @@ -0,0 +1,129 @@ +package tech.picnic.errorprone.refastertemplates; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; + +/** + * Refaster templates that inline variables which are declared and initialized only to be directly + * returned. + */ +final class DirectReturnTemplates { + private DirectReturnTemplates() {} + + static final class DirectlyReturnBooleanVariable { + @BeforeTemplate + boolean before(boolean element) { + boolean var = element; + return var; + } + + @AfterTemplate + boolean after(boolean element) { + return element; + } + } + + static final class DirectlyReturnByteVariable { + @BeforeTemplate + byte before(byte element) { + byte var = element; + return var; + } + + @AfterTemplate + byte after(byte element) { + return element; + } + } + + static final class DirectlyReturnCharVariable { + @BeforeTemplate + char before(char element) { + char var = element; + return var; + } + + @AfterTemplate + char after(char element) { + return element; + } + } + + static final class DirectlyReturnShortVariable { + @BeforeTemplate + short before(short element) { + short var = element; + return var; + } + + @AfterTemplate + short after(short element) { + return element; + } + } + + static final class DirectlyReturnIntVariable { + @BeforeTemplate + int before(int element) { + int var = element; + return var; + } + + @AfterTemplate + int after(int element) { + return element; + } + } + + static final class DirectlyReturnLongVariable { + @BeforeTemplate + long before(long element) { + long var = element; + return var; + } + + @AfterTemplate + long after(long element) { + return element; + } + } + + static final class DirectlyReturnFloatVariable { + @BeforeTemplate + float before(float element) { + float var = element; + return var; + } + + @AfterTemplate + float after(float element) { + return element; + } + } + + static final class DirectlyReturnDoubleVariable { + @BeforeTemplate + double before(double element) { + double var = element; + return var; + } + + @AfterTemplate + double after(double element) { + return element; + } + } + + static final class DirectlyReturnObjectVariable { + @BeforeTemplate + T before(T element) { + T var = element; + return var; + } + + @AfterTemplate + T after(T element) { + return element; + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java index 4238d22212..a265a71bd2 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java @@ -42,6 +42,7 @@ public final class RefasterCheckTest { "BigDecimal", "Collection", "Comparator", + "DirectReturn", "DoubleStream", "Equality", "ImmutableList", diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/DirectReturnTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/DirectReturnTemplatesTestInput.java new file mode 100644 index 0000000000..b264c1d505 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/DirectReturnTemplatesTestInput.java @@ -0,0 +1,64 @@ +package tech.picnic.errorprone.bugpatterns; + +import static java.util.stream.Collectors.joining; + +import java.util.stream.Stream; + +final class DirectReturnTemplatesTest implements RefasterTemplateTestCase { + boolean testDirectlyReturnBooleanVariable() { + boolean var = true; + return var; + } + + byte testDirectlyReturnByteVariable() { + byte var = Byte.MAX_VALUE; + return var; + } + + char testDirectlyReturnCharVariable() { + char var = Character.MAX_VALUE; + return var; + } + + short testDirectlyReturnShortVariable() { + short var = Short.MAX_VALUE; + return var; + } + + int testDirectlyReturnIntVariable() { + int var = Integer.MAX_VALUE; + return var; + } + + long testDirectlyReturnLongVariable() { + long var = Long.MAX_VALUE; + return var; + } + + float testDirectlyReturnFloatVariable() { + float var = Float.MAX_VALUE; + return var; + } + + double testDirectlyReturnDoubleVariable() { + double var = Double.MAX_VALUE; + return var; + } + + String testDirectlyReturnObjectVariable() { + String var = "foo"; + return var; + } + + String testDirectlyReturnObjectVariableWithPrecedingStatement() { + String unrelated = "foo"; + String var = Stream.of("bar", "baz").collect(joining(" ")); + return var; + } + + String testDirectlyReturnObjectVariableWithInterveningStatement() { + String var = "foo"; + String unrelated = "bar"; + return var; + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/DirectReturnTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/DirectReturnTemplatesTestOutput.java new file mode 100644 index 0000000000..800773a65d --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/DirectReturnTemplatesTestOutput.java @@ -0,0 +1,54 @@ +package tech.picnic.errorprone.bugpatterns; + +import static java.util.stream.Collectors.joining; + +import java.util.stream.Stream; + +final class DirectReturnTemplatesTest implements RefasterTemplateTestCase { + boolean testDirectlyReturnBooleanVariable() { + return true; + } + + byte testDirectlyReturnByteVariable() { + return Byte.MAX_VALUE; + } + + char testDirectlyReturnCharVariable() { + return Character.MAX_VALUE; + } + + short testDirectlyReturnShortVariable() { + return Short.MAX_VALUE; + } + + int testDirectlyReturnIntVariable() { + return Integer.MAX_VALUE; + } + + long testDirectlyReturnLongVariable() { + return Long.MAX_VALUE; + } + + float testDirectlyReturnFloatVariable() { + return Float.MAX_VALUE; + } + + double testDirectlyReturnDoubleVariable() { + return Double.MAX_VALUE; + } + + String testDirectlyReturnObjectVariable() { + return "foo"; + } + + String testDirectlyReturnObjectVariableWithPrecedingStatement() { + String unrelated = "foo"; + return Stream.of("bar", "baz").collect(joining(" ")); + } + + String testDirectlyReturnObjectVariableWithInterveningStatement() { + String var = "foo"; + String unrelated = "bar"; + return var; + } +}