Skip to content

Commit bb49e1a

Browse files
SONARJAVA-6152 Repair S1612's quickfix for string literals (#5519)
1 parent a27c505 commit bb49e1a

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

java-checks-common/src/main/java/org/sonar/java/checks/helpers/ExpressionsHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.sonar.plugins.java.api.tree.ClassTree;
3737
import org.sonar.plugins.java.api.tree.ExpressionTree;
3838
import org.sonar.plugins.java.api.tree.IdentifierTree;
39+
import org.sonar.plugins.java.api.tree.LiteralTree;
3940
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
4041
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
4142
import org.sonar.plugins.java.api.tree.NewArrayTree;
@@ -80,6 +81,9 @@ public static String concatenate(@Nullable ExpressionTree tree) {
8081
IdentifierTree idt = (IdentifierTree) expr;
8182
pieces.push(idt.name());
8283
}
84+
if (expr instanceof LiteralTree literal) {
85+
pieces.push(literal.token().text());
86+
}
8387

8488
StringBuilder sb = new StringBuilder();
8589
for (String piece : pieces) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ void quickFixes(List<String> strings) {
358358
// ^^
359359
// fix@qf_cast2 {{Replace with "TestA[][].class::cast"}}
360360
// edit@qf_cast2 [[sc=10;ec=40]] {{TestA[][].class::cast}}
361+
362+
strings.stream().allMatch(it -> "x1".equals(it)); // Noncompliant {{Replace this lambda with method reference '"x1"::equals'.}} [[quickfixes=qf_str_lit]]
363+
// ^^
364+
// fix@qf_str_lit {{Replace with ""x1"::equals"}}
365+
// edit@qf_str_lit [[sc=31;ec=52]] {{"x1"::equals}}
361366
}
362367

363368
int notStatic(int x) {

0 commit comments

Comments
 (0)