Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions clippy_lints/src/byte_char_slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ declare_lint_pass!(ByteCharSlice => [BYTE_CHAR_SLICES]);

impl EarlyLintPass for ByteCharSlice {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if let Some(slice) = is_byte_char_slices(expr)
&& !expr.span.from_expansion()
if !expr.span.from_expansion()
&& let Some(slice) = is_byte_char_slices(expr)
{
span_lint_and_sugg(
cx,
Expand All @@ -47,33 +47,28 @@ impl EarlyLintPass for ByteCharSlice {
}
}

/// Checks whether the slice is that of byte chars, and if so, builds a byte-string out of it
fn is_byte_char_slices(expr: &Expr) -> Option<String> {
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, expr) = &expr.kind {
match &expr.kind {
ExprKind::Array(members) => {
if members.is_empty() {
return None;
}

members
.iter()
.map(|member| match &member.kind {
ExprKind::Lit(Lit {
kind: LitKind::Byte,
symbol,
..
}) => Some(symbol.as_str()),
_ => None,
})
.map(|maybe_quote| match maybe_quote {
Some("\"") => Some("\\\""),
Some("\\'") => Some("'"),
other => other,
})
.collect::<Option<String>>()
},
_ => None,
}
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, expr) = &expr.kind
&& let ExprKind::Array(members) = &expr.kind
&& !members.is_empty()
{
members
.iter()
.map(|member| match &member.kind {
ExprKind::Lit(Lit {
kind: LitKind::Byte,
symbol,
..
}) => Some(symbol.as_str()),
_ => None,
})
.map(|maybe_quote| match maybe_quote {
Some("\"") => Some("\\\""),
Some("\\'") => Some("'"),
other => other,
})
.collect::<Option<String>>()
} else {
None
}
Expand Down
1 change: 0 additions & 1 deletion tests/ui/byte_char_slices.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(unused)]
#![warn(clippy::byte_char_slices)]

fn main() {
Expand Down
1 change: 0 additions & 1 deletion tests/ui/byte_char_slices.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(unused)]
#![warn(clippy::byte_char_slices)]

fn main() {
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/byte_char_slices.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: can be more succinctly written as a byte str
--> tests/ui/byte_char_slices.rs:5:15
--> tests/ui/byte_char_slices.rs:4:15
|
LL | let bad = &[b'a', b'b', b'c'];
| ^^^^^^^^^^^^^^^^^^^ help: try: `b"abc"`
Expand All @@ -8,25 +8,25 @@ LL | let bad = &[b'a', b'b', b'c'];
= help: to override `-D warnings` add `#[allow(clippy::byte_char_slices)]`

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

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

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

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