diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/MethodTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/DirectReturnTemplates.java similarity index 88% rename from error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/MethodTemplates.java rename to error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/DirectReturnTemplates.java index 8fc42fa80d6..9213f3a6760 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/MethodTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/DirectReturnTemplates.java @@ -3,124 +3,126 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; -/** Refaster templates related to expressions dealing with (possibly) null values. */ -final class MethodTemplates { - private MethodTemplates() {} +/** + * Refaster templates that inline variables which are declared and initialized only to be directly + * returned. + */ +final class DirectReturnTemplates { + private DirectReturnTemplates() {} - /** We prefer that variables are directly returned instead of first assigned and then returned. */ - static final class DirectlyReturnObjectVariable { + static final class DirectlyReturnBooleanVariable { @BeforeTemplate - T before(T element) { - T var = element; + boolean before(boolean element) { + boolean var = element; return var; } @AfterTemplate - T after(T element) { + boolean after(boolean element) { return element; } } - static final class DirectlyReturnBooleanVariable { + static final class DirectlyReturnByteVariable { @BeforeTemplate - boolean before(boolean element) { - boolean var = element; + byte before(byte element) { + byte var = element; return var; } @AfterTemplate - boolean after(boolean element) { + byte after(byte element) { return element; } } - static final class DirectlyReturnIntegerVariable { + static final class DirectlyReturnCharVariable { @BeforeTemplate - int before(int element) { - int var = element; + char before(char element) { + char var = element; return var; } @AfterTemplate - int after(int element) { + char after(char element) { return element; } } - static final class DirectlyReturnFloatVariable { + static final class DirectlyReturnShortVariable { @BeforeTemplate - float before(float element) { - float var = element; + short before(short element) { + short var = element; return var; } @AfterTemplate - float after(float element) { + short after(short element) { return element; } } - static final class DirectlyReturnCharVariable { + static final class DirectlyReturnIntVariable { @BeforeTemplate - char before(char element) { - char var = element; + int before(int element) { + int var = element; return var; } @AfterTemplate - char after(char element) { + int after(int element) { return element; } } - static final class DirectlyReturnByteVariable { + static final class DirectlyReturnLongVariable { @BeforeTemplate - byte before(byte element) { - byte var = element; + long before(long element) { + long var = element; return var; } @AfterTemplate - byte after(byte element) { + long after(long element) { return element; } } - static final class DirectlyReturnShortVariable { + static final class DirectlyReturnFloatVariable { @BeforeTemplate - short before(short element) { - short var = element; + float before(float element) { + float var = element; return var; } @AfterTemplate - short after(short element) { + float after(float element) { return element; } } - static final class DirectlyReturnLongVariable { + static final class DirectlyReturnDoubleVariable { @BeforeTemplate - long before(long element) { - long var = element; + double before(double element) { + double var = element; return var; } @AfterTemplate - long after(long element) { + double after(double element) { return element; } } - static final class DirectlyReturnDoubleVariable { + static final class DirectlyReturnObjectVariable { @BeforeTemplate - double before(double element) { - double var = element; + T before(T element) { + T var = element; return var; } @AfterTemplate - double after(double element) { + 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 b34754afe0f..b8032a1ed43 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 @@ -45,6 +45,7 @@ public final class RefasterCheckTest { "BigDecimal", "Collection", "Comparator", + "DirectReturn", "DoubleStream", "Equality", "ImmutableList", @@ -60,7 +61,6 @@ public final class RefasterCheckTest { "JUnit", "LongStream", "MapEntry", - "Method", "Mockito", "Multimap", "Null", 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 00000000000..b264c1d5053 --- /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 00000000000..800773a65d5 --- /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; + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/MethodTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/MethodTemplatesTestInput.java deleted file mode 100644 index 3581e164fb5..00000000000 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/MethodTemplatesTestInput.java +++ /dev/null @@ -1,69 +0,0 @@ -package tech.picnic.errorprone.bugpatterns; - -import java.util.stream.Collectors; -import java.util.stream.Stream; - -final class MethodTemplateTest implements RefasterTemplateTestCase { - String testObjectReturn() { - String var = "Hello!"; - return var; - } - - byte testByteReturn() { - byte var = Byte.MAX_VALUE; - return var; - } - - char testCharacterReturn() { - char var = Character.MAX_VALUE; - return var; - } - - short testShortReturn() { - short var = Short.MAX_VALUE; - return var; - } - - int testIntegerReturn() { - int var = Integer.MAX_VALUE; - return var; - } - - long testLongReturn() { - long var = 349827359L; - return var; - } - - float testFloatReturn() { - float var = 4324.347284F; - return var; - } - - double testDoubleReturn() { - double var = 3492.34284D; - return var; - } - - boolean testBooleanReturn() { - boolean var = true; - return var; - } - - String testStuffAboveDoesntMatter() { - System.out.println("Hi"); - String var = "Hello!"; - return var; - } - - String testStuffBelowDoesMatter() { - String var = "Hello!"; - System.out.println("Hi"); - return var; - } - - String testChainedReturn() { - String var = - Stream.of("I", "like", "error-prone", ":-)").collect(Collectors.joining(" ", "", "!")); - return var; - } -} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/MethodTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/MethodTemplatesTestOutput.java deleted file mode 100644 index 1793d443936..00000000000 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/MethodTemplatesTestOutput.java +++ /dev/null @@ -1,57 +0,0 @@ -package tech.picnic.errorprone.bugpatterns; - -import java.util.stream.Collectors; -import java.util.stream.Stream; - -final class MethodTemplateTest implements RefasterTemplateTestCase { - String testObjectReturn() { - return "Hello!"; - } - - byte testByteReturn() { - return Byte.MAX_VALUE; - } - - char testCharacterReturn() { - return Character.MAX_VALUE; - } - - short testShortReturn() { - return Short.MAX_VALUE; - } - - int testIntegerReturn() { - return Integer.MAX_VALUE; - } - - long testLongReturn() { - return 349827359L; - } - - float testFloatReturn() { - return 4324.347284F; - } - - double testDoubleReturn() { - return 3492.34284D; - } - - boolean testBooleanReturn() { - return true; - } - - String testStuffAboveDoesntMatter() { - System.out.println("Hi"); - return "Hello!"; - } - - String testStuffBelowDoesMatter() { - String var = "Hello!"; - System.out.println("Hi"); - return var; - } - - String testChainedReturn() { - return Stream.of("I", "like", "error-prone", ":-)").collect(Collectors.joining(" ", "", "!")); - } -}