Search code examples
javaeclipseeclipse-luna

Eclipse - generate a warning when using map.get with an unexpected type


Is there a setting in Eclipse Luna (or a newer version) that enables a warning to be displayed in the following case:

Map<K, V> map = new HashMap<>();
map.get(val);

where the type of val is not K? (the warning should be displayed on the second line)

I know that the code above is valid because Map.get takes an Object as an argument, but in almost every case is an error on my part, so I would like a warning to be displayed.


Solution

  • Eclipse and its compiler are meant to be aligned with the Java compiler and the formal language specification. The javac compiler accepts your code without any warning (even with -Xlint enabled), because the get method takes an Object.

    So there is no reason why Eclipse should come up with its own warning. Seems more like the job of a static analysis tool like FindBugs.