Skip to content

Commit c080899

Browse files
committed
add strip_{prefix,suffix} to PATTERN_METHODS
this will warn, if a single_char_pattern is used in one of the above methods
1 parent 019dfb9 commit c080899

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ const TRAIT_METHODS: [ShouldImplTraitCase; 30] = [
21902190
];
21912191

21922192
#[rustfmt::skip]
2193-
const PATTERN_METHODS: [(&str, usize); 17] = [
2193+
const PATTERN_METHODS: [(&str, usize); 19] = [
21942194
("contains", 1),
21952195
("starts_with", 1),
21962196
("ends_with", 1),
@@ -2206,6 +2206,8 @@ const PATTERN_METHODS: [(&str, usize); 17] = [
22062206
("rmatches", 1),
22072207
("match_indices", 1),
22082208
("rmatch_indices", 1),
2209+
("strip_prefix", 1),
2210+
("strip_suffix", 1),
22092211
("trim_start_matches", 1),
22102212
("trim_end_matches", 1),
22112213
];

tests/ui/single_char_pattern.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ fn main() {
3333
x.rmatch_indices('x');
3434
x.trim_start_matches('x');
3535
x.trim_end_matches('x');
36+
x.strip_prefix('x');
37+
x.strip_suffix('x');
3638
// Make sure we escape characters correctly.
3739
x.split('\n');
3840
x.split('\'');

tests/ui/single_char_pattern.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ fn main() {
3333
x.rmatch_indices("x");
3434
x.trim_start_matches("x");
3535
x.trim_end_matches("x");
36+
x.strip_prefix("x");
37+
x.strip_suffix("x");
3638
// Make sure we escape characters correctly.
3739
x.split("\n");
3840
x.split("'");

tests/ui/single_char_pattern.stderr

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,64 +121,76 @@ LL | x.trim_end_matches("x");
121121
| ^^^ help: try using a `char` instead: `'x'`
122122

123123
error: single-character string constant used as pattern
124-
--> $DIR/single_char_pattern.rs:37:13
124+
--> $DIR/single_char_pattern.rs:36:20
125+
|
126+
LL | x.strip_prefix("x");
127+
| ^^^ help: try using a `char` instead: `'x'`
128+
129+
error: single-character string constant used as pattern
130+
--> $DIR/single_char_pattern.rs:37:20
131+
|
132+
LL | x.strip_suffix("x");
133+
| ^^^ help: try using a `char` instead: `'x'`
134+
135+
error: single-character string constant used as pattern
136+
--> $DIR/single_char_pattern.rs:39:13
125137
|
126138
LL | x.split("/n");
127139
| ^^^^ help: try using a `char` instead: `'/n'`
128140

129141
error: single-character string constant used as pattern
130-
--> $DIR/single_char_pattern.rs:38:13
142+
--> $DIR/single_char_pattern.rs:40:13
131143
|
132144
LL | x.split("'");
133145
| ^^^ help: try using a `char` instead: `'/''`
134146

135147
error: single-character string constant used as pattern
136-
--> $DIR/single_char_pattern.rs:39:13
148+
--> $DIR/single_char_pattern.rs:41:13
137149
|
138150
LL | x.split("/'");
139151
| ^^^^ help: try using a `char` instead: `'/''`
140152

141153
error: single-character string constant used as pattern
142-
--> $DIR/single_char_pattern.rs:44:31
154+
--> $DIR/single_char_pattern.rs:46:31
143155
|
144156
LL | x.replace(";", ",").split(","); // issue #2978
145157
| ^^^ help: try using a `char` instead: `','`
146158

147159
error: single-character string constant used as pattern
148-
--> $DIR/single_char_pattern.rs:45:19
160+
--> $DIR/single_char_pattern.rs:47:19
149161
|
150162
LL | x.starts_with("/x03"); // issue #2996
151163
| ^^^^^^ help: try using a `char` instead: `'/x03'`
152164

153165
error: single-character string constant used as pattern
154-
--> $DIR/single_char_pattern.rs:52:13
166+
--> $DIR/single_char_pattern.rs:54:13
155167
|
156168
LL | x.split(r"a");
157169
| ^^^^ help: try using a `char` instead: `'a'`
158170

159171
error: single-character string constant used as pattern
160-
--> $DIR/single_char_pattern.rs:53:13
172+
--> $DIR/single_char_pattern.rs:55:13
161173
|
162174
LL | x.split(r#"a"#);
163175
| ^^^^^^ help: try using a `char` instead: `'a'`
164176

165177
error: single-character string constant used as pattern
166-
--> $DIR/single_char_pattern.rs:54:13
178+
--> $DIR/single_char_pattern.rs:56:13
167179
|
168180
LL | x.split(r###"a"###);
169181
| ^^^^^^^^^^ help: try using a `char` instead: `'a'`
170182

171183
error: single-character string constant used as pattern
172-
--> $DIR/single_char_pattern.rs:55:13
184+
--> $DIR/single_char_pattern.rs:57:13
173185
|
174186
LL | x.split(r###"'"###);
175187
| ^^^^^^^^^^ help: try using a `char` instead: `'/''`
176188

177189
error: single-character string constant used as pattern
178-
--> $DIR/single_char_pattern.rs:56:13
190+
--> $DIR/single_char_pattern.rs:58:13
179191
|
180192
LL | x.split(r###"#"###);
181193
| ^^^^^^^^^^ help: try using a `char` instead: `'#'`
182194

183-
error: aborting due to 30 previous errors
195+
error: aborting due to 32 previous errors
184196

0 commit comments

Comments
 (0)