-
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 manual_slice_fill
lint
#14082
base: master
Are you sure you want to change the base?
add manual_slice_fill
lint
#14082
Conversation
r? @Manishearth rustbot has assigned @Manishearth. Use |
a7f0cf8
to
2318bbb
Compare
r? clippy still a bit busy, trying not to do new lint reviews |
2318bbb
to
d7bd4a0
Compare
if msrv.meets(msrvs::SLICE_FILL) | ||
&& let Some(higher::Range { | ||
start: Some(start), | ||
end: Some(end), | ||
limits: RangeLimits::HalfOpen, | ||
}) = higher::Range::hir(arg) | ||
&& let ExprKind::Lit(Spanned { | ||
node: LitKind::Int(Pu128(0), _), | ||
.. | ||
}) = start.kind | ||
&& let ExprKind::Block(_, _) = body.kind | ||
&& let ExprKind::Assign(assignee, assignval, _) = peel_blocks_with_stmt(body).kind | ||
&& let ExprKind::Index(slice, _, _) = assignee.kind | ||
&& let ExprKind::MethodCall(path, recv, _, _) = end.kind | ||
&& let ExprKind::Path(Resolved(_, recvpath)) = recv.kind | ||
&& let ExprKind::Path(Resolved(_, slicepath)) = slice.kind | ||
&& switch_to_eager_eval(cx, assignval) | ||
&& span_is_local(assignval.span) | ||
&& path.ident.name == sym::len |
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.
This code could do with either some functions to abstract things or comments to explain them. I was able to trace what is being matched where, but it could certainly be easier to follow.
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.
Updated.
/// for i in 0..some_slice.len() { | ||
/// some_slice[i] = 0; | ||
/// } |
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 we also try to detect for i in &mut some_slice { *i = 0; }
?
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.
Right, implementation added to allow detection of such patterns as well. Thanks!
d7bd4a0
to
801090f
Compare
801090f
to
913adc0
Compare
close #13856
changelog: [
manual_slice_fill
]: new lint