Skip to content

Commit 265e484

Browse files
committed
Auto merge of rust-lang#6117 - dtolnay:string_lit_as_bytes, r=ebroto
Downgrade string_lit_as_bytes to nursery Between rust-lang#1402 (regarding `to_owned`) and rust-lang#4494 (regarding `impl Read`), as well as other confusion I've seen hit in my work codebase involving string_lit_as_bytes (`"...".as_bytes().into()`), I don't think this lint is at a quality to be enabled by default. I would consider re-enabling this lint after it is updated to understand when the surrounding type information is sufficient to unsize `b"..."` to &\[u8\] without causing a type error. As currently implemented, this lint is pushing people to write `&b"_"[..]` which is not an improvement over `"_".as_bytes()` as far as I am concerned. --- changelog: Remove string_lit_as_bytes from default set of enabled lints
2 parents 6bfc19c + c81bea4 commit 265e484

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

clippy_lints/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14771477
LintId::of(&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
14781478
LintId::of(&slow_vector_initialization::SLOW_VECTOR_INITIALIZATION),
14791479
LintId::of(&stable_sort_primitive::STABLE_SORT_PRIMITIVE),
1480-
LintId::of(&strings::STRING_LIT_AS_BYTES),
14811480
LintId::of(&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL),
14821481
LintId::of(&suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL),
14831482
LintId::of(&swap::ALMOST_SWAPPED),
@@ -1619,7 +1618,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16191618
LintId::of(&returns::LET_AND_RETURN),
16201619
LintId::of(&returns::NEEDLESS_RETURN),
16211620
LintId::of(&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
1622-
LintId::of(&strings::STRING_LIT_AS_BYTES),
16231621
LintId::of(&tabs_in_doc_comments::TABS_IN_DOC_COMMENTS),
16241622
LintId::of(&to_digit_is_some::TO_DIGIT_IS_SOME),
16251623
LintId::of(&try_err::TRY_ERR),
@@ -1831,6 +1829,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18311829
LintId::of(&needless_borrow::NEEDLESS_BORROW),
18321830
LintId::of(&path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE),
18331831
LintId::of(&redundant_pub_crate::REDUNDANT_PUB_CRATE),
1832+
LintId::of(&strings::STRING_LIT_AS_BYTES),
18341833
LintId::of(&transmute::USELESS_TRANSMUTE),
18351834
LintId::of(&use_self::USE_SELF),
18361835
]);

clippy_lints/src/strings.rs

+22-2
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+
/// ```compile_fail
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
@@ -80,7 +100,7 @@ declare_clippy_lint! {
80100
/// let bs = b"a byte string";
81101
/// ```
82102
pub STRING_LIT_AS_BYTES,
83-
style,
103+
nursery,
84104
"calling `as_bytes` on a string literal instead of using a byte string literal"
85105
}
86106

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
21612161
},
21622162
Lint {
21632163
name: "string_lit_as_bytes",
2164-
group: "style",
2164+
group: "nursery",
21652165
desc: "calling `as_bytes` on a string literal instead of using a byte string literal",
21662166
deprecation: None,
21672167
module: "strings",

0 commit comments

Comments
 (0)