-
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
New lint: unbuffered_bytes
#14089
base: master
Are you sure you want to change the base?
New lint: unbuffered_bytes
#14089
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Jarcho (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
5a02762
to
fecee63
Compare
I made a few more changes, should be ready for review now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine minus a few nits.
Theoretically, a trait implementation of Read could override .bytes() with an efficient implementation. I'm not sure how to add this check to the lint, and I can't find any cases of this being done in practice.
This is not possible given the return type of bytes
. The only potential false positive would be:
- A type that generates the data. No way to detect this, but generating one byte at a time isn't necessarily efficient either.
- A type which reads from memory. These really should just implement
BufRead
, but they may not. - Opaque types which hide a
BufRead
impl. This can be worked around, but it's probably not very common.
I don't think an automatic fix for this lint is possible, but I'd love to be proven wrong
You can wrap the receiver in a call to std::io::BufRead::new
. You'll need to use walk_span_to_context(recv.span, expr.span)
and then a multipart suggestion inserting to the start and end of the span.
Compromise: Is it possible to make this lint pedantic on types that are not on a allowlist?
The closest thing is to have a configuration value for the lint to make it more or less aggressive. We might at some point get different predefined configs, but that's not going to be anytime soon.
@Jarcho I've resolved your comments, thanks for the feedback! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Can you just squash the commits please.
If you want to add the suggestion to the lint feel free, but it's not needed.
adaa140
to
4b5fa3f
Compare
I've squashed my commits @Jarcho I may try to do the suggestion later (in a future PR), when I have a bit more time on my hands. |
Checks for
Read::bytes()
on an unbuffered Read type.The default implementation calls
read
for each byte, which can be very inefficient for data that’s not in memory, such asFile
.Considerations which I'd like to have feedback on:
.bytes()
is called on any type that implementsstd::io::Read
but notstd::io::BufRead
. This is quite aggressive and in and may result in false positives. Alternatives:R: Read
and calls.bytes()
are not caught by the lint, which could be quite a nasty case of this bug.pedantic
on types that are not on a allowlist?Read
could override.bytes()
with an efficient implementation. I'm not sure how to add this check to the lint, and I can't find any cases of this being done in practice.Fixes #14087