Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When variables are assigned then returned, instead directly return them #1

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<T> {
@BeforeTemplate
T before(T element) {
T var = element;
return var;
}

@AfterTemplate
T after(T element) {
return element;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public final class RefasterCheckTest {
"BigDecimal",
"Collection",
"Comparator",
"DirectReturn",
"DoubleStream",
"Equality",
"ImmutableList",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}