Skip to content

Commit 17fe562

Browse files
SONARJAVA-5552 Fix FN in S1943 on method references. (#5143)
1 parent df9c74d commit 17fe562

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S1874",
33
"hasTruePositives": true,
4-
"falseNegatives": 257,
4+
"falseNegatives": 258,
55
"falsePositives": 0
66
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package checks;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.File;
45
import java.io.FileReader;
56
import java.io.FileWriter;
@@ -15,7 +16,9 @@
1516
import java.nio.charset.StandardCharsets;
1617
import java.util.Collection;
1718
import java.util.Formatter;
19+
import java.util.Optional;
1820
import java.util.Scanner;
21+
import java.util.stream.Stream;
1922
import org.apache.commons.io.FileUtils;
2023
import org.apache.commons.io.IOUtils;
2124
import org.checkerframework.common.reflection.qual.UnknownClass;
@@ -149,5 +152,38 @@ void commons_fileutils_with_null(File file, CharSequence charSequence) throws IO
149152
FileUtils.writeStringToFile(file, "data", (java.nio.charset.Charset) null); // Noncompliant
150153
}
151154

155+
static class MethodReferences {
156+
void usageOfConstructorReference(String resourcePath) throws IOException {
157+
try (InputStream is = MethodReferences.class.getResourceAsStream(resourcePath)) {
158+
Optional.ofNullable(is)
159+
.map(InputStreamReader::new) // Noncompliant {{Remove this reference.}}
160+
// ^^^
161+
.map(reader -> reader.hashCode() + 1);
162+
}
163+
}
152164

165+
String usageOfMethodReference(checks.File file) throws Exception {
166+
return myMap(FileUtils::readFileToString, file); // Noncompliant {{Remove this reference.}}
167+
// ^^^^^^^^^^^^^^^^
168+
}
169+
170+
// We need an interface for FileUtils::readFileToString that allows throwing an exception.
171+
interface IOFunction<T, R> {
172+
R apply(T input) throws IOException;
173+
}
174+
175+
String myMap(IOFunction<checks.File, String> mapper, checks.File file) throws IOException {
176+
return mapper.apply(file);
177+
}
178+
179+
Optional<String> goodExamples() {
180+
return Optional.of("good")
181+
.map(String::new) // Compliant
182+
.map(String::toUpperCase); // Compliant
183+
}
184+
185+
Stream<ByteArrayOutputStream> goodBaos() {
186+
return Stream.generate(ByteArrayOutputStream::new); // Compliant
187+
}
188+
}
153189
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.sonar.plugins.java.api.tree.Arguments;
3232
import org.sonar.plugins.java.api.tree.ExpressionTree;
3333
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
34+
import org.sonar.plugins.java.api.tree.MethodReferenceTree;
3435
import org.sonar.plugins.java.api.tree.NewClassTree;
3536
import org.sonar.plugins.java.api.tree.Tree;
3637
import org.sonar.plugins.java.api.tree.TypeCastTree;
@@ -130,11 +131,6 @@ public boolean isCompatibleWithJavaVersion(JavaVersion version) {
130131
return version.isSet() && version.asInt() < 18;
131132
}
132133

133-
@Override
134-
public List<Tree.Kind> nodesToVisit() {
135-
return Arrays.asList(Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS);
136-
}
137-
138134
@Override
139135
protected MethodMatchers getMethodInvocationMatchers() {
140136
ArrayList<MethodMatchers> matchers = new ArrayList<>(Arrays.asList(
@@ -209,6 +205,11 @@ protected void onMethodInvocationFound(MethodInvocationTree mit) {
209205
}
210206
}
211207

208+
@Override
209+
protected void onMethodReferenceFound(MethodReferenceTree methodReferenceTree) {
210+
reportIssue(methodReferenceTree.method(), "Remove this reference.");
211+
}
212+
212213
private void testNullLiteralPassedForEncoding(ExpressionTree argument) {
213214
if (isNullLiteral(argument)) {
214215
reportIssue(argument, "Replace this \"null\" with actual charset.");

0 commit comments

Comments
 (0)