File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -69,7 +69,27 @@ declare_clippy_lint! {
6969 /// **Why is this bad?** Byte string literals (e.g., `b"foo"`) can be used
7070 /// instead. They are shorter but less discoverable than `as_bytes()`.
7171 ///
72- /// **Known Problems:** None.
72+ /// **Known Problems:**
73+ /// `"str".as_bytes()` and the suggested replacement of `b"str"` are not
74+ /// equivalent because they have different types. The former is `&[u8]`
75+ /// while the latter is `&[u8; 3]`. That means in general they will have a
76+ /// different set of methods and different trait implementations.
77+ ///
78+ /// ```rust
79+ /// fn f(v: Vec<u8>) {}
80+ ///
81+ /// f("...".as_bytes().to_owned()); // works
82+ /// f(b"...".to_owned()); // does not work, because arg is [u8; 3] not Vec<u8>
83+ ///
84+ /// fn g(r: impl std::io::Read) {}
85+ ///
86+ /// g("...".as_bytes()); // works
87+ /// g(b"..."); // does not work
88+ /// ```
89+ ///
90+ /// The actual equivalent of `"str".as_bytes()` with the same type is not
91+ /// `b"str"` but `&b"str"[..]`, which is a great deal of punctuation and not
92+ /// more readable than a function call.
7393 ///
7494 /// **Example:**
7595 /// ```rust
You can’t perform that action at this time.
0 commit comments