From 31c15648677cbf498e477bbcd0660dbf86723427 Mon Sep 17 00:00:00 2001 From: Elliot Bobrow Date: Wed, 24 Mar 2021 14:05:50 -0700 Subject: [PATCH] apply changes from review --- clippy_lints/src/strings.rs | 42 ++++++++++++++--------------- tests/ui/string_lit_as_bytes.fixed | 3 ++- tests/ui/string_lit_as_bytes.rs | 1 + tests/ui/string_lit_as_bytes.stderr | 16 +++++++---- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index 43947a2674d8..99ca7ef77a5c 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -286,33 +286,31 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes { } if_chain! { - if let ExprKind::MethodCall(path, _, args, _) = &e.kind; + if let ExprKind::MethodCall(path, _, [recv], _) = &e.kind; if path.ident.name == sym!(into_bytes); - if let ExprKind::MethodCall(path, _, args, _) = &args[0].kind; - if path.ident.name == sym!(to_string); - if let ExprKind::Lit(lit) = &args[0].kind; + 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 { - if lit_content.as_str().is_ascii() - && lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT - && !args[0].span.from_expansion() - { - let mut applicability = Applicability::MachineApplicable; + let mut applicability = Applicability::MachineApplicable; - span_lint_and_sugg( - cx, - STRING_LIT_AS_BYTES, - e.span, - "calling `.to_string().into_bytes()` on a string literal", - "consider using a byte string literal instead", - format!( - "b{}", - snippet_with_applicability(cx, args[0].span, r#""foo""#, &mut applicability) - ), - applicability, - ); - } + 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, + ); } } } diff --git a/tests/ui/string_lit_as_bytes.fixed b/tests/ui/string_lit_as_bytes.fixed index a141d649d3be..dd22bfa5c53e 100644 --- a/tests/ui/string_lit_as_bytes.fixed +++ b/tests/ui/string_lit_as_bytes.fixed @@ -8,7 +8,8 @@ fn str_lit_as_bytes() { let bs = br###"raw string with 3# plus " ""###; - let bs = b"lit to string"; + 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(); diff --git a/tests/ui/string_lit_as_bytes.rs b/tests/ui/string_lit_as_bytes.rs index 95e79a1e253e..d2a710ed6b8c 100644 --- a/tests/ui/string_lit_as_bytes.rs +++ b/tests/ui/string_lit_as_bytes.rs @@ -9,6 +9,7 @@ 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(); diff --git a/tests/ui/string_lit_as_bytes.stderr b/tests/ui/string_lit_as_bytes.stderr index 28064e28af86..e0ddb070b504 100644 --- a/tests/ui/string_lit_as_bytes.stderr +++ b/tests/ui/string_lit_as_bytes.stderr @@ -12,23 +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 `.to_string().into_bytes()` on a string literal +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"` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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:24: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:26: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 5 previous errors +error: aborting due to 6 previous errors