Skip to content

Commit e3c6044

Browse files
committed
fix some false negatives for single_char_pattern
1 parent 907f6d9 commit e3c6044

File tree

4 files changed

+80
-35
lines changed

4 files changed

+80
-35
lines changed

clippy_lints/src/methods/single_char_pattern.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ use rustc_span::symbol::Symbol;
99

1010
use super::SINGLE_CHAR_PATTERN;
1111

12-
const PATTERN_METHODS: [(&str, usize); 19] = [
12+
const PATTERN_METHODS: [(&str, usize); 24] = [
1313
("contains", 1),
1414
("starts_with", 1),
1515
("ends_with", 1),
1616
("find", 1),
1717
("rfind", 1),
1818
("split", 1),
19+
("split_inclusive", 1),
1920
("rsplit", 1),
2021
("split_terminator", 1),
2122
("rsplit_terminator", 1),
2223
("splitn", 2),
2324
("rsplitn", 2),
25+
("split_once", 1),
26+
("rsplit_once", 1),
2427
("matches", 1),
2528
("rmatches", 1),
2629
("match_indices", 1),
@@ -29,6 +32,8 @@ const PATTERN_METHODS: [(&str, usize); 19] = [
2932
("strip_suffix", 1),
3033
("trim_start_matches", 1),
3134
("trim_end_matches", 1),
35+
("replace", 1),
36+
("replacen", 1),
3237
];
3338

3439
/// lint for length-1 `str`s for methods in `PATTERN_METHODS`

tests/ui/single_char_pattern.fixed

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fn main() {
1717
x.split('💣');
1818
// Can't use this lint for unicode code points which don't fit in a char
1919
x.split("❤️");
20+
x.split_inclusive('x');
2021
x.contains('x');
2122
x.starts_with('x');
2223
x.ends_with('x');
@@ -27,6 +28,8 @@ fn main() {
2728
x.rsplit_terminator('x');
2829
x.splitn(2, 'x');
2930
x.rsplitn(2, 'x');
31+
x.split_once('x');
32+
x.rsplit_once('x');
3033
x.matches('x');
3134
x.rmatches('x');
3235
x.match_indices('x');
@@ -35,6 +38,8 @@ fn main() {
3538
x.trim_end_matches('x');
3639
x.strip_prefix('x');
3740
x.strip_suffix('x');
41+
x.replace('x', "y");
42+
x.replacen('x', "y", 3);
3843
// Make sure we escape characters correctly.
3944
x.split('\n');
4045
x.split('\'');
@@ -43,7 +48,7 @@ fn main() {
4348
let h = HashSet::<String>::new();
4449
h.contains("X"); // should not warn
4550

46-
x.replace(";", ",").split(','); // issue #2978
51+
x.replace(';', ",").split(','); // issue #2978
4752
x.starts_with('\x03'); // issue #2996
4853

4954
// Issue #3204

tests/ui/single_char_pattern.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fn main() {
1717
x.split("💣");
1818
// Can't use this lint for unicode code points which don't fit in a char
1919
x.split("❤️");
20+
x.split_inclusive("x");
2021
x.contains("x");
2122
x.starts_with("x");
2223
x.ends_with("x");
@@ -27,6 +28,8 @@ fn main() {
2728
x.rsplit_terminator("x");
2829
x.splitn(2, "x");
2930
x.rsplitn(2, "x");
31+
x.split_once("x");
32+
x.rsplit_once("x");
3033
x.matches("x");
3134
x.rmatches("x");
3235
x.match_indices("x");
@@ -35,6 +38,8 @@ fn main() {
3538
x.trim_end_matches("x");
3639
x.strip_prefix("x");
3740
x.strip_suffix("x");
41+
x.replace("x", "y");
42+
x.replacen("x", "y", 3);
3843
// Make sure we escape characters correctly.
3944
x.split("\n");
4045
x.split("'");
@@ -43,7 +48,7 @@ fn main() {
4348
let h = HashSet::<String>::new();
4449
h.contains("X"); // should not warn
4550

46-
x.replace(";", ",").split(","); // issue #2978
51+
x.replace(';', ",").split(","); // issue #2978
4752
x.starts_with("\x03"); // issue #2996
4853

4954
// Issue #3204

tests/ui/single_char_pattern.stderr

+62-32
Original file line numberDiff line numberDiff line change
@@ -25,184 +25,214 @@ LL | x.split("💣");
2525
| ^^^^ help: try using a `char` instead: `'💣'`
2626

2727
error: single-character string constant used as pattern
28-
--> $DIR/single_char_pattern.rs:20:16
28+
--> $DIR/single_char_pattern.rs:20:23
29+
|
30+
LL | x.split_inclusive("x");
31+
| ^^^ help: try using a `char` instead: `'x'`
32+
33+
error: single-character string constant used as pattern
34+
--> $DIR/single_char_pattern.rs:21:16
2935
|
3036
LL | x.contains("x");
3137
| ^^^ help: try using a `char` instead: `'x'`
3238

3339
error: single-character string constant used as pattern
34-
--> $DIR/single_char_pattern.rs:21:19
40+
--> $DIR/single_char_pattern.rs:22:19
3541
|
3642
LL | x.starts_with("x");
3743
| ^^^ help: try using a `char` instead: `'x'`
3844

3945
error: single-character string constant used as pattern
40-
--> $DIR/single_char_pattern.rs:22:17
46+
--> $DIR/single_char_pattern.rs:23:17
4147
|
4248
LL | x.ends_with("x");
4349
| ^^^ help: try using a `char` instead: `'x'`
4450

4551
error: single-character string constant used as pattern
46-
--> $DIR/single_char_pattern.rs:23:12
52+
--> $DIR/single_char_pattern.rs:24:12
4753
|
4854
LL | x.find("x");
4955
| ^^^ help: try using a `char` instead: `'x'`
5056

5157
error: single-character string constant used as pattern
52-
--> $DIR/single_char_pattern.rs:24:13
58+
--> $DIR/single_char_pattern.rs:25:13
5359
|
5460
LL | x.rfind("x");
5561
| ^^^ help: try using a `char` instead: `'x'`
5662

5763
error: single-character string constant used as pattern
58-
--> $DIR/single_char_pattern.rs:25:14
64+
--> $DIR/single_char_pattern.rs:26:14
5965
|
6066
LL | x.rsplit("x");
6167
| ^^^ help: try using a `char` instead: `'x'`
6268

6369
error: single-character string constant used as pattern
64-
--> $DIR/single_char_pattern.rs:26:24
70+
--> $DIR/single_char_pattern.rs:27:24
6571
|
6672
LL | x.split_terminator("x");
6773
| ^^^ help: try using a `char` instead: `'x'`
6874

6975
error: single-character string constant used as pattern
70-
--> $DIR/single_char_pattern.rs:27:25
76+
--> $DIR/single_char_pattern.rs:28:25
7177
|
7278
LL | x.rsplit_terminator("x");
7379
| ^^^ help: try using a `char` instead: `'x'`
7480

7581
error: single-character string constant used as pattern
76-
--> $DIR/single_char_pattern.rs:28:17
82+
--> $DIR/single_char_pattern.rs:29:17
7783
|
7884
LL | x.splitn(2, "x");
7985
| ^^^ help: try using a `char` instead: `'x'`
8086

8187
error: single-character string constant used as pattern
82-
--> $DIR/single_char_pattern.rs:29:18
88+
--> $DIR/single_char_pattern.rs:30:18
8389
|
8490
LL | x.rsplitn(2, "x");
8591
| ^^^ help: try using a `char` instead: `'x'`
8692

8793
error: single-character string constant used as pattern
88-
--> $DIR/single_char_pattern.rs:30:15
94+
--> $DIR/single_char_pattern.rs:31:18
95+
|
96+
LL | x.split_once("x");
97+
| ^^^ help: try using a `char` instead: `'x'`
98+
99+
error: single-character string constant used as pattern
100+
--> $DIR/single_char_pattern.rs:32:19
101+
|
102+
LL | x.rsplit_once("x");
103+
| ^^^ help: try using a `char` instead: `'x'`
104+
105+
error: single-character string constant used as pattern
106+
--> $DIR/single_char_pattern.rs:33:15
89107
|
90108
LL | x.matches("x");
91109
| ^^^ help: try using a `char` instead: `'x'`
92110

93111
error: single-character string constant used as pattern
94-
--> $DIR/single_char_pattern.rs:31:16
112+
--> $DIR/single_char_pattern.rs:34:16
95113
|
96114
LL | x.rmatches("x");
97115
| ^^^ help: try using a `char` instead: `'x'`
98116

99117
error: single-character string constant used as pattern
100-
--> $DIR/single_char_pattern.rs:32:21
118+
--> $DIR/single_char_pattern.rs:35:21
101119
|
102120
LL | x.match_indices("x");
103121
| ^^^ help: try using a `char` instead: `'x'`
104122

105123
error: single-character string constant used as pattern
106-
--> $DIR/single_char_pattern.rs:33:22
124+
--> $DIR/single_char_pattern.rs:36:22
107125
|
108126
LL | x.rmatch_indices("x");
109127
| ^^^ help: try using a `char` instead: `'x'`
110128

111129
error: single-character string constant used as pattern
112-
--> $DIR/single_char_pattern.rs:34:26
130+
--> $DIR/single_char_pattern.rs:37:26
113131
|
114132
LL | x.trim_start_matches("x");
115133
| ^^^ help: try using a `char` instead: `'x'`
116134

117135
error: single-character string constant used as pattern
118-
--> $DIR/single_char_pattern.rs:35:24
136+
--> $DIR/single_char_pattern.rs:38:24
119137
|
120138
LL | x.trim_end_matches("x");
121139
| ^^^ help: try using a `char` instead: `'x'`
122140

123141
error: single-character string constant used as pattern
124-
--> $DIR/single_char_pattern.rs:36:20
142+
--> $DIR/single_char_pattern.rs:39:20
125143
|
126144
LL | x.strip_prefix("x");
127145
| ^^^ help: try using a `char` instead: `'x'`
128146

129147
error: single-character string constant used as pattern
130-
--> $DIR/single_char_pattern.rs:37:20
148+
--> $DIR/single_char_pattern.rs:40:20
131149
|
132150
LL | x.strip_suffix("x");
133151
| ^^^ help: try using a `char` instead: `'x'`
134152

135153
error: single-character string constant used as pattern
136-
--> $DIR/single_char_pattern.rs:39:13
154+
--> $DIR/single_char_pattern.rs:41:15
155+
|
156+
LL | x.replace("x", "y");
157+
| ^^^ help: try using a `char` instead: `'x'`
158+
159+
error: single-character string constant used as pattern
160+
--> $DIR/single_char_pattern.rs:42:16
161+
|
162+
LL | x.replacen("x", "y", 3);
163+
| ^^^ help: try using a `char` instead: `'x'`
164+
165+
error: single-character string constant used as pattern
166+
--> $DIR/single_char_pattern.rs:44:13
137167
|
138168
LL | x.split("/n");
139169
| ^^^^ help: try using a `char` instead: `'/n'`
140170

141171
error: single-character string constant used as pattern
142-
--> $DIR/single_char_pattern.rs:40:13
172+
--> $DIR/single_char_pattern.rs:45:13
143173
|
144174
LL | x.split("'");
145175
| ^^^ help: try using a `char` instead: `'/''`
146176

147177
error: single-character string constant used as pattern
148-
--> $DIR/single_char_pattern.rs:41:13
178+
--> $DIR/single_char_pattern.rs:46:13
149179
|
150180
LL | x.split("/'");
151181
| ^^^^ help: try using a `char` instead: `'/''`
152182

153183
error: single-character string constant used as pattern
154-
--> $DIR/single_char_pattern.rs:46:31
184+
--> $DIR/single_char_pattern.rs:51:31
155185
|
156-
LL | x.replace(";", ",").split(","); // issue #2978
186+
LL | x.replace(';', ",").split(","); // issue #2978
157187
| ^^^ help: try using a `char` instead: `','`
158188

159189
error: single-character string constant used as pattern
160-
--> $DIR/single_char_pattern.rs:47:19
190+
--> $DIR/single_char_pattern.rs:52:19
161191
|
162192
LL | x.starts_with("/x03"); // issue #2996
163193
| ^^^^^^ help: try using a `char` instead: `'/x03'`
164194

165195
error: single-character string constant used as pattern
166-
--> $DIR/single_char_pattern.rs:54:13
196+
--> $DIR/single_char_pattern.rs:59:13
167197
|
168198
LL | x.split(r"a");
169199
| ^^^^ help: try using a `char` instead: `'a'`
170200

171201
error: single-character string constant used as pattern
172-
--> $DIR/single_char_pattern.rs:55:13
202+
--> $DIR/single_char_pattern.rs:60:13
173203
|
174204
LL | x.split(r#"a"#);
175205
| ^^^^^^ help: try using a `char` instead: `'a'`
176206

177207
error: single-character string constant used as pattern
178-
--> $DIR/single_char_pattern.rs:56:13
208+
--> $DIR/single_char_pattern.rs:61:13
179209
|
180210
LL | x.split(r###"a"###);
181211
| ^^^^^^^^^^ help: try using a `char` instead: `'a'`
182212

183213
error: single-character string constant used as pattern
184-
--> $DIR/single_char_pattern.rs:57:13
214+
--> $DIR/single_char_pattern.rs:62:13
185215
|
186216
LL | x.split(r###"'"###);
187217
| ^^^^^^^^^^ help: try using a `char` instead: `'/''`
188218

189219
error: single-character string constant used as pattern
190-
--> $DIR/single_char_pattern.rs:58:13
220+
--> $DIR/single_char_pattern.rs:63:13
191221
|
192222
LL | x.split(r###"#"###);
193223
| ^^^^^^^^^^ help: try using a `char` instead: `'#'`
194224

195225
error: single-character string constant used as pattern
196-
--> $DIR/single_char_pattern.rs:60:13
226+
--> $DIR/single_char_pattern.rs:65:13
197227
|
198228
LL | x.split(r#"/"#);
199229
| ^^^^^^ help: try using a `char` instead: `'/'`
200230

201231
error: single-character string constant used as pattern
202-
--> $DIR/single_char_pattern.rs:61:13
232+
--> $DIR/single_char_pattern.rs:66:13
203233
|
204234
LL | x.split(r"/");
205235
| ^^^^ help: try using a `char` instead: `'/'`
206236

207-
error: aborting due to 34 previous errors
237+
error: aborting due to 39 previous errors
208238

0 commit comments

Comments
 (0)