Skip to content

Commit e567e30

Browse files
authored
refactor(byte_char_slices): clean-up (#15985)
changelog: none
2 parents a6dfbb3 + 5107f34 commit e567e30

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

clippy_lints/src/byte_char_slices.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ declare_lint_pass!(ByteCharSlice => [BYTE_CHAR_SLICES]);
3131

3232
impl EarlyLintPass for ByteCharSlice {
3333
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
34-
if let Some(slice) = is_byte_char_slices(expr)
35-
&& !expr.span.from_expansion()
34+
if !expr.span.from_expansion()
35+
&& let Some(slice) = is_byte_char_slices(expr)
3636
{
3737
span_lint_and_sugg(
3838
cx,
@@ -47,33 +47,28 @@ impl EarlyLintPass for ByteCharSlice {
4747
}
4848
}
4949

50+
/// Checks whether the slice is that of byte chars, and if so, builds a byte-string out of it
5051
fn is_byte_char_slices(expr: &Expr) -> Option<String> {
51-
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, expr) = &expr.kind {
52-
match &expr.kind {
53-
ExprKind::Array(members) => {
54-
if members.is_empty() {
55-
return None;
56-
}
57-
58-
members
59-
.iter()
60-
.map(|member| match &member.kind {
61-
ExprKind::Lit(Lit {
62-
kind: LitKind::Byte,
63-
symbol,
64-
..
65-
}) => Some(symbol.as_str()),
66-
_ => None,
67-
})
68-
.map(|maybe_quote| match maybe_quote {
69-
Some("\"") => Some("\\\""),
70-
Some("\\'") => Some("'"),
71-
other => other,
72-
})
73-
.collect::<Option<String>>()
74-
},
75-
_ => None,
76-
}
52+
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, expr) = &expr.kind
53+
&& let ExprKind::Array(members) = &expr.kind
54+
&& !members.is_empty()
55+
{
56+
members
57+
.iter()
58+
.map(|member| match &member.kind {
59+
ExprKind::Lit(Lit {
60+
kind: LitKind::Byte,
61+
symbol,
62+
..
63+
}) => Some(symbol.as_str()),
64+
_ => None,
65+
})
66+
.map(|maybe_quote| match maybe_quote {
67+
Some("\"") => Some("\\\""),
68+
Some("\\'") => Some("'"),
69+
other => other,
70+
})
71+
.collect::<Option<String>>()
7772
} else {
7873
None
7974
}

tests/ui/byte_char_slices.fixed

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(unused)]
21
#![warn(clippy::byte_char_slices)]
32

43
fn main() {

tests/ui/byte_char_slices.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(unused)]
21
#![warn(clippy::byte_char_slices)]
32

43
fn main() {

tests/ui/byte_char_slices.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: can be more succinctly written as a byte str
2-
--> tests/ui/byte_char_slices.rs:5:15
2+
--> tests/ui/byte_char_slices.rs:4:15
33
|
44
LL | let bad = &[b'a', b'b', b'c'];
55
| ^^^^^^^^^^^^^^^^^^^ help: try: `b"abc"`
@@ -8,25 +8,25 @@ LL | let bad = &[b'a', b'b', b'c'];
88
= help: to override `-D warnings` add `#[allow(clippy::byte_char_slices)]`
99

1010
error: can be more succinctly written as a byte str
11-
--> tests/ui/byte_char_slices.rs:7:18
11+
--> tests/ui/byte_char_slices.rs:6:18
1212
|
1313
LL | let quotes = &[b'"', b'H', b'i'];
1414
| ^^^^^^^^^^^^^^^^^^^ help: try: `b"\"Hi"`
1515

1616
error: can be more succinctly written as a byte str
17-
--> tests/ui/byte_char_slices.rs:9:18
17+
--> tests/ui/byte_char_slices.rs:8:18
1818
|
1919
LL | let quotes = &[b'\'', b'S', b'u', b'p'];
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b"'Sup"`
2121

2222
error: can be more succinctly written as a byte str
23-
--> tests/ui/byte_char_slices.rs:11:19
23+
--> tests/ui/byte_char_slices.rs:10:19
2424
|
2525
LL | let escapes = &[b'\x42', b'E', b's', b'c'];
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b"\x42Esc"`
2727

2828
error: useless use of `vec!`
29-
--> tests/ui/byte_char_slices.rs:15:16
29+
--> tests/ui/byte_char_slices.rs:14:16
3030
|
3131
LL | let good = vec![b'a', b'a'];
3232
| ^^^^^^^^^^^^^^^^ help: you can use an array directly: `[b'a', b'a']`

0 commit comments

Comments
 (0)