Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downgrade string_lit_as_bytes to nursery #6117

Merged
merged 3 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
LintId::of(&slow_vector_initialization::SLOW_VECTOR_INITIALIZATION),
LintId::of(&stable_sort_primitive::STABLE_SORT_PRIMITIVE),
LintId::of(&strings::STRING_LIT_AS_BYTES),
LintId::of(&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL),
LintId::of(&suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL),
LintId::of(&swap::ALMOST_SWAPPED),
Expand Down Expand Up @@ -1618,7 +1617,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&returns::LET_AND_RETURN),
LintId::of(&returns::NEEDLESS_RETURN),
LintId::of(&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
LintId::of(&strings::STRING_LIT_AS_BYTES),
LintId::of(&tabs_in_doc_comments::TABS_IN_DOC_COMMENTS),
LintId::of(&to_digit_is_some::TO_DIGIT_IS_SOME),
LintId::of(&try_err::TRY_ERR),
Expand Down Expand Up @@ -1831,6 +1829,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&needless_borrow::NEEDLESS_BORROW),
LintId::of(&path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE),
LintId::of(&redundant_pub_crate::REDUNDANT_PUB_CRATE),
LintId::of(&strings::STRING_LIT_AS_BYTES),
LintId::of(&transmute::USELESS_TRANSMUTE),
LintId::of(&use_self::USE_SELF),
]);
Expand Down
24 changes: 22 additions & 2 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
/// ```compile_fail
/// 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
Expand All @@ -80,7 +100,7 @@ declare_clippy_lint! {
/// let bs = b"a byte string";
/// ```
pub STRING_LIT_AS_BYTES,
style,
nursery,
"calling `as_bytes` on a string literal instead of using a byte string literal"
}

Expand Down
2 changes: 1 addition & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
},
Lint {
name: "string_lit_as_bytes",
group: "style",
group: "nursery",
desc: "calling `as_bytes` on a string literal instead of using a byte string literal",
deprecation: None,
module: "strings",
Expand Down