Skip to content

Commit 387195b

Browse files
authored
SONARJAVA-5936 : S1452 Remove FPs for wildcards in nested types (#5391)
1 parent d67e0f4 commit 387195b

6 files changed

Lines changed: 46 additions & 24 deletions

File tree

its/ruling/src/test/resources/guava/java-S1452.json

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
{
2-
"com.google.guava:guava:src/com/google/common/base/Enums.java": [
3-
100,
4-
100
5-
],
62
"com.google.guava:guava:src/com/google/common/cache/CacheBuilder.java": [
73
767
84
],
@@ -16,18 +12,11 @@
1612
513
1713
],
1814
"com.google.guava:guava:src/com/google/common/collect/Maps.java": [
19-
106,
20-
111,
21-
649,
22-
2131,
23-
2135
15+
649
2416
],
2517
"com.google.guava:guava:src/com/google/common/collect/MinMaxPriorityQueue.java": [
2618
903
2719
],
28-
"com.google.guava:guava:src/com/google/common/collect/Ordering.java": [
29-
413
30-
],
3120
"com.google.guava:guava:src/com/google/common/collect/RegularImmutableAsList.java": [
3221
49
3322
],
@@ -47,16 +36,12 @@
4736
"com.google.guava:guava:src/com/google/common/collect/TreeMultimap.java": [
4837
156
4938
],
50-
"com.google.guava:guava:src/com/google/common/hash/Funnels.java": [
51-
169
52-
],
5339
"com.google.guava:guava:src/com/google/common/reflect/Element.java": [
5440
48
5541
],
5642
"com.google.guava:guava:src/com/google/common/reflect/Invokable.java": [
5743
67,
58-
108,
59-
129
44+
108
6045
],
6146
"com.google.guava:guava:src/com/google/common/reflect/Parameter.java": [
6247
56,
@@ -67,7 +52,6 @@
6752
157,
6853
238,
6954
275,
70-
315,
7155
364,
7256
387,
7357
562,
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{
22
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/EvaluationResult.java": [
33
46
4-
],
5-
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/computation/task/projectanalysis/step/NewCoverageMeasuresStep.java": [
6-
131
74
]
85
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package checks;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
6+
public class WildCardReturnParameterNestedTypeSample {
7+
void bar() {
8+
foo(listOfLists());
9+
}
10+
11+
void foo(List<List<? extends A>> listList) {
12+
13+
}
14+
15+
List<List<? extends A>> listOfLists() {
16+
return new ArrayList<>();
17+
}
18+
19+
private static class A {
20+
}
21+
22+
private static class B extends A {
23+
}
24+
25+
static class Entry<K, V> {
26+
}
27+
28+
@SuppressWarnings("unchecked")
29+
static <K> Function<Entry<K, ?>, K> keyFunction() {
30+
throw new UnsupportedOperationException();
31+
}
32+
}
33+
34+

java-checks-test-sources/default/src/main/java/checks/WildcardReturnParameterTypeCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public List<Class<?>> getListOfClass() { // Compliant Class is ignored
3434
return null;
3535
}
3636

37-
public Class<List<?>> getClassofList() { // Noncompliant
37+
public Class<List<?>> getClassofList() { // Compliant, nested type is ignored
3838
return null;
3939
}
4040

@@ -43,7 +43,7 @@ public List<? extends Class<String>> bar() { // Noncompliant {{Remove usage of g
4343
}
4444

4545
public List<? // Noncompliant
46-
extends List<?>> getSomething() { // Noncompliant
46+
extends List<?>> getSomething() { // Compliant, skip nested wildcard return types
4747
return null;
4848
}
4949

java-checks/src/main/java/org/sonar/java/checks/WildcardReturnParameterTypeCheck.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public void visitParameterizedType(ParameterizedTypeTree tree) {
6868
} else if (!symbolType.is("java.lang.Class") && !symbolType.isUnknown()) {
6969
typeArguments.forEach(this::reportIfWildcard);
7070
}
71-
super.visitParameterizedType(tree);
7271
}
7372

7473
private void reportIfWildcard(Tree tree) {

java-checks/src/test/java/org/sonar/java/checks/WildcardReturnParameterTypeCheckTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ void test_non_compiling() {
3939
.withCheck(new WildcardReturnParameterTypeCheck())
4040
.verifyNoIssues();
4141
}
42+
43+
@Test
44+
void test_nested_types() {
45+
CheckVerifier.newVerifier()
46+
.onFile(mainCodeSourcesPath("checks/WildCardReturnParameterNestedTypeSample.java"))
47+
.withCheck(new WildcardReturnParameterTypeCheck())
48+
.verifyNoIssues();
49+
}
4250
}

0 commit comments

Comments
 (0)