Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Replace the type specification in this constructor call with the diamond operator ("<>"). [(sonar.java.source not set. Assuming 7 or greater.)]
-
Default Severity:Minor
-
Impact:Low
-
Likelihood:Low
-
Default Quality Profiles:Sonar way
-
Covered Languages:Java
-
Remediation Function:Constant/Issue
-
Constant Cost:1min
-
Analysis Scope:Main Sources, Test Sources
-
Implementation details:
Description
Java 7 introduced the diamond operator (<>) to reduce the verbosity of generics code. For instance, instead of having to declare a List's type in both its declaration and its constructor, you can now simplify the constructor declaration with <>, and the compiler will infer the type.
Note that this rule is automatically disabled when the project's sonar.java.source is lower than 7.
Noncompliant Code Example
List<String> strings = new ArrayList<String>(); // Noncompliant Map<String,List<Integer>> map = new HashMap<String,List<Integer>>(); // Noncompliant
Compliant Solution
List<String> strings = new ArrayList<>(); Map<String,List<Integer>> map = new HashMap<>();
Attachments
Issue Links
- is implemented by
-
SONARJAVA-1276 Rule : S2293 The diamond operator ("<>") should be used
-
- Closed
-
- is related to
-
SONARJAVA-3910 Add quick fixes for S2293 (DiamondOperatorCheck)
-
- Closed
-
-
SONARJAVA-1607 NPE on S2293 when using parametrized types in lambda
-
- Closed
-
-
SONARJAVA-3185 S2293 fails analysis with IllegalStateException
-
- Closed
-
-
SONARJAVA-2549 FN on S2293 on method invocation
-
- Closed
-