Skip to content

Commit

Permalink
SONARJAVA-5185 exclude Optional.orElseThrow from S2201 as it has side…
Browse files Browse the repository at this point in the history
… effects (#4946)
  • Loading branch information
erwan-serandour authored Dec 5, 2024
1 parent 3e2cb44 commit 218d318
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
15 changes: 0 additions & 15 deletions its/ruling/src/test/resources/sonar-server/java-S2201.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
{
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactory.java": [
129
],
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/issue/ws/AssignAction.java": [
153
],
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java": [
110
],
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/qualityprofile/ws/SetDefaultAction.java": [
71
],
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/rule/RuleCreator.java": [
87
],
"org.sonarsource.sonarqube:sonar-server:src/main/java/org/sonar/server/usergroups/ws/AddUserAction.java": [
102
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ public Collection<String> get() {
List<String> makeList() {
return new ArrayList<>();
}

void validateValue(Object o){
Optional.ofNullable(o).orElseThrow(); // Compliant: orElseThrow() is used for the side effect
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class IgnoredReturnValueCheck extends IssuableSubscriptionVisitor {
private static final String JAVA_LANG_STRING = "java.lang.String";
private static final String JAVA_UTIL_FUNCTION_SUPPLIER = "java.util.function.Supplier";
private static final String JAVA_UTIL_STREAM_STREAM = "java.util.stream.Stream";
private static final String JAVA_UTIL_OPTIONAL = "java.util.Optional";
private static final String COLLECT = "collect";
private static final List<String> CHECKED_TYPES = Arrays.asList(
JAVA_LANG_STRING,
Expand Down Expand Up @@ -67,14 +68,15 @@ public class IgnoredReturnValueCheck extends IssuableSubscriptionVisitor {
"java.time.ZonedDateTime",
"java.math.BigInteger",
"java.math.BigDecimal",
"java.util.Optional",
JAVA_UTIL_OPTIONAL,
"com.google.common.base.Optional");

private static final List<String> EXCLUDED_PREFIX = Arrays.asList("parse", "format", "decode", "valueOf");

private static final MethodMatchers EXCLUDED = MethodMatchers.or(
MethodMatchers.create().ofTypes("java.lang.Character").names("toChars").addParametersMatcher("int", "char[]", "int").build(),
MethodMatchers.create().ofTypes(JAVA_LANG_STRING).names("intern").addWithoutParametersMatcher().build());
MethodMatchers.create().ofTypes(JAVA_LANG_STRING).names("intern").addWithoutParametersMatcher().build(),
MethodMatchers.create().ofTypes(JAVA_UTIL_OPTIONAL).names("orElseThrow").withAnyParameters().build());

private static final MethodMatchers STRING_GET_BYTES = MethodMatchers.create()
.ofTypes(JAVA_LANG_STRING).names("getBytes").addParametersMatcher("java.nio.charset.Charset").build();
Expand Down

0 comments on commit 218d318

Please sign in to comment.