Skip to content

Commit

Permalink
Match primitive wrapper types exactly in BoxedPrimitiveEquality
Browse files Browse the repository at this point in the history
instead of making assumptions about arbitrary implementations of `Number`.

PiperOrigin-RevId: 506803509
  • Loading branch information
cushon authored and Error Prone Team committed Feb 3, 2023
1 parent a2f6e26 commit ea28574
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
package com.google.errorprone.bugpatterns;

import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.util.ASTHelpers.enclosingPackage;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.isStatic;
import static com.google.errorprone.util.ASTHelpers.isSubtype;

import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.suppliers.Supplier;
import com.sun.source.tree.ExpressionTree;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
Expand All @@ -43,12 +39,9 @@
altNames = {"NumericEquality"},
severity = ERROR)
public final class BoxedPrimitiveEquality extends AbstractReferenceEquality {
private final boolean handleNumber;

@Inject
public BoxedPrimitiveEquality(ErrorProneFlags flags) {
this.handleNumber = flags.getBoolean("BoxedPrimitiveEquality:HandleNumber").orElse(true);
}
public BoxedPrimitiveEquality() {}

@Override
protected boolean matchArgument(ExpressionTree tree, VisitorState state) {
Expand All @@ -63,28 +56,7 @@ protected boolean matchArgument(ExpressionTree tree, VisitorState state) {
}

private boolean isRelevantType(Type type, VisitorState state) {
if (handleNumber && isSubtype(type, JAVA_LANG_NUMBER.get(state), state)) {
if (enclosingPackage(type.tsym)
.getQualifiedName()
.contentEquals("java.util.concurrent.atomic")) {
// atomics don't implement value equality
return false;
}
return true;
}
switch (state.getTypes().unboxedType(type).getTag()) {
case BYTE:
case CHAR:
case SHORT:
case INT:
case LONG:
case FLOAT:
case DOUBLE:
case BOOLEAN:
return true;
default:
return false;
}
return !type.isPrimitive() && state.getTypes().unboxedType(type).isPrimitive();
}

private static boolean isStaticConstant(Symbol sym) {
Expand All @@ -94,7 +66,4 @@ private static boolean isStaticConstant(Symbol sym) {
public static boolean isFinal(Symbol s) {
return (s.flags() & Flags.FINAL) == Flags.FINAL;
}

private static final Supplier<Type> JAVA_LANG_NUMBER =
VisitorState.memoize(state -> state.getTypeFromString("java.lang.Number"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,18 @@ public void positive() {
}

@Test
public void positive_forNumber() {
public void negative_forNumber() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" boolean f(Number a, Number b) {",
" // BUG: Diagnostic contains:",
" return a == b;",
" }",
"}")
.doTest();
}

@Test
public void number_flagOff_noFinding() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" boolean f(Number a, Number b) {",
" return a == b;",
" }",
"}")
.setArgs("-XepOpt:BoxedPrimitiveEquality:HandleNumber=false")
.doTest();
}

@Test
public void negative() {
compilationHelper
Expand All @@ -81,6 +66,9 @@ public void negative() {
" boolean f(boolean a, boolean b) {",
" return a == b;",
" }",
" boolean g(String a, String b) {",
" return a == b;",
" }",
"}")
.doTest();
}
Expand Down

0 comments on commit ea28574

Please sign in to comment.