-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add lint to suggest as_chunks over chunks_exact with constant #16002
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
base: master
Are you sure you want to change the base?
Add lint to suggest as_chunks over chunks_exact with constant #16002
Conversation
This comment has been minimized.
This comment has been minimized.
|
Lintcheck changes for c50ccf8
This comment will be updated if you push new changes |
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.
Good start:) Left a couple suggestions
| LL | let mut it = slice.chunks_exact(CHUNK_SIZE); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: consider using `as_chunks::<4>()` for better ergonomics |
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.
As this example shows, the suggestion replace even named constants with their values. To fix that, build the suggestion using a snippet of arg, to preserve its textual representation:
- format!("consider using `{suggestion}::<{chunk_size}>()` for better ergonomics")
+ let arg_str = snippet(cx, arg.span, "_");
+ format!("consider using `{suggestion}::<{arg_str}>()` for better ergonomics")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.
Done in 610acda
|
|
||
| // Should trigger lint - literal constant | ||
| let mut it = slice.chunks_exact(4); | ||
| //~^ ERROR: using `chunks_exact` with a constant chunk size |
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.
nit: you could also use the lint name, which is a bit shorter:
| //~^ ERROR: using `chunks_exact` with a constant chunk size | |
| //~^ chunks_exact_to_as_chunks |
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.
Done in 610acda
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.
Oh, no, that's not what I meant.. sorry for not articulating myself clearly.
The error message by itself is great, no need to change it. It's just that in the ui-test file, you can specify the error annotation not only as //~^ ERROR: <error message>, but also as //~^ <lint_name> (in your case, //~^ chunks_exact_with_const_size, which makes the test suite much less verbose overall, and so that's the change I was proposing.
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.
I guess I misunderstood, and I hope this hash is correctly implemented and suits your suggestion bc1d010.
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.
Yes, that's exactly what I meant:)
clippy_lints/src/methods/mod.rs
Outdated
| /// for chunk in chunks {} | ||
| /// ``` | ||
| #[clippy::version = "1.93.0"] | ||
| pub CHUNKS_EXACT_TO_AS_CHUNKS, |
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.
lint names usually either point out the "bad" pattern used (e.g. ok_expect), or what the pattern could be replaced with (e.g. manual_map). I think yours could use the former scheme, and be called something like chunks_exact_with_const_size
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.
Done in 610acda
| "as_chunks" | ||
| }; | ||
|
|
||
| span_lint_and_help( |
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 could be pretty effortlessly switched over to give suggestions, which are quite nice to have. https://doc.rust-lang.org/clippy/development/emitting_lints.html#suggestions-automatic-fixes should help you in that, but don't hesitate to ask if something's unclear
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.
Done in 7082eef
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.
Awesome, thank you!
| // Check receiver is slice or array type | ||
| let recv_ty = cx.typeck_results().expr_ty(recv).peel_refs(); | ||
| if !recv_ty.is_slice() && !recv_ty.is_array() { | ||
| return; | ||
| } |
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.
You want to check for types that can be adjusted to slices, which includes e.g. Vec. For that, you can use TyCtxt::expr_ty_adjusted:
| // Check receiver is slice or array type | |
| let recv_ty = cx.typeck_results().expr_ty(recv).peel_refs(); | |
| if !recv_ty.is_slice() && !recv_ty.is_array() { | |
| return; | |
| } | |
| // Check if receiver is slice-like | |
| if !cx.typeck_results().expr_ty_adjusted(recv).peel_refs().is_slice() { | |
| return; | |
| } |
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.
Done in 1d99b91
| // Check for Rust version | ||
| if !msrv.meets(cx, msrvs::AS_CHUNKS) { | ||
| return; | ||
| } |
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 check is somewhat expensive, so it's best to perform it towards the end, e.g. after the constant check
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.
Done in 2731771
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.
Sorry, I meant after the constant check, but before the lint emission -- currently, the check doesn't actually do anything 😅
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.
Done
Suggest using slice.as_chunks::<N>() when chunks_exact(N) is called with a compile-time constant. This provides better ergonomics and type safety. Fixes rust-lang#15882
Changed example suggestion from const to snippet Shortend error message Followed clippy naming conventions
849dbe8 to
610acda
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
clippy_lints/src/methods/mod.rs
Outdated
| ); | ||
| }, | ||
| (name @ (sym::chunks_exact | sym::chunks_exact_mut), [arg]) => { | ||
| chunks_exact_with_const_size::check(cx, expr, recv, arg, name, self.msrv); |
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.
Currently, the span that the lint points at is the whole method call expression (expr.span) -- as the last result in Lintcheck (https://github.com/rust-lang/rust-clippy/actions/runs/19080004877#user-content-chunks-exact-with-const-size) shows, that might lead to a somewhat confusing diagnostics:
warning: chunks_exact_with_const_size
--> target/lintcheck/sources/rustc-demangle-0.1.24/src/v0.rs:299:25
|
299 | let mut bytes = self
| _________________________^
300 | | .nibbles
301 | | .as_bytes()
302 | | .chunks_exact(2)
| |____________________________^
|
= help: consider using `as_chunks::<2>()` for better ergonomics
= note: `--force-warn clippy::chunks-exact-with-const-size` implied by `--force-warn clippy::all`
You can instead only highlight the method call (in this case, chunks_exact(2), by using call_span, which comes from the call to method_call(expr) above (line 13) -- see filter_map_bool_then for a (somewhat) simple 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.
I hope I understood that correctly. Done in dc9fa9d
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.
Pretty much! Though I think the .0.iter() part was actually correct, you didn't need to remove it
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.
In fact, it would be great if you could add a test case for a multiline call -- to make sure that we're giving the correct diagnostic
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.
In fact, it would be great if you could add a test case for a multiline call -- to make sure that we're giving the correct diagnostic
I wanted to add a multiline test, but check-fmt.rs comes in the way. And to be honest, I do not know how to handle that issue.
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.
You could either:
- Artificially elongate the expression, e.g. by adding more intermediate method calls, so that rustfmt splits it up into multiple lines
- Put the
#[rustfmt::skip]attribute onto the whole statement, to avoid formatting it entirely -- but I wouldn't use that unless absolutely necessary, e.g. when you want to add tests for really bad formatting, likevec . chunks_exact( 4)
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.
Great progress! Here's another couple comments^^
clippy_lints/src/methods/mod.rs
Outdated
| (name @ (sym::chunks_exact | sym::chunks_exact_mut), [arg]) => { | ||
| chunks_exact_with_const_size::check(cx, recv, arg, expr.span, call_span, name, self.msrv); | ||
| }, |
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.
Oh, I just noticed that this is located in the branch for lints that handle receivers and arguments coming from expansion. I think it would be safer to not do that, at least in the beginning -- working with from-expansion stuff can be quite error-prone -- could you please move this code to the branch under method_call (starting somewhere around line 5050)?
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.
Done in 3cfa99a
| // Check for Rust version | ||
| if !msrv.meets(cx, msrvs::AS_CHUNKS) { | ||
| return; | ||
| } |
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.
Sorry, I meant after the constant check, but before the lint emission -- currently, the check doesn't actually do anything 😅
clippy_lints/src/methods/mod.rs
Outdated
| ); | ||
| }, | ||
| (name @ (sym::chunks_exact | sym::chunks_exact_mut), [arg]) => { | ||
| chunks_exact_with_const_size::check(cx, expr, recv, arg, name, self.msrv); |
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.
Pretty much! Though I think the .0.iter() part was actually correct, you didn't need to remove it
clippy_lints/src/methods/mod.rs
Outdated
| ); | ||
| }, | ||
| (name @ (sym::chunks_exact | sym::chunks_exact_mut), [arg]) => { | ||
| chunks_exact_with_const_size::check(cx, expr, recv, arg, name, self.msrv); |
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.
In fact, it would be great if you could add a test case for a multiline call -- to make sure that we're giving the correct diagnostic
| "as_chunks" | ||
| }; | ||
|
|
||
| span_lint_and_help( |
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.
Awesome, thank you!
| diag.span_suggestion( | ||
| expr_span, | ||
| "consider using `as_chunks` instead", | ||
| suggestion, | ||
| applicability, | ||
| ); |
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 suggestion doesn't need to point at the whole expression either -- you can suggest replacing just the span of chunks_exact(N) (so call_span) with as_chunks::<4>().0.iter() (notice the lack of the starting ., as call_span doesn't include it). This has an additional advantage of not requiring you to get a snippet for the receiver, as you won't need that in the suggestion anymore
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.
Done in 370d0ae
…nts may not come from expansions to reduce risk for errors
…tion to reduce costs
| // Suggestion replaces just "chunks_exact(N)" with "as_chunks::<N>().0.iter()" | ||
| let suggestion = format!("{suggestion_method}::<{arg_str}>().0.iter()"); | ||
|
|
||
| span_lint_and_sugg( | ||
| cx, | ||
| CHUNKS_EXACT_WITH_CONST_SIZE, | ||
| call_span, | ||
| format!("using `{method_name}` with a constant chunk size"), | ||
| "consider using `as_chunks` instead", | ||
| suggestion, | ||
| applicability, | ||
| ); | ||
| } |
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.
As Lintcheck shows in #16002 (comment), here's one bigger remaining1 problem: chunks_exact(_mut) (I'll use the immutable version from here on, for brevity) returns ChunksExact, and apart from being an iterator, that struct also has a remainder method, which returns, well, the remainder. But since we replace the whole chunks_exact call with as_chunks().0.iter(), we lose access to that remainder.
In theory, we could make the lint a lot smarter, so that when it sees that the chunk_exact is stored into a variable, and is then used both as an iterator and to get the remainder, we adjust both calls correctly. So this example from Lintcheck:
let chunk_iter = bytes.chunks_exact(CHUNK_SIZE);
let remainder_chunk = chunk_iter.remainder();
for chunk in chunk_iter {
/* ... */
}would be turned into something like this:
let chunk_iter = bytes.as_chunks::<CHUNK_SIZE>();
let remainder_chunk = chunk_iter.1;
for chunk in chunk_iter.0.iter() {
/* ... */
}But that'd be pretty complicated, so I suggest we do the following instead:
- If we see that the call to
.chunks_exactis stored into a variable, - don't offer a suggestion (as it might not be correct)
- but instead give a help message, something like:
let chunk_iter = bytes.chunks_exact(CHUNK_SIZE); ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.as_chunks::<CHUNK_SIZE>() instead`
(1) can be done as follows:
if let Node::LetStmt(_) = cx.tcx.parent_hir_node(expr.hir_id) {
/* only leave a help message, not a suggestion */
}Here's how that would probably look in your code:
Suggestion
| // Suggestion replaces just "chunks_exact(N)" with "as_chunks::<N>().0.iter()" | |
| let suggestion = format!("{suggestion_method}::<{arg_str}>().0.iter()"); | |
| span_lint_and_sugg( | |
| cx, | |
| CHUNKS_EXACT_WITH_CONST_SIZE, | |
| call_span, | |
| format!("using `{method_name}` with a constant chunk size"), | |
| "consider using `as_chunks` instead", | |
| suggestion, | |
| applicability, | |
| ); | |
| } | |
| let as_chunks = format_args!("{suggestion_method}::<{arg_str}>()"); | |
| span_lint_and_then( | |
| cx, | |
| CHUNKS_EXACT_WITH_CONST_SIZE, | |
| call_span, | |
| format!("using `{method_name}` with a constant chunk size"), | |
| |diag| { | |
| if let Node::LetStmt(_) = cx.tcx.parent_hir_node(expr.hir_id) { | |
| // The `ChunksExact(Mut)` struct is stored for later -- this likely means that the user intends to not only use it as an iterator, | |
| // but also access the remainder using `(into_)remainder`. For now, just give a help message in this case. | |
| // TODO: give a suggestion that replaces this: | |
| // ``` | |
| // let chunk_iter = bytes.chunks_exact(CHUNK_SIZE); | |
| // let remainder_chunk = chunk_iter.remainder(); | |
| // for chunk in chunk_iter { | |
| // /* ... */ | |
| // } | |
| // ``` | |
| // with this: | |
| // ``` | |
| // let chunk_iter = bytes.as_chunks::<CHUNK_SIZE>(); | |
| // let remainder_chunk = chunk_iter.1; | |
| // for chunk in chunk_iter.0.iter() { | |
| // /* ... */ | |
| // } | |
| // ``` | |
| diag.span_help( | |
| call_span, | |
| format!("consider using `{as_chunks}` instead"), | |
| ); | |
| } else { | |
| diag.span_suggestion( | |
| call_span, | |
| "consider using `as_chunks` instead", | |
| // Suggestion replaces just "chunks_exact(N)" with "as_chunks::<N>().0.iter()" | |
| format!("{as_chunks}.0.iter()"), | |
| applicability, | |
| ); | |
| } | |
| } | |
| ); | |
| } |
Footnotes
-
foreshadowing :P ↩
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.
I might get that totally wrong but isn't that in opposition to your comment above? Because I think it is useful to have the suggestion the possibility to access to the tuple.
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 reason I wouldn't want to suggest that in here is that, again, I think that if the user stores the ChunksExact in a variable, then they probably want to access the remainder -- and suggesting .0.iter() would go against that. If they do want to use the variable just as an iterator, they can of course add .0.iter() themselves, but that scenario would be rather rare I think.
In general, the return type of as_chunks is pretty self-explanatory imo -- it's a tuple, and a quick glance at the API docs tells you what each half stands for, so I imagine the user will be able to figure that out...
But if we do want to be extra helpful, we could emit an additional note: given the example code from above, it could look like this:
note: you can access the chunks using `chunk_iter.0.iter()`, and the remainder using `chunk_iter.1`
To do that, you'll need to:
- extract the
patfromLetStmt patis an arbitrary pattern, since the left-hand side of a let-statement could in theory be an arbitrary pattern (e.g. destructuring), but we can assume it's justPatKind::Binding. Therefore, match on that and extractident-- the identifier (=name) of the variable- construct the note message, and emit it using
diag.span_note(after emitting the help message)
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.
Well done!:)
Oh, but there's a caveat I forgot about and you no doubt noticed – since in one of the test cases we now only give a help message and not a suggestion, ui_test (the UI test library we're using) will fail, as even after applying all the suggestions, the test file still raises warnings, precisely because help-only cases haven't actually changed.
Because of this, such test cases are placed into a separate file, usually called <lint_name>_unfixable.rs (there's also this //@no-rustfix thing you'll often see in such files, but you don't need to worry about that for 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.
I was wondering why the tests were failing. Luckily, I do not get frustrated that easily might have taken me a couple of attempts before I would have noticed.
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.
Done in 64aae82
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.
Btw, could you please add the comment from the suggestion (of some version of it, as you please) to your code? Having a TODO will let an interested contributor pick it up in the future
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.
I am going to use your comment as it is more helpful and provides transparency next possible steps.
If method is stored in let binding, emit a help message instead of the fix
| format!("using `{method_name}` with a constant chunk size"), | ||
| |diag| { | ||
| if let Node::LetStmt(let_stmt) = cx.tcx.parent_hir_node(expr.hir_id) { | ||
| diag.help(format!("consider using `{as_chunks}` instead")); |
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.
You can use span_help with call_span here
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.
Done in 83e0039
| let mut applicability = Applicability::MachineApplicable; | ||
| let arg_str = snippet_with_applicability(cx, arg.span, "_", &mut applicability); | ||
|
|
||
| let as_chunks = format!("{suggestion_method}::<{arg_str}>()"); |
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.
nit: you should be able to use format_args! here to avoid an intermediary allocation
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.
Done in 83e0039
Adds a new lint
chunks_exact_to_as_chunksthat suggests usingas_chunksinstead ofchunks_exactwhen the chunk size is a compile-time constant.changelog: [
chunks_exact_to_as_chunks]: Suggest using slice.as_chunks::() when chunks_exact(N) is called with a compile-time constant. This provides better ergonomics and type safety.fixes #15882