Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3a6f59e

Browse files
committed
Document string_lit_as_bytes known problems
1 parent a5ef305 commit 3a6f59e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

clippy_lints/src/strings.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)