Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Feb 19, 2023
1 parent acddd00 commit eaea479
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,23 @@ boolean testStreamAllMatch2() {
}

ImmutableSet<Integer> testStreamMapToIntSum() {
Function<String, Integer> parseIntFunction = (String s) -> Integer.parseInt(s);
Function<String, Integer> parseIntFunction = Integer::parseInt;
return ImmutableSet.of(
Stream.of(1).map(i -> i * 2).reduce(0, Integer::sum),
Stream.of("1").map(Integer::parseInt).reduce(0, Integer::sum),
Stream.of("1").map(parseIntFunction).reduce(0, Integer::sum));
}

ImmutableSet<Double> testStreamMapToDoubleSum() {
Function<String, Double> parseDoubleFunction = (String s) -> Double.parseDouble(s);
Function<String, Double> parseDoubleFunction = Double::parseDouble;
return ImmutableSet.of(
Stream.of(1).map(i -> i * 2.0).reduce(0.0, Double::sum),
Stream.of("1").map(Double::parseDouble).reduce(0.0, Double::sum),
Stream.of("1").map(parseDoubleFunction).reduce(0.0, Double::sum));
}

ImmutableSet<Long> testStreamMapToLongSum() {
Function<String, Long> parseLongFunction = (String s) -> Long.parseLong(s);
Function<String, Long> parseLongFunction = Long::parseLong;
return ImmutableSet.of(
Stream.of(1).map(i -> i * 2L).reduce(0L, Long::sum),
Stream.of("1").map(Long::parseLong).reduce(0L, Long::sum),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,23 @@ boolean testStreamAllMatch2() {
}

ImmutableSet<Integer> testStreamMapToIntSum() {
Function<String, Integer> parseIntFunction = (String s) -> Integer.parseInt(s);
Function<String, Integer> parseIntFunction = Integer::parseInt;
return ImmutableSet.of(
Stream.of(1).mapToInt(i -> i * 2).sum(),
Stream.of("1").mapToInt(Integer::parseInt).sum(),
Stream.of("1").map(parseIntFunction).reduce(0, Integer::sum));
}

ImmutableSet<Double> testStreamMapToDoubleSum() {
Function<String, Double> parseDoubleFunction = (String s) -> Double.parseDouble(s);
Function<String, Double> parseDoubleFunction = Double::parseDouble;
return ImmutableSet.of(
Stream.of(1).mapToDouble(i -> i * 2.0).sum(),
Stream.of("1").mapToDouble(Double::parseDouble).sum(),
Stream.of("1").map(parseDoubleFunction).reduce(0.0, Double::sum));
}

ImmutableSet<Long> testStreamMapToLongSum() {
Function<String, Long> parseLongFunction = (String s) -> Long.parseLong(s);
Function<String, Long> parseLongFunction = Long::parseLong;
return ImmutableSet.of(
Stream.of(1).mapToLong(i -> i * 2L).sum(),
Stream.of("1").mapToLong(Long::parseLong).sum(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
package tech.picnic.errorprone.refaster.matchers;

import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.sun.source.tree.MemberReferenceTree.ReferenceMode.INVOKE;

import com.google.errorprone.VisitorState;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.LambdaExpressionTree;
import com.sun.source.tree.MemberReferenceTree;
import com.sun.source.tree.Tree;

/** A matcher of lambda expressions or method references. */
/** A matcher of lambda expressions and method references. */
public final class IsLambdaExpressionOrMethodReference implements Matcher<ExpressionTree> {
private static final long serialVersionUID = 1L;

private static final Matcher<ExpressionTree> DELEGATE =
anyOf(isLambdaExpression(), isMethodReference());

/** Instantiates a new {@link IsLambdaExpressionOrMethodReference} instance. */
public IsLambdaExpressionOrMethodReference() {}

@Override
public boolean matches(ExpressionTree tree, VisitorState state) {
return DELEGATE.matches(tree, state);
}

/** Returns a matcher that matches lambda expressions. */
public static <T extends Tree> Matcher<T> isLambdaExpression() {
return (tree, state) -> tree instanceof LambdaExpressionTree;
}

/** Returns a matcher that matches method references. */
public static <T extends Tree> Matcher<T> isMethodReference() {
return (tree, state) ->
tree instanceof MemberReferenceTree
&& ((MemberReferenceTree) tree).getMode().equals(INVOKE);
return tree instanceof LambdaExpressionTree || tree instanceof MemberReferenceTree;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import com.google.errorprone.BugPattern;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.bugpatterns.BugChecker;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

final class IsLambdaExpressionOrMethodReferenceTest {

@Disabled
@Test
void matches() {
CompilationTestHelper.newInstance(MatcherTestChecker.class, getClass())
Expand All @@ -21,23 +18,35 @@ void matches() {
"",
"class A {",
" Integer negative1() {",
" // BUG: Diagnostic contains:",
" Function<String, Integer> parseIntFunction = (String s) -> Integer.parseInt(s);",
" return Stream.of(\"1\").map(parseIntFunction).reduce(0, Integer::sum);",
" return Stream.of(\"1\")",
" .map(parseIntFunction)",
" // BUG: Diagnostic contains:",
" .reduce(0, Integer::sum);",
" }",
"",
" Integer negative2() {",
" // BUG: Diagnostic contains:",
" Function<String, Integer> stringLengthMethodReference = String::length;",
" return Stream.of(\"1\").map(stringLengthMethodReference).reduce(0, Integer::sum);",
" return Stream.of(\"1\")",
" .map(stringLengthMethodReference)",
" // BUG: Diagnostic contains:",
" .reduce(0, Integer::sum);",
" }",
"",
" Double negative3() {",
" Function<String, Double> parseDoubleFunction = new Function<String, Double>() {",
" @Override",
" public Double apply(String s) {",
" return Double.parseDouble(s);",
" }",
" };",
" return Stream.of(\"1\").map(parseDoubleFunction).reduce(0.0, Double::sum);",
" Function<String, Double> parseDoubleFunction =",
" new Function<String, Double>() {",
" @Override",
" public Double apply(String s) {",
" return Double.parseDouble(s);",
" }",
" };",
" return Stream.of(\"1\")",
" .map(parseDoubleFunction)",
" // BUG: Diagnostic contains:",
" .reduce(0.0, Double::sum);",
" }",
"",
" Long negative4() {",
Expand All @@ -47,27 +56,37 @@ void matches() {
" return Long.parseLong(s);",
" }",
" }",
" return Stream.of(\"1\").map(new ParseLongFunction()).reduce(0L, Long::sum);",
" return Stream.of(\"1\")",
" .map(new ParseLongFunction())",
" // BUG: Diagnostic contains:",
" .reduce(0L, Long::sum);",
" }",
"",
" Integer positive1() {",
" // BUG: Diagnostic contains:",
" return Stream.of(1).map(i -> i * 2).reduce(0, Integer::sum);",
" return Stream.of(1)",
" // BUG: Diagnostic contains:",
" .map(i -> i * 2)",
" // BUG: Diagnostic contains:",
" .reduce(0, Integer::sum);",
" }",
"",
" Long positive2() {",
" // BUG: Diagnostic contains:",
" return Stream.of(1)",
" .map(",
" i -> {",
" return i * 2L;",
" })",
" .reduce(0L, Long::sum);",
" .map(",
" // BUG: Diagnostic contains:",
" i -> {",
" return i * 2L;",
" })",
" // BUG: Diagnostic contains:",
" .reduce(0L, Long::sum);",
" }",
"",
" Double positive3() {",
" // BUG: Diagnostic contains:",
" return Stream.of(\"1\").map(Double::parseDouble).reduce(0.0, Double::sum);",
" return Stream.of(\"1\")",
" // BUG: Diagnostic contains:",
" .map(Double::parseDouble)",
" // BUG: Diagnostic contains:",
" .reduce(0.0, Double::sum);",
" }",
"}")
.doTest();
Expand All @@ -80,6 +99,9 @@ void matches() {
public static final class MatcherTestChecker extends AbstractMatcherTestChecker {
private static final long serialVersionUID = 1L;

// XXX: This is a false positive reported by Checkstyle. See
// https://github.com/checkstyle/checkstyle/issues/10161#issuecomment-1242732120.
@SuppressWarnings("RedundantModifier")
public MatcherTestChecker() {
super(new IsLambdaExpressionOrMethodReference());
}
Expand Down

0 comments on commit eaea479

Please sign in to comment.