From 6d1aaac6c373e4781f779bddf992242abb53c7c3 Mon Sep 17 00:00:00 2001 From: Jens Hausdorf <mail@jens-hausdorf.de> Date: Sun, 10 Mar 2019 11:06:19 +0100 Subject: [PATCH 1/2] Avoid reporting string_lit_as_bytes for long strings Port of @jens1o code ([b76f939][jens1o_commit]) Fixes #1208 [jens1o_commit]: https://github.com/jens1o/rust-clippy/commit/b76f939ac2efcfe24900c286b3b7713d972d9088 Co-authored-by: Thiago Arrais <thiago.arrais@gmail.com> --- clippy_lints/src/strings.rs | 1 + tests/ui/string_lit_as_bytes.fixed | 5 +++-- tests/ui/string_lit_as_bytes.rs | 5 +++-- tests/ui/string_lit_as_bytes.stderr | 6 +++--- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index eae464ee249..9be2d40bae1 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -173,6 +173,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() <= 32 && !in_macro_or_desugar(args[0].span) { span_lint_and_sugg( diff --git a/tests/ui/string_lit_as_bytes.fixed b/tests/ui/string_lit_as_bytes.fixed index a70504656d9..1922478165f 100644 --- a/tests/ui/string_lit_as_bytes.fixed +++ b/tests/ui/string_lit_as_bytes.fixed @@ -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(); diff --git a/tests/ui/string_lit_as_bytes.rs b/tests/ui/string_lit_as_bytes.rs index ea8c712854b..560cbcb657b 100644 --- a/tests/ui/string_lit_as_bytes.rs +++ b/tests/ui/string_lit_as_bytes.rs @@ -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(); diff --git a/tests/ui/string_lit_as_bytes.stderr b/tests/ui/string_lit_as_bytes.stderr index f51cd71a6dc..59aaec75bd2 100644 --- a/tests/ui/string_lit_as_bytes.stderr +++ b/tests/ui/string_lit_as_bytes.stderr @@ -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")` From 7d1a9447ea15e7b7ce845876c8850f0feed54185 Mon Sep 17 00:00:00 2001 From: Thiago Arrais <thiago.arrais@gmail.com> Date: Mon, 8 Jul 2019 13:12:51 -0300 Subject: [PATCH 2/2] Extract semantic constant --- clippy_lints/src/strings.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index 9be2d40bae1..57f63a600a7 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -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 { @@ -173,7 +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() <= 32 + && lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT && !in_macro_or_desugar(args[0].span) { span_lint_and_sugg(