question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request - detect key of wrong type passed to Map.get() or Map.remove()

See original GitHub issue

Since the signature of these methods, by design, does not constrict you to use the generic type of the key, and is just Object, it is easy to accidentally pass the wrong variable.

This is especially true for remove(), which in the case of cache clean-up code, might not have full test coverage, or missing/difficult to assert behaviour. This has been seen to lead to leaked memory.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Stephan202commented, Aug 9, 2021

This sounds like what the CollectionIncompatibleType check (source) already does. Is that what you’re looking for?

0reactions
gregallencommented, Aug 9, 2021

ok, wow! IntelliJ’s inspection is not so generous or conservative depending on your view point.

maybe there could be an experimental version of the rule that was “stricter”?

Or, maybe a warning type rule for using non-final types as keys?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are the reasons why Map.get(Object key) is not (fully ...
TreeMap may fail when you pass objects of the wrong type to the get method but may pass occasionally, e.g. when the map...
Read more >
335953 – No warning for wrongly typed map get - Bugs - Eclipse
Map.get(Object) where V is the type variable for the key. ... you try to get() from the Map with a wrong key type,...
Read more >
Map and Set - The Modern JavaScript Tutorial
map.has(key) – returns true if the key exists, false otherwise. map.delete(key) – removes the element (the key/value pair) by the key. map.clear ...
Read more >
How to Handle the Incompatible Types Error in Java - Rollbar
The Java incompatible types error happens when a value assigned to a variable or returned by a method is incompatible with the one...
Read more >
Map (Java Platform SE 8 ) - Oracle Help Center
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found