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

Allow class types in DangerousIdentityKey #2036

Merged
merged 2 commits into from
Jan 13, 2022
Merged
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 @@ -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