Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid reporting string_lit_as_bytes for long strings #4233

Merged
merged 2 commits into from
Jul 9, 2019
Merged
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
4 changes: 4 additions & 0 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ fn is_add(cx: &LateContext<'_, '_>, src: &Expr, target: &Expr) -> bool {
}
}

// Max length a b"foo" string can take
const MAX_LENGTH_BYTE_STRING_LIT: usize = 32;

declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES]);

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
Expand Down Expand Up @@ -173,6 +176,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
);
} else if callsite == expanded
&& lit_content.as_str().chars().all(|c| c.is_ascii())
&& lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT
&& !in_macro_or_desugar(args[0].span)
{
span_lint_and_sugg(
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/string_lit_as_bytes.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
fn str_lit_as_bytes() {
let bs = b"hello there";

let bs = br###"raw string with three ### in it and some " ""###;
let bs = br###"raw string with 3# plus " ""###;

// no warning, because this cannot be written as a byte string literal:
// no warning, because these cannot be written as byte string literals:
let ubs = "☃".as_bytes();
let ubs = "hello there! this is a very long string".as_bytes();

let strify = stringify!(foobar).as_bytes();

Expand Down
5 changes: 3 additions & 2 deletions tests/ui/string_lit_as_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
fn str_lit_as_bytes() {
let bs = "hello there".as_bytes();

let bs = r###"raw string with three ### in it and some " ""###.as_bytes();
let bs = r###"raw string with 3# plus " ""###.as_bytes();

// no warning, because this cannot be written as a byte string literal:
// no warning, because these cannot be written as byte string literals:
let ubs = "☃".as_bytes();
let ubs = "hello there! this is a very long string".as_bytes();

let strify = stringify!(foobar).as_bytes();

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/string_lit_as_bytes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ LL | let bs = "hello there".as_bytes();
error: calling `as_bytes()` on a string literal
--> $DIR/string_lit_as_bytes.rs:9:14
|
LL | let bs = r###"raw string with three ### in it and some " ""###.as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with three ### in it and some " ""###`
LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with 3# plus " ""###`

error: calling `as_bytes()` on `include_str!(..)`
--> $DIR/string_lit_as_bytes.rs:16:22
--> $DIR/string_lit_as_bytes.rs:17:22
|
LL | let includestr = include_str!("entry.rs").as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry.rs")`
Expand Down