Skip to content

Commit 1b73639

Browse files
eamonnmcmanusError Prone Team
authored andcommitted
Update MisleadingEscapedSpace to account for CRLF line endings.
PiperOrigin-RevId: 889336806
1 parent 73f258d commit 1b73639

2 files changed

Lines changed: 98 additions & 106 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/MisleadingEscapedSpace.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public Description matchLiteral(LiteralTree tree, VisitorState state) {
6767
for (int i = 0; i < literal.length(); ++i) {
6868
switch (literal.charAt(i)) {
6969
case '\n':
70+
case '\r':
7071
seenEscape = false;
7172
break;
7273
case '\\':

core/src/test/java/com/google/errorprone/bugpatterns/MisleadingEscapedSpaceTest.java

Lines changed: 97 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -17,152 +17,143 @@
1717
package com.google.errorprone.bugpatterns;
1818

1919
import com.google.errorprone.CompilationTestHelper;
20+
import com.google.testing.junit.testparameterinjector.TestParameter;
21+
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2022
import org.junit.Test;
2123
import org.junit.runner.RunWith;
22-
import org.junit.runners.JUnit4;
2324

24-
@RunWith(JUnit4.class)
25+
@RunWith(TestParameterInjector.class)
2526
public final class MisleadingEscapedSpaceTest {
2627
private final CompilationTestHelper testHelper =
2728
CompilationTestHelper.newInstance(MisleadingEscapedSpace.class, getClass());
2829

30+
private enum LineSeparator {
31+
CRLF("\r\n"),
32+
LF("\n");
33+
34+
final String separator;
35+
36+
LineSeparator(String separator) {
37+
this.separator = separator;
38+
}
39+
}
40+
41+
@TestParameter private LineSeparator lineSeparator;
42+
43+
private void test(String text) {
44+
testHelper.addSourceLines("Test.class", text.replace("\n", lineSeparator.separator)).doTest();
45+
}
46+
2947
@Test
3048
public void misleadingEscape() {
31-
testHelper
32-
.addSourceLines(
33-
"Test.class",
34-
"""
35-
class Test {
36-
// BUG: Diagnostic contains:
37-
private static final String FOO = " \\s ";
38-
}
39-
""")
40-
.doTest();
49+
test(
50+
"""
51+
class Test {
52+
// BUG: Diagnostic contains:
53+
private static final String FOO = " \\s ";
54+
}
55+
""");
4156
}
4257

4358
@Test
4459
public void literalBackslashS() {
45-
testHelper
46-
.addSourceLines(
47-
"Test.class",
48-
"""
49-
class Test {
50-
private static final String FOO = " \\\\s ";
51-
}
52-
""")
53-
.doTest();
60+
test(
61+
"""
62+
class Test {
63+
private static final String FOO = " \\\\s ";
64+
}
65+
""");
5466
}
5567

5668
@Test
5769
public void asSingleCharacter_misleading() {
58-
testHelper
59-
.addSourceLines(
60-
"Test.class",
61-
"""
62-
class Test {
63-
// BUG: Diagnostic contains:
64-
private static final char x = '\\s';
65-
}
66-
""")
67-
.doTest();
70+
test(
71+
"""
72+
class Test {
73+
// BUG: Diagnostic contains:
74+
private static final char x = '\\s';
75+
}
76+
""");
6877
}
6978

7079
@Test
7180
public void withinTextBlock_notAtEndOfLine_misleading() {
72-
testHelper
73-
.addSourceLines(
74-
"Test.class",
75-
"""
76-
class Test {
77-
private static final String FOO =
78-
// BUG: Diagnostic contains:
79-
\"""
80-
foo \\s bar
81-
\""";
82-
private static final String BAZ =
83-
// BUG: Diagnostic contains:
84-
\"""
85-
foo \\s
86-
bar \\s baz
87-
\""";
88-
}
89-
""")
90-
.doTest();
81+
test(
82+
"""
83+
class Test {
84+
private static final String FOO =
85+
// BUG: Diagnostic contains:
86+
\"""
87+
foo \\s bar
88+
\""";
89+
private static final String BAZ =
90+
// BUG: Diagnostic contains:
91+
\"""
92+
foo \\s
93+
bar \\s baz
94+
\""";
95+
}
96+
""");
9197
}
9298

9399
@Test
94100
public void atEndOfLine_notMisleading() {
95-
testHelper
96-
.addSourceLines(
97-
"Test.class",
98-
"""
99-
class Test {
100-
private static final String FOO =
101-
\"""
102-
foo \\s
103-
bar \\s
104-
\""";
105-
}
106-
""")
107-
.doTest();
101+
test(
102+
"""
103+
class Test {
104+
private static final String FOO =
105+
\"""
106+
foo \\s
107+
bar \\s
108+
\""";
109+
}
110+
""");
108111
}
109112

110113
@Test
111114
public void multipleAtEndOfLine_notMisleading() {
112-
testHelper
113-
.addSourceLines(
114-
"Test.class",
115-
"""
116-
class Test {
117-
private static final String FOO =
118-
\"""
119-
foo \\s\\s\\s\\s
120-
\""";
121-
}
122-
""")
123-
.doTest();
115+
test(
116+
"""
117+
class Test {
118+
private static final String FOO =
119+
\"""
120+
foo \\s\\s\\s\\s
121+
\""";
122+
}
123+
""");
124124
}
125125

126126
@Test
127127
public void withinCommentInBrokenUpString_noFinding() {
128-
testHelper
129-
.addSourceLines(
130-
"Test.class",
131-
"""
132-
class Test {
133-
private static final String FOO = "foo" + /* \\s */ " bar";
134-
}
135-
""")
136-
.doTest();
128+
test(
129+
"""
130+
class Test {
131+
private static final String FOO = "foo" + /* \\s */ " bar";
132+
}
133+
""");
137134
}
138135

139136
@Test
140137
public void atEndOfString_noFinding() {
141-
testHelper
142-
.addSourceLines(
143-
"Test.class",
144-
"""
145-
class Test {
146-
private static final String FOO =
147-
\"""
148-
foo
149-
bar\\s\""";
150-
}
151-
""")
152-
.doTest();
138+
test(
139+
"""
140+
class Test {
141+
private static final String FOO =
142+
\"""
143+
foo
144+
bar\\s\""";
145+
}
146+
""");
153147
}
154148

155149
@Test
156150
public void escapedSpaceAtEndOfString() {
157-
testHelper
158-
.addSourceLines(
159-
"Test.class",
160-
"""
161-
class Test {
162-
// BUG: Diagnostic contains:
163-
private static final String FOO = "foo\\s";
164-
}
165-
""")
166-
.doTest();
151+
test(
152+
"""
153+
class Test {
154+
// BUG: Diagnostic contains:
155+
private static final String FOO = "foo\\s";
156+
}
157+
""");
167158
}
168159
}

0 commit comments

Comments
 (0)