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

Suggest Double.compare in ObjectEqualsForPrimitives #4394

Closed
wants to merge 1 commit 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
Expand Up @@ -29,7 +29,12 @@
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Type;

import javax.lang.model.type.TypeKind;

/**
* Check for usage of {@code Objects.equal} on primitive types.
Expand All @@ -53,11 +58,28 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return NO_MATCH;
}

String arg1 = state.getSourceForNode(tree.getArguments().get(0));
String arg2 = state.getSourceForNode(tree.getArguments().get(1));
ExpressionTree expression1 = tree.getArguments().get(0);
ExpressionTree expression2 = tree.getArguments().get(1);
if (isFloatingPoint(expression1) || isFloatingPoint(expression2)) {
// Objects.equal(a_double, another_double) compares NaN as equal, but a_double == another_double does not.
// We could replace this with Double.doubleToLongBits(a_double) == Double.doubleToLongBits(another_double)
// to avoid boxing, but that can be considered as less readable.
return NO_MATCH;
}

String arg1 = state.getSourceForNode(expression1);
String arg2 = state.getSourceForNode(expression2);

// TODO: Rewrite to a != b if the original code has a negation (e.g. !Object.equals)
Fix fix = SuggestedFix.builder().replace(tree, "(" + arg1 + " == " + arg2 + ")").build();
return describeMatch(tree, fix);
}

private static boolean isFloatingPoint(ExpressionTree expression) {
Type type = ASTHelpers.getType(expression);
if (type == null) {
return false;
}
return type.getKind() == TypeKind.DOUBLE || type.getKind() == TypeKind.FLOAT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,64 @@ public void intAndLong() {
"}")
.doTest();
}

@Test
public void doubles() {
refactoringHelper
.addInputLines(
"Test.java",
"import java.util.Objects;",
"public class Test {",
" private static boolean testDoubles(double a, double b) {",
" return Objects.equals(a, b);",
" }",
"}")
.expectUnchanged()
.doTest();
}

@Test
public void doubleAndFloat() {
refactoringHelper
.addInputLines(
"Test.java",
"import java.util.Objects;",
"public class Test {",
" private static boolean testDoubles(double a, float b) {",
" return Objects.equals(a, b);",
" }",
"}")
.expectUnchanged()
.doTest();
}

@Test
public void doubleAndLong() {
refactoringHelper
.addInputLines(
"Test.java",
"import java.util.Objects;",
"public class Test {",
" private static boolean testDoubles(double a, long b) {",
" return Objects.equals(a, b);",
" }",
"}")
.expectUnchanged()
.doTest();
}

@Test
public void floatAndLong() {
refactoringHelper
.addInputLines(
"Test.java",
"import java.util.Objects;",
"public class Test {",
" private static boolean testDoubles(float a, long b) {",
" return Objects.equals(a, b);",
" }",
"}")
.expectUnchanged()
.doTest();
}
}
Loading