From 218d318290a424016cb02f6aca5fc842d2927392 Mon Sep 17 00:00:00 2001 From: erwan-serandour Date: Thu, 5 Dec 2024 09:12:55 +0100 Subject: [PATCH] SONARJAVA-5185 exclude Optional.orElseThrow from S2201 as it has side effects (#4946) --- .../test/resources/sonar-server/java-S2201.json | 15 --------------- .../S2201_IgnoredReturnValueCheckSample.java | 4 ++++ .../java/checks/IgnoredReturnValueCheck.java | 6 ++++-- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/its/ruling/src/test/resources/sonar-server/java-S2201.json b/its/ruling/src/test/resources/sonar-server/java-S2201.json index cda0ef60284..5c43b2720a1 100644 --- a/its/ruling/src/test/resources/sonar-server/java-S2201.json +++ b/its/ruling/src/test/resources/sonar-server/java-S2201.json @@ -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 ] } diff --git a/java-checks-test-sources/default/src/main/java/checks/S2201_IgnoredReturnValueCheckSample.java b/java-checks-test-sources/default/src/main/java/checks/S2201_IgnoredReturnValueCheckSample.java index 955803c2a74..c8f7c6a1f82 100644 --- a/java-checks-test-sources/default/src/main/java/checks/S2201_IgnoredReturnValueCheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/S2201_IgnoredReturnValueCheckSample.java @@ -176,4 +176,8 @@ public Collection get() { List makeList() { return new ArrayList<>(); } + + void validateValue(Object o){ + Optional.ofNullable(o).orElseThrow(); // Compliant: orElseThrow() is used for the side effect + } } diff --git a/java-checks/src/main/java/org/sonar/java/checks/IgnoredReturnValueCheck.java b/java-checks/src/main/java/org/sonar/java/checks/IgnoredReturnValueCheck.java index 1096f252b61..2fcac6d4642 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/IgnoredReturnValueCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/IgnoredReturnValueCheck.java @@ -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 CHECKED_TYPES = Arrays.asList( JAVA_LANG_STRING, @@ -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 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();