Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

lapla-cogito
Copy link
Contributor

close #13856

changelog: [manual_slice_fill]: new lint

@rustbot
Copy link
Collaborator

rustbot commented Jan 27, 2025

r? @Manishearth

rustbot has assigned @Manishearth.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 27, 2025
@Manishearth
Copy link
Member

r? clippy

still a bit busy, trying not to do new lint reviews

@rustbot rustbot assigned llogiq and unassigned Manishearth Jan 27, 2025
Comment on lines 27 to 45
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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Comment on lines +728 to +730
/// for i in 0..some_slice.len() {
/// some_slice[i] = 0;
/// }
Copy link
Contributor

@llogiq llogiq Jan 28, 2025

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; }?

Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: manual_slice_fill
4 participants