Skip to content

Commit 5e3160c

Browse files
committed
Auto merge of #7156 - hellow554:single_char_strip, r=flip1995
[single_char_pattern] add strip_prefix and strip_suffix Title says it all. Adjusted ui tests. I added the second commit in case you don't like that I moved that table into `single_char_pattern.rs` directly. I don't see any reason why it shouldn't be in that file. It isn't used anywhere else. *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: add strip_prefix and strip_suffix to single_char_pattern lint
2 parents f41f380 + 19e7448 commit 5e3160c

File tree

5 files changed

+50
-33
lines changed

5 files changed

+50
-33
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,27 +2189,6 @@ const TRAIT_METHODS: [ShouldImplTraitCase; 30] = [
21892189
ShouldImplTraitCase::new("std::ops::Sub", "sub", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
21902190
];
21912191

2192-
#[rustfmt::skip]
2193-
const PATTERN_METHODS: [(&str, usize); 17] = [
2194-
("contains", 1),
2195-
("starts_with", 1),
2196-
("ends_with", 1),
2197-
("find", 1),
2198-
("rfind", 1),
2199-
("split", 1),
2200-
("rsplit", 1),
2201-
("split_terminator", 1),
2202-
("rsplit_terminator", 1),
2203-
("splitn", 2),
2204-
("rsplitn", 2),
2205-
("matches", 1),
2206-
("rmatches", 1),
2207-
("match_indices", 1),
2208-
("rmatch_indices", 1),
2209-
("trim_start_matches", 1),
2210-
("trim_end_matches", 1),
2211-
];
2212-
22132192
#[derive(Clone, Copy, PartialEq, Debug)]
22142193
enum SelfKind {
22152194
Value,

clippy_lints/src/methods/single_char_pattern.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,31 @@ use rustc_span::symbol::Symbol;
99

1010
use super::SINGLE_CHAR_PATTERN;
1111

12+
const PATTERN_METHODS: [(&str, usize); 19] = [
13+
("contains", 1),
14+
("starts_with", 1),
15+
("ends_with", 1),
16+
("find", 1),
17+
("rfind", 1),
18+
("split", 1),
19+
("rsplit", 1),
20+
("split_terminator", 1),
21+
("rsplit_terminator", 1),
22+
("splitn", 2),
23+
("rsplitn", 2),
24+
("matches", 1),
25+
("rmatches", 1),
26+
("match_indices", 1),
27+
("rmatch_indices", 1),
28+
("strip_prefix", 1),
29+
("strip_suffix", 1),
30+
("trim_start_matches", 1),
31+
("trim_end_matches", 1),
32+
];
33+
1234
/// lint for length-1 `str`s for methods in `PATTERN_METHODS`
1335
pub(super) fn check(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
14-
for &(method, pos) in &crate::methods::PATTERN_METHODS {
36+
for &(method, pos) in &PATTERN_METHODS {
1537
if_chain! {
1638
if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty_adjusted(&args[0]).kind();
1739
if *ty.kind() == ty::Str;

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)