Skip to content

Check for .to_string().into_bytes() in string_lit_to_bytes #6959

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

Merged
merged 1 commit into from
Mar 25, 2021
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
29 changes: 29 additions & 0 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,35 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
}
}
}

if_chain! {
if let ExprKind::MethodCall(path, _, [recv], _) = &e.kind;
if path.ident.name == sym!(into_bytes);
if let ExprKind::MethodCall(path, _, [recv], _) = &recv.kind;
if matches!(&*path.ident.name.as_str(), "to_owned" | "to_string");
if let ExprKind::Lit(lit) = &recv.kind;
if let LitKind::Str(lit_content, _) = &lit.node;

if lit_content.as_str().is_ascii();
if lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT;
if !recv.span.from_expansion();
then {
let mut applicability = Applicability::MachineApplicable;

span_lint_and_sugg(
cx,
STRING_LIT_AS_BYTES,
e.span,
"calling `into_bytes()` on a string literal",
"consider using a byte string literal instead",
format!(
"b{}.to_vec()",
snippet_with_applicability(cx, recv.span, r#""..""#, &mut applicability)
),
applicability,
);
}
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/string_lit_as_bytes.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ fn str_lit_as_bytes() {

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

let bs = b"lit to string".to_vec();
let bs = b"lit to owned".to_vec();

// 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 ubs = "☃".to_string().into_bytes();
let ubs = "this is also too long and shouldn't be fixed".to_string().into_bytes();

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

let current_version = env!("CARGO_PKG_VERSION").as_bytes();
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/string_lit_as_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ fn str_lit_as_bytes() {

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

let bs = "lit to string".to_string().into_bytes();
let bs = "lit to owned".to_owned().into_bytes();

// 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 ubs = "☃".to_string().into_bytes();
let ubs = "this is also too long and shouldn't be fixed".to_string().into_bytes();

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

let current_version = env!("CARGO_PKG_VERSION").as_bytes();
Expand Down
18 changes: 15 additions & 3 deletions tests/ui/string_lit_as_bytes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,29 @@ error: calling `as_bytes()` on a string literal
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 `into_bytes()` on a string literal
--> $DIR/string_lit_as_bytes.rs:11:14
|
LL | let bs = "lit to string".to_string().into_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"lit to string".to_vec()`

error: calling `into_bytes()` on a string literal
--> $DIR/string_lit_as_bytes.rs:12:14
|
LL | let bs = "lit to owned".to_owned().into_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"lit to owned".to_vec()`

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

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

error: aborting due to 4 previous errors
error: aborting due to 6 previous errors