about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2020-10-08 04:50:24 -0700
committerDavid Tolnay <dtolnay@gmail.com>2020-10-08 04:50:24 -0700
commit3a6f59ecae90892c3162df95d9633c4263ab91fa (patch)
tree8284abb7706d200e5163917e77d4a460c99cd84c
parenta5ef305cb5a10437963ecda82a833b6932dc70d1 (diff)
downloadrust-3a6f59ecae90892c3162df95d9633c4263ab91fa.tar.gz
rust-3a6f59ecae90892c3162df95d9633c4263ab91fa.zip
Document string_lit_as_bytes known problems
-rw-r--r--clippy_lints/src/strings.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs
index 203af06a734..415c07b2261 100644
--- a/clippy_lints/src/strings.rs
+++ b/clippy_lints/src/strings.rs
@@ -69,7 +69,27 @@ declare_clippy_lint! {
     /// **Why is this bad?** Byte string literals (e.g., `b"foo"`) can be used
     /// instead. They are shorter but less discoverable than `as_bytes()`.
     ///
-    /// **Known Problems:** None.
+    /// **Known Problems:**
+    /// `"str".as_bytes()` and the suggested replacement of `b"str"` are not
+    /// equivalent because they have different types. The former is `&[u8]`
+    /// while the latter is `&[u8; 3]`. That means in general they will have a
+    /// different set of methods and different trait implementations.
+    ///
+    /// ```rust
+    /// fn f(v: Vec<u8>) {}
+    ///
+    /// f("...".as_bytes().to_owned()); // works
+    /// f(b"...".to_owned()); // does not work, because arg is [u8; 3] not Vec<u8>
+    ///
+    /// fn g(r: impl std::io::Read) {}
+    ///
+    /// g("...".as_bytes()); // works
+    /// g(b"..."); // does not work
+    /// ```
+    ///
+    /// The actual equivalent of `"str".as_bytes()` with the same type is not
+    /// `b"str"` but `&b"str"[..]`, which is a great deal of punctuation and not
+    /// more readable than a function call.
     ///
     /// **Example:**
     /// ```rust