Skip to content

Commit 0f13b29

Browse files
committed
Correctly preserve prefix in StringLiteralEquality
Fixes: #130
1 parent 2127a09 commit 0f13b29

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/main/java/org/openrewrite/staticanalysis/StringLiteralEquality.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private boolean isStringLiteral(Expression expression) {
6969
private J.MethodInvocation asEqualsMethodInvocation(J.Binary binary) {
7070
return new J.MethodInvocation(
7171
Tree.randomId(),
72-
binary.getPrefix(),
72+
Space.EMPTY,
7373
Markers.EMPTY,
7474
new JRightPadded<>(binary.getLeft().withPrefix(Space.EMPTY), Space.EMPTY, Markers.EMPTY),
7575
null,
@@ -109,10 +109,9 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) {
109109
if (isStringLiteral(binary.getLeft()) || isStringLiteral(binary.getRight())) {
110110
J after = null;
111111
if (binary.getOperator() == J.Binary.Type.Equal) {
112-
after = asEqualsMethodInvocation(binary);
112+
after = asEqualsMethodInvocation(binary).withPrefix(binary.getPrefix());
113113
} else if (binary.getOperator() == J.Binary.Type.NotEqual) {
114-
J.MethodInvocation mi = asEqualsMethodInvocation(binary);
115-
after = asNegatedUnary(mi);
114+
after = asNegatedUnary(asEqualsMethodInvocation(binary)).withPrefix(binary.getPrefix());
116115
}
117116
if (after != null) {
118117
doAfterVisit(new EqualsAvoidsNull().getVisitor());

src/test/java/org/openrewrite/staticanalysis/StringLiteralEqualityTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public String getString() {
5151
}
5252
5353
public void method(String str) {
54-
if (str == "test") ;
54+
if (str.length() > 1 && str == "test") ;
5555
if ("test" == str) ;
5656
if ("test" == "test") ;
5757
if ("test" == new String("test")) ;
@@ -78,7 +78,7 @@ public String getString() {
7878
}
7979
8080
public void method(String str) {
81-
if ("test".equals(str)) ;
81+
if (str.length() > 1 && "test".equals(str)) ;
8282
if ("test".equals(str)) ;
8383
if ("test".equals("test")) ;
8484
if ("test".equals(new String("test"))) ;
@@ -113,7 +113,7 @@ public String getString() {
113113
}
114114
115115
public void method(String str) {
116-
if (str != "test") ;
116+
if (str.length() > 1 && str != "test") ;
117117
if ("test" != str) ;
118118
if ("test" != "test") ;
119119
if ("test" != new String("test")) ;
@@ -131,7 +131,7 @@ public String getString() {
131131
}
132132
133133
public void method(String str) {
134-
if (!"test".equals(str)) ;
134+
if (str.length() > 1 && !"test".equals(str)) ;
135135
if (!"test".equals(str)) ;
136136
if (!"test".equals("test")) ;
137137
if (!"test".equals(new String("test"))) ;

0 commit comments

Comments
 (0)