Some key-value map implementations return null if a key is non-existent (eg, HashMap), while other can throw some key-does-not-exist exception.
In both cases, retrieving and using the returned value without any prior checks is a code smell and can lead to unexpected null pointer exceptions (or non-existent key exceptions).
It is a good practice to check for null before using the returned value, or check the existence of the key before retrieving it.
“what could be its impact?” and “what could happen in case of a successful attack?” (for Vulnerabilities and Security Hotspots)
I see some potential for false-positives (or at least annoyances) in such a rule.
What if you were to just outright return the value from the Map, and the calling code in some other function may be responsible for doing the null check?
Or when you are certain that your Map always contains a mapping for a specific key and that it would be unnecessary boilerplate to explicitly check for its presence?
And where you would actually want to have an exception instead of some fallback that hides away the actual problem, but where getting a NullPointerException is good enough to point you at the problem and adding your own dedicated exception handling to every use of a Map would be overkill.
Thanks for starting this discussion and sharing your rule idea. You’ve correctly identified that using the result of map.get() without a check is a common source of NullPointerExceptions, and your examples illustrate this well.
The follow-up points raised by @CrushaKRool about potential false positives are also legitimate. This highlights the core challenge: inferring the programmer’s intention with Java’s standard Map.get(). Sometimes the key is expected to be present and if it is not present it is a bug. In this case, I think failing fast is the most appropriate action (an NPE is acceptable). Other times, the key might be present or not and performing a check is necessary.
In Java, it is difficult to guess the intention of the programmer. In other languages, it is different. For instance, in Scala map(key) raises an exception if the key does not exist and map.get(key) returns null.
Introducing the rule risks creating a lot of noise and we prioritize keeping false positives low. We’ll hold off on adding this specific rule for now. However, we will keep track of the idea and if at some point a Map.getExist method [or similar] is added to the standard library, we will implement the rule.
Thank you for reaching out with this rule idea and to provide this level of details.