-
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
Add slice_as_bytes lint #10984
Add slice_as_bytes lint #10984
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) soon. 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 (
|
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.
Very good for a first-time contributor, thanks ❤️! Could you consider these changes?
Also, as a side note, the lint should go in the methods
type. Try putting it there (maybe creating a new branch and copy-pasting your changes to the new files?) or, if it's too complex, just leave it as is (We'll manually re-type all lints in a few weeks).
(cargo dev new_lint --name=slice_as_bytes --type=methods --category=pedantic
)
☔ The latest upstream changes (presumably #10951) made this pull request unmergeable. Please resolve the merge conflicts. |
I believe I've addressed all the comments, please let me know if I missed something :) |
I'm going to be honest, I don't like this lint without clearer warnings that it could break working code. The comment is this removes code which can "perform a safety check which can panic". If someone wrote their code depending on that safety check, this clippy silently removes it. I realise this would be a much bigger change (sorry!), but could clippy have some way of annotating changes which change behaviour in this way? |
Checking every use of that same vector would be too expensive performance-wise, so I don't think it's worth it. We can document it in the lint though. Could you provide an example where this check is needed? I feel like using this check would be just moving the panic from one place to another. But thanks for the warning! ❤️ |
I'm not sure what you're referring to, Could you please explain?
Sure, helix-editor/helix#7417. We were providing chunks of bytes from a string to be processed by some other code. The chunks themselves are not necessarily valid strings, ie can be cut in the middle of a char. |
☔ The latest upstream changes (presumably #11012) made this pull request unmergeable. Please resolve the merge conflicts. |
Referring to: "If someone wrote their code depending on that safety check, this clippy [lint] silently removes it." -- @ChrisJefferson I was saying that, if we need to check if all uses of a vector |
I think we should do a big lintcheck and see the repercusions of this change. We can always leave it at |
☔ The latest upstream changes (presumably #10970) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
Apologies for the long delay. Just a category change, and some minor cleanup are needed.
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub SLICE_AS_BYTES, | ||
perf, |
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.
Given the behaviour change and the small perf gain, this should not be linting by default. pedantic
would be a better category.
let ty = cx.typeck_results().expr_ty(indexed).peel_refs(); | ||
let is_str = ty.is_str(); | ||
let is_string = is_type_lang_item(cx, ty, LangItem::String); | ||
if is_str || is_string { |
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.
Should just be `ty.is_str() || is_type_lang_item(..)``. They can both just be referred to as a string in the message.
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { | ||
if let ExprKind::Index(indexed, index) = recv.kind { | ||
if is_range_literal(index) { |
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.
Let-chains can be used here. No need for nested if statements.
SLICE_AS_BYTES, | ||
expr.span, | ||
&(format!( | ||
"slicing a {type_name} before calling `as_bytes` results in needless UTF-8 alignment checks, and has the possiblity of panicking" |
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.
The justification for the lint should not be in the message, only a short description of what is being linted. The rest of it should just be in the documentation, or if needed, as an additional note.
let is_str = ty.is_str(); | ||
let is_string = is_type_lang_item(cx, ty, LangItem::String); | ||
if is_str || is_string { | ||
let mut applicability = Applicability::MachineApplicable; |
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.
Should be MaybeIncorrect
. Same justification as the category change.
/// Checks for string slices immediantly followed by `as_bytes`. | ||
/// ### Why is this bad? | ||
/// It involves doing an unnecessary UTF-8 alignment check which is less efficient, and can cause a panic. | ||
/// ### Example |
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.
A ### Known problems
section is needed with the note that the panic may be required for the code's correctness.
Hey this is triage, I'm closing this due to inactivity. If you want to continue this implementation, you're welcome to create a new PR. @A-Walrus, thank you for the time, you already put into this! Interested parties are welcome to pick this implementation up as well :) @rustbot label +S-inactive-closed -S-waiting-on-author -S-waiting-on-review |
Detects patterns like
and suggest replacing with
fixes: #10981