Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Feb 14, 2021
1 parent 06f57ea commit 9252e4a
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
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<T> {
@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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class RefasterCheckTest {
"BigDecimal",
"Collection",
"Comparator",
"DirectReturn",
"DoubleStream",
"Equality",
"ImmutableList",
Expand All @@ -60,7 +61,6 @@ public final class RefasterCheckTest {
"JUnit",
"LongStream",
"MapEntry",
"Method",
"Mockito",
"Multimap",
"Null",
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;
}
}

This file was deleted.

Loading

0 comments on commit 9252e4a

Please sign in to comment.