[Java] Usage of raw type

Description
Generic classes should be used as such to enhance type safety. Mark usages of raw types.

Type
Code Smell

Snippet

public class UsageOfRawType {
	
	public static void main(String[] args) {
		Map raw = new HashMap(); //Noncompliant
		Map<String,String> typed = new HashMap<>(); //Compliant
	}
	
}

Note
Also covered by Eclipse JDT.

Hi,
there’s already a Jira ticket, please vote here:
https://jira.sonarsource.com/browse/RSPEC-3740
Regards,
Gilbert

Hi,
thanks, I will vote.

I created https://jira.sonarsource.com/browse/SONARJAVA-2941 to add it to our backlog. I don’t know when we will tackle it but at least this rule is back in our radar.

Hi all,

I hope it’s ok to comment on this old discussion.

Here are some relevant quotes from docs.oracle.com:

  1. “A raw type is the name of a generic class or interface without any type arguments.”
  2. “[…] a non-generic class or interface type is not a raw type”
  3. “[…] you should avoid using raw types”.

You may want to consider adding these references to the SONARJAVA-2941 or RSPEC-3740 ticket.

Also, the name of the rule could be changed to “Raw types should be avoided” or simply “Do not use raw types”, since by the definition (see 1.) “raw type” is more concise than “a generic type used raw”.

Thanks Björn, I added a link to your comment on RSPEC-3740

1 Like