Skip to content

Commit

Permalink
Allow class types in DangerousIdentityKey (#2036)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoenig10 authored Jan 13, 2022
1 parent eba4ba3 commit ad9a16e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.util.ASTHelpers;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
Expand All @@ -42,10 +43,20 @@ public final class DangerousIdentityKey extends MoreAbstractAsKeyOfSetOrMap {

@Override
protected boolean isBadType(Type type, VisitorState state) {
// Only flag final types, otherwise we'll encounter false positives when presented with overrides.
if (type == null || !type.isFinal()) {
if (type == null) {
return false;
}

// Ignore non-final types, otherwise we'll encounter false positives when presented with overrides.
if (!type.isFinal()) {
return false;
}

// Ignore class types
if (ASTHelpers.isSameType(type, state.getSymtab().classType, state)) {
return false;
}

return !implementsMethod(state.getTypes(), type, state.getNames().equals, state)
|| !implementsMethod(state.getTypes(), type, state.getNames().hashCode, state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ public void testValidEnum() {
.doTest();
}

@Test
public void testValidClass() {
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.*;",
"class Test {",
" private Object test() {",
" return new HashSet<Class<String>>();",
" }",
"}")
.doTest();
}

@Test
public void testInvalidNoEquals() {
compilationHelper
Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-2036.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
fix:
description: '`DangerousIdentityKey` now allows `Class` to be used as a map or set
key.'
links:
- https://github.com/palantir/gradle-baseline/pull/2036

0 comments on commit ad9a16e

Please sign in to comment.