Skip to content

Commit bcf32c8

Browse files
authored
SONARJAVA-6283 S3706 Implement quickfix (#5584)
1 parent 94f44c7 commit bcf32c8

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,17 @@ void sequentialStreamForEach(Collection<String> col) {
3737
s.forEach(System.out::println); // Compliant (the rule ignores this case)
3838
}
3939

40+
// fix@qf1 {{Remove the call to ".stream()"}}
41+
// edit@qf1 [[sc=15;ec=24]] {{}}
42+
void quickFix(Collection<Integer> collection) {
43+
collection.stream().forEach(System.out::println); // Noncompliant [[quickfixes=qf1]]
44+
// ^^^^^^^^^^^^^^^^
45+
}
46+
47+
// fix@qf2 {{Remove the call to ".stream()"}}
48+
// edit@qf2 [[sc=8;ec=17]] {{}}
49+
void quickFix2(Set<String> set) {
50+
set.stream().forEach(e -> System.out.println("Element: " + e)); // Noncompliant [[quickfixes=qf2]]
51+
}
52+
4053
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
import java.util.List;
2020
import org.sonar.check.Rule;
21+
import org.sonar.java.reporting.JavaQuickFix;
22+
import org.sonar.java.reporting.JavaTextEdit;
2123
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
2224
import org.sonar.plugins.java.api.semantic.MethodMatchers;
2325
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
2426
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
2527
import org.sonar.plugins.java.api.tree.Tree;
28+
import org.sonar.java.checks.helpers.QuickFixHelper;
2629

2730
@Rule(key = "S3706")
2831
public class StreamForeachCheck extends IssuableSubscriptionVisitor {
@@ -56,7 +59,18 @@ private void checkUnnecessaryForEach(MethodInvocationTree mitForEach) {
5659
&& msetForEach.expression() instanceof MethodInvocationTree mitStream
5760
&& STREAM_METHOD.matches(mitStream)
5861
&& mitStream.methodSelect() instanceof MemberSelectExpressionTree msetStream) {
59-
reportIssue(msetStream.identifier(), msetForEach.identifier(), "Simplify the code by replacing .stream().forEach() with .forEach().");
62+
QuickFixHelper.newIssue(context)
63+
.forRule(this)
64+
.onRange(msetStream.identifier(), msetForEach.identifier())
65+
.withMessage("Simplify the code by replacing .stream().forEach() with .forEach().")
66+
.withQuickFix(() -> computeQuickFix(msetStream, mitStream))
67+
.report();
6068
}
6169
}
70+
71+
private static JavaQuickFix computeQuickFix(MemberSelectExpressionTree msetStream, MethodInvocationTree mitStream) {
72+
return JavaQuickFix.newQuickFix("Remove the call to \".stream()\"")
73+
.addTextEdit(JavaTextEdit.removeBetweenTree(msetStream.operatorToken(), mitStream))
74+
.build();
75+
}
6276
}

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S3706.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"ruleSpecification": "RSPEC-3706",
1212
"sqKey": "S3706",
1313
"scope": "All",
14-
"quickfix": "unknown"
14+
"quickfix": "covered"
1515
}

0 commit comments

Comments
 (0)