Skip to content

Commit 6d1aaac

Browse files
jens1othiagoarrais
andcommitted
Avoid reporting string_lit_as_bytes for long strings
Port of @jens1o code ([b76f939][jens1o_commit]) Fixes #1208 [jens1o_commit]: jens1o@b76f939 Co-authored-by: Thiago Arrais <[email protected]>
1 parent 316da7e commit 6d1aaac

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

clippy_lints/src/strings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
173173
);
174174
} else if callsite == expanded
175175
&& lit_content.as_str().chars().all(|c| c.is_ascii())
176+
&& lit_content.as_str().len() <= 32
176177
&& !in_macro_or_desugar(args[0].span)
177178
{
178179
span_lint_and_sugg(

tests/ui/string_lit_as_bytes.fixed

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
fn str_lit_as_bytes() {
77
let bs = b"hello there";
88

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

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

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

tests/ui/string_lit_as_bytes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
fn str_lit_as_bytes() {
77
let bs = "hello there".as_bytes();
88

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

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

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

tests/ui/string_lit_as_bytes.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ LL | let bs = "hello there".as_bytes();
99
error: calling `as_bytes()` on a string literal
1010
--> $DIR/string_lit_as_bytes.rs:9:14
1111
|
12-
LL | let bs = r###"raw string with three ### in it and some " ""###.as_bytes();
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with three ### in it and some " ""###`
12+
LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with 3# plus " ""###`
1414

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

0 commit comments

Comments
 (0)