jborgers / PMD-jPinpoint-rules

PMD rule set for responsible Java and Kotlin coding: performance, sustainability, multi-threading, data mixup and more.
Apache License 2.0
43 stars 10 forks source link

Fix request: NonComparableMapKeys false positive for Enums #366

Closed stokpop closed 2 days ago

stokpop commented 3 weeks ago

Enums (Auth) are used as keys: are Comparable already.

private static final Map<Auth, Func> settings = Map.ofEntries( Map.entry(Auth.A, Func.A), Map.entry(Auth.B, Func.O), Map.entry(Auth.C, Func.V));

BTW: use EnumMap instead in these cases?

Note: tricky when value is added to an enum: https://stackoverflow.com/questions/519788/why-is-compareto-on-an-enum-final-in-java

jborgers commented 2 days ago

I don't see the false positive in the pmd7 version. Seems to work fine. Pls check.

stokpop commented 2 days ago

Yes, I cannot get the rule to fail, so should be good. Below AuthEnum is ok, Auth is not (does not implement Comparable).

import java.util.Map;
class Foo {

private enum AuthEnum {
A,B,C
}

private static class Auth { 
   public static Auth A;
   public static Auth B;
   public static Auth C;

}

private static final Map<AuthEnum, Func> settings =
Map.ofEntries(
Map.entry(AuthEnum.A, Func.A),
Map.entry(AuthEnum.B, Func.O),
Map.entry(AuthEnum.C, Func.V));