-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds a new lint that checks for potentially harder to read byte char slices: `&[b'a', b'b']` and suggests to replace them with the easier to read `b"ab"` form. Signed-Off-By: Marcel Müller <[email protected]> Co-authored-by: Matthias Beyer <[email protected]> Use iterator to skip validation Signed-off-by: Marcel Müller <[email protected]> Suggested-by: Alex Macleod <[email protected]> Convert quote escapes to proper form Signed-off-by: Marcel Müller <[email protected]> Add more convertable test cases Signed-off-by: Marcel Müller <[email protected]>
- Loading branch information
Showing
7 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use rustc_ast::ast::{BorrowKind, Expr, ExprKind, Mutability}; | ||
use rustc_ast::token::{Lit, LitKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for hard to read slices of byte characters, that could be more easily expressed as a | ||
/// byte string. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// Potentially makes the string harder to read. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// &[b'H', b'e', b'l', b'l', b'o'] | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// b"Hello" | ||
/// ``` | ||
#[clippy::version = "1.68.0"] | ||
pub BYTE_CHAR_SLICE, | ||
style, | ||
"hard to read byte char slice" | ||
} | ||
declare_lint_pass!(ByteCharSlice => [BYTE_CHAR_SLICE]); | ||
|
||
impl EarlyLintPass for ByteCharSlice { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
if let Some(slice) = is_byte_char_slice(expr) | ||
&& !expr.span.from_expansion() | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
BYTE_CHAR_SLICE, | ||
expr.span, | ||
"can be more succinctly written as a byte str", | ||
"try", | ||
format!("b\"{slice}\""), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn is_byte_char_slice(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, | ||
} | ||
} else { | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::byte_char_slice)] | ||
|
||
fn main() { | ||
let bad = b"abc"; | ||
let quotes = b"\"Hi"; | ||
let quotes = b"'Sup"; | ||
let escapes = b"\x42Esc"; | ||
|
||
let good = &[b'a', 0x42]; | ||
let good = [b'a', b'a']; | ||
let good: u8 = [b'a', b'c'].into_iter().sum(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::byte_char_slice)] | ||
|
||
fn main() { | ||
let bad = &[b'a', b'b', b'c']; | ||
let quotes = &[b'"', b'H', b'i']; | ||
let quotes = &[b'\'', b'S', b'u', b'p']; | ||
let escapes = &[b'\x42', b'E', b's', b'c']; | ||
|
||
let good = &[b'a', 0x42]; | ||
let good = vec![b'a', b'a']; | ||
let good: u8 = [b'a', b'c'].into_iter().sum(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
error: can be more succinctly written as a byte str | ||
--> tests/ui/byte_char_slice.rs:5:15 | ||
| | ||
LL | let bad = &[b'a', b'b', b'c']; | ||
| ^^^^^^^^^^^^^^^^^^^ help: try: `b"abc"` | ||
| | ||
= note: `-D clippy::byte-char-slice` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::byte_char_slice)]` | ||
|
||
error: can be more succinctly written as a byte str | ||
--> tests/ui/byte_char_slice.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_slice.rs:7: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_slice.rs:8: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_slice.rs:11:16 | ||
| | ||
LL | let good = vec![b'a', b'a']; | ||
| ^^^^^^^^^^^^^^^^ help: you can use an array directly: `[b'a', b'a']` | ||
| | ||
= note: `-D clippy::useless-vec` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::useless_vec)]` | ||
|
||
error: aborting due to 5 previous errors | ||
|