-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
string_lit_as_bytes doesn't work correctly #1402
Comments
It's because |
This doesn't seem to be a compiler issue. Try using |
@sinkuu |
Just sharing a concrete repro for "doesn't work": fn f(_: Vec<u8>) {}
fn main() {
f("...".as_bytes().to_owned());
} Suggestion by clippy: warning: calling `as_bytes()` on a string literal
--> src/main.rs:4:7
|
4 | f("...".as_bytes().to_owned());
| ^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"..."`
|
= note: `#[warn(clippy::string_lit_as_bytes)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes So we change: - f("...".as_bytes().to_owned());
+ f(b"...".to_owned()); Resulting type error: error[E0308]: mismatched types
--> src/main.rs:4:7
|
4 | f(b"...".to_owned());
| ^^^^^^^^^^^^^^^^^
| |
| expected struct `Vec`, found array `[u8; 3]`
| help: try using a conversion method: `b"...".to_owned().to_vec()`
|
= note: expected struct `Vec<u8>`
found array `[u8; 3]` |
Downgrade string_lit_as_bytes to nursery Between #1402 (regarding `to_owned`) and #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
I'm not shure why this not work correct.
"key".as_bytes().to_owned()
=>b"key".to_owned()
for typeVec<u8>
The text was updated successfully, but these errors were encountered: