Avoid reporting string_lit_as_bytes for long strings

Port of @jens1o code ([b76f939][jens1o_commit])

Fixes #1208

[jens1o_commit]: b76f939ac2

Co-authored-by: Thiago Arrais <thiago.arrais@gmail.com>
This commit is contained in:
Jens Hausdorf 2019-03-10 11:06:19 +01:00 committed by Thiago Arrais
parent 316da7eb41
commit 6d1aaac6c3
4 changed files with 10 additions and 7 deletions

View File

@ -173,6 +173,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
); );
} else if callsite == expanded } else if callsite == expanded
&& lit_content.as_str().chars().all(|c| c.is_ascii()) && lit_content.as_str().chars().all(|c| c.is_ascii())
&& lit_content.as_str().len() <= 32
&& !in_macro_or_desugar(args[0].span) && !in_macro_or_desugar(args[0].span)
{ {
span_lint_and_sugg( span_lint_and_sugg(

View File

@ -6,10 +6,11 @@
fn str_lit_as_bytes() { fn str_lit_as_bytes() {
let bs = b"hello there"; 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 = "".as_bytes();
let ubs = "hello there! this is a very long string".as_bytes();
let strify = stringify!(foobar).as_bytes(); let strify = stringify!(foobar).as_bytes();

View File

@ -6,10 +6,11 @@
fn str_lit_as_bytes() { fn str_lit_as_bytes() {
let bs = "hello there".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 = "".as_bytes();
let ubs = "hello there! this is a very long string".as_bytes();
let strify = stringify!(foobar).as_bytes(); let strify = stringify!(foobar).as_bytes();

View File

@ -9,11 +9,11 @@ LL | let bs = "hello there".as_bytes();
error: calling `as_bytes()` on a string literal error: calling `as_bytes()` on a string literal
--> $DIR/string_lit_as_bytes.rs:9:14 --> $DIR/string_lit_as_bytes.rs:9:14
| |
LL | let bs = r###"raw string with three ### in it and some " ""###.as_bytes(); LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with three ### in it and some " ""###` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with 3# plus " ""###`
error: calling `as_bytes()` on `include_str!(..)` 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(); LL | let includestr = include_str!("entry.rs").as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry.rs")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry.rs")`