Skip to content

Commit 9d76c0f

Browse files
authored
SONARJAVA-3882 Don't complain about ImmutableSet.of and ImmutableMap.of in S4738 (#4883)
1 parent 0d7703c commit 9d76c0f

3 files changed

Lines changed: 5 additions & 13 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S4738",
33
"hasTruePositives": false,
4-
"falseNegatives": 55,
4+
"falseNegatives": 44,
55
"falsePositives": 0
6-
}
6+
}

java-checks-test-sources/default/src/main/files/non-compiling/checks/ReplaceGuavaWithJavaCheck_java9.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ void tempDir() throws IOException {
6060
}
6161

6262
void immutableCollections() {
63-
ImmutableSet.of("A", "B", "C"); // Noncompliant {{Use "java.util.Set.of()" instead.}}
63+
ImmutableSet.of("A", "B", "C"); // Compliant, because ImmutableSet.of keeps the order of the elements
6464
ImmutableList.of("A", "B", "C"); // Noncompliant {{Use "java.util.List.of()" instead.}}
65-
ImmutableMap.of("A", "B", "C", "D"); // Noncompliant {{Use "java.util.Map.of()" or "java.util.Map.ofEntries()" instead.}}
65+
ImmutableMap.of("A", "B", "C", "D"); // Compliant, because ImmutableMap.of keeps the order of the elements
6666
}
6767
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public class ReplaceGuavaWithJavaCheck extends AbstractMethodDetection implement
4040
private static final String GUAVA_BASE_ENCODING = "com.google.common.io.BaseEncoding";
4141
private static final String GUAVA_OPTIONAL = "com.google.common.base.Optional";
4242
private static final String GUAVA_FILES = "com.google.common.io.Files";
43-
private static final String GUAVA_IMMUTABLE_SET = "com.google.common.collect.ImmutableSet";
4443
private static final String GUAVA_IMMUTABLE_LIST = "com.google.common.collect.ImmutableList";
45-
private static final String GUAVA_IMMUTABLE_MAP = "com.google.common.collect.ImmutableMap";
4644

4745
private static final Map<String, String> GUAVA_TO_JAVA_UTIL_TYPES = MapBuilder.<String, String>newMap()
4846
.put("com.google.common.base.Predicate", "java.util.function.Predicate")
@@ -69,7 +67,7 @@ protected MethodMatchers getMethodInvocationMatchers() {
6967
MethodMatchers.create().ofTypes(GUAVA_OPTIONAL).names("absent").addWithoutParametersMatcher().build(),
7068
MethodMatchers.create().ofTypes(GUAVA_OPTIONAL).names("fromNullable", "of").withAnyParameters().build(),
7169
MethodMatchers.create().ofTypes(GUAVA_FILES).names("createTempDir").addWithoutParametersMatcher().build(),
72-
MethodMatchers.create().ofTypes(GUAVA_IMMUTABLE_LIST, GUAVA_IMMUTABLE_SET, GUAVA_IMMUTABLE_MAP)
70+
MethodMatchers.create().ofTypes(GUAVA_IMMUTABLE_LIST)
7371
.names("of").withAnyParameters().build());
7472
}
7573

@@ -111,12 +109,6 @@ protected void onMethodInvocationFound(MethodInvocationTree mit) {
111109
case GUAVA_IMMUTABLE_LIST:
112110
reportJava9Issue(mit, "java.util.List.of()");
113111
break;
114-
case GUAVA_IMMUTABLE_SET:
115-
reportJava9Issue(mit, "java.util.Set.of()");
116-
break;
117-
case GUAVA_IMMUTABLE_MAP:
118-
reportJava9Issue(mit, "java.util.Map.of()\" or \"java.util.Map.ofEntries()");
119-
break;
120112
default:
121113
break;
122114
}

0 commit comments

Comments
 (0)