Skip to content

Commit 981ffa7

Browse files
committed
Auto merge of rust-lang#6959 - ebobrow:iss-6927-fix, r=camsteffen
Check for `.to_string().into_bytes()` in string_lit_to_bytes fixes rust-lang#6927 changelog: Add a check for `into_bytes()` to string_lit_to_bytes lint
2 parents 4a1825a + e9ebc27 commit 981ffa7

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

clippy_lints/src/strings.rs

+29
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,35 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
284284
}
285285
}
286286
}
287+
288+
if_chain! {
289+
if let ExprKind::MethodCall(path, _, [recv], _) = &e.kind;
290+
if path.ident.name == sym!(into_bytes);
291+
if let ExprKind::MethodCall(path, _, [recv], _) = &recv.kind;
292+
if matches!(&*path.ident.name.as_str(), "to_owned" | "to_string");
293+
if let ExprKind::Lit(lit) = &recv.kind;
294+
if let LitKind::Str(lit_content, _) = &lit.node;
295+
296+
if lit_content.as_str().is_ascii();
297+
if lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT;
298+
if !recv.span.from_expansion();
299+
then {
300+
let mut applicability = Applicability::MachineApplicable;
301+
302+
span_lint_and_sugg(
303+
cx,
304+
STRING_LIT_AS_BYTES,
305+
e.span,
306+
"calling `into_bytes()` on a string literal",
307+
"consider using a byte string literal instead",
308+
format!(
309+
"b{}.to_vec()",
310+
snippet_with_applicability(cx, recv.span, r#""..""#, &mut applicability)
311+
),
312+
applicability,
313+
);
314+
}
315+
}
287316
}
288317
}
289318

tests/ui/string_lit_as_bytes.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ fn str_lit_as_bytes() {
88

99
let bs = br###"raw string with 3# plus " ""###;
1010

11+
let bs = b"lit to string".to_vec();
12+
let bs = b"lit to owned".to_vec();
13+
1114
// no warning, because these cannot be written as byte string literals:
1215
let ubs = "☃".as_bytes();
1316
let ubs = "hello there! this is a very long string".as_bytes();
1417

18+
let ubs = "☃".to_string().into_bytes();
19+
let ubs = "this is also too long and shouldn't be fixed".to_string().into_bytes();
20+
1521
let strify = stringify!(foobar).as_bytes();
1622

1723
let current_version = env!("CARGO_PKG_VERSION").as_bytes();

tests/ui/string_lit_as_bytes.rs

+6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ fn str_lit_as_bytes() {
88

99
let bs = r###"raw string with 3# plus " ""###.as_bytes();
1010

11+
let bs = "lit to string".to_string().into_bytes();
12+
let bs = "lit to owned".to_owned().into_bytes();
13+
1114
// no warning, because these cannot be written as byte string literals:
1215
let ubs = "☃".as_bytes();
1316
let ubs = "hello there! this is a very long string".as_bytes();
1417

18+
let ubs = "☃".to_string().into_bytes();
19+
let ubs = "this is also too long and shouldn't be fixed".to_string().into_bytes();
20+
1521
let strify = stringify!(foobar).as_bytes();
1622

1723
let current_version = env!("CARGO_PKG_VERSION").as_bytes();

tests/ui/string_lit_as_bytes.stderr

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,29 @@ error: calling `as_bytes()` on a string literal
1212
LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with 3# plus " ""###`
1414

15+
error: calling `into_bytes()` on a string literal
16+
--> $DIR/string_lit_as_bytes.rs:11:14
17+
|
18+
LL | let bs = "lit to string".to_string().into_bytes();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"lit to string".to_vec()`
20+
21+
error: calling `into_bytes()` on a string literal
22+
--> $DIR/string_lit_as_bytes.rs:12:14
23+
|
24+
LL | let bs = "lit to owned".to_owned().into_bytes();
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"lit to owned".to_vec()`
26+
1527
error: calling `as_bytes()` on `include_str!(..)`
16-
--> $DIR/string_lit_as_bytes.rs:19:22
28+
--> $DIR/string_lit_as_bytes.rs:25:22
1729
|
1830
LL | let includestr = include_str!("entry_unfixable.rs").as_bytes();
1931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry_unfixable.rs")`
2032

2133
error: calling `as_bytes()` on a string literal
22-
--> $DIR/string_lit_as_bytes.rs:21:13
34+
--> $DIR/string_lit_as_bytes.rs:27:13
2335
|
2436
LL | let _ = "string with newline/t/n".as_bytes();
2537
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"string with newline/t/n"`
2638

27-
error: aborting due to 4 previous errors
39+
error: aborting due to 6 previous errors
2840

0 commit comments

Comments
 (0)