-
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
Lint string_lit_as_bytes should not be reported for string literal of length 33 and more #1208
Comments
fn foo(a: &[u8]) {
println!("{}", a.len());
}
fn main() {
foo(b"azertyuiopqsdfghjklmwxcvbnazertyuiopqsdfghjklmwxcvbn");
} prints 52. |
@mcarton While the following: fn foo<A: AsRef<[u8]>>(a: A) {
println!("{}", a.as_ref().len());
}
fn main() {
foo(b"azertyuiopqsdfghjklmwxcvbnazertyuiopqsdfghjklmwxcvbn");
} gives a the above compiler error. And using: foo("azertyuiopqsdfghjklmwxcvbnazertyuiopqsdfghjklmwxcvbn".as_bytes()); triggers the |
Ah, I only looked at |
I am not sure it is related to the size of the string. let cstring = CString::new("Hello".as_bytes()).unwrap(); because using
|
I'm also affected by this problem. Is there somewhere I could start contributing to fix this? |
I believe all that is needed is an rust-clippy/clippy_lints/src/strings.rs Lines 173 to 211 in 15d1731
Also a rustfix test ensuring that everything works correctly should be added so we don't regress this. |
I'd like to take this! :) |
@oli-obk Before opening a PR: I wonder what you exactly mean with a rustfix test? I've removed the test expectations and any error that should be added should change the stderr, shouldn't it? |
Adding This allows us to detect cases like this one, since the suggested change doesn't compile. |
@jens1o I believe you forgot to open a PR with your fix? |
Yeah. I don't know whether there is missing something. If so, please, @phansch, take it over, as I'll be unavailable over the next week. :) |
@jens1o: are you working on this? I'm interested in porting this to master. |
@thiagoarrais It seems you can take this issue. :) |
Port of @jens1o code ([b76f939][jens1o_commit]) Fixes rust-lang#1208 [jens1o_commit]: jens1o@b76f939 Co-authored-by: Thiago Arrais <[email protected]>
Port of @jens1o code ([b76f939][jens1o_commit]) Fixes rust-lang#1208 [jens1o_commit]: jens1o@b76f939 Co-authored-by: Thiago Arrais <[email protected]>
Avoid reporting string_lit_as_bytes for long strings Port of @jens1o code ([b76f939][jens1o_commit]) Fixes #1208 [jens1o_commit]: jens1o@b76f939 <!-- Thank you for making Clippy better! We're collecting our changelog from pull request descriptions. If your PR only updates to the latest nightly, you can leave the `changelog` entry as `none`. Otherwise, please write a short comment explaining your change. If your PR fixes an issue, you can add "fixes #issue_number" into this PR description. This way the issue will be automatically closed when your PR is merged. If you added a new lint, here's a checklist for things that will be checked during review or continuous integration. - [ ] Followed [lint naming conventions][lint_naming] - [ ] Added passing UI tests (including committed `.stderr` file) - [ ] `cargo test` passes locally - [ ] Executed `util/dev update_lints` - [ ] Added lint documentation - [ ] Run `cargo fmt` Note that you can skip the above if you are just opening a WIP PR in order to get feedback. Delete this line and everything above before opening your PR --> changelog: bugfix for long strings as bytes
Hello.
The lint
string_lit_as_bytes
reports a warning even if the string literal has a length of 33 or more.However, the
AsRef<[T]>
for array is only defined for array of length up to 32.A code example triggering a false positive is here.
More specifically, it is this code:
Since the string has a length of 43, the
the trait bound '[u8; 43]: std::convert::AsRef<[u8]>' is not satisfied
(compiler error), so we cannot useas_bytes()
.I believe this issue happens only when a trait like
AsRef<[T]>
is used.Thanks to fix this issue.
The text was updated successfully, but these errors were encountered: