Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Fix for issue 6640 #6665
Fix for issue 6640 #6665
Changes from 1 commit
bfbc083
e0e51e4
6165ccc
a78271b
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Some of these comments are just restating what the code itself already says.
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 is basically a copy from
unused_unit
. Should I change 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.
You should probably use
span_lint_and_then
here as well because this lint suggests changes at several places:span_lint_and_sugg
only works for one suggestion this would leave the return statements unchanged which would lead to syntax errors until the user updates them.span_lint_and_then
allows a suggesting for the return type and the for the expressions usingmultipart_suggestion
.I personally would try to reuse the
span_lint_and_then
and just adjust the suggestions and message string according to the case.type_snippet
)type_change
)return Some(XYZ);
->return XYZ;
(Some(XYZ)
->XYZ
)return Some(());
->return;
(Some(())
-> ``)This would allow the
span_lint_and_then
reuse like this:That's just my own style, so there might be better ways or some other way which seems cleaner to you. Just use the one you are most comfortable with 🙃
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.
Thank you for the great suggestion. However, there is one thing I am not certain with. One issue I see with using
span_lint_and_then()
for cases like-> Option<()>
is that depending on the function, we would have two different kinds of suggestions: 1)return Some(());
->return;
and 2)Some(())
-> remove the line. I wonder if we can always propose good suggestions for 2).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.
We should still notify the user even if they should just remove a line.
multipart_suggestion
can also be used to remove code by just suggesting an empty string. You already use this in the replacement of the-> Option<()>
. The only thing you would need to adapt is the suggestion generation (Here is the change in pseudo rust):The span is just set to
ret_expr.span
. I believe that this would only be theSome(())
part fromreturn Some(())
. The suggestion would therefore leave the simple return, and we don't need to handle any special cases. Could you maybe try this? 🙃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.
Thanks again for the great suggestion. I had already started to try that, but I had given up for the above reason.
So it works, but I see two potential issues:
return
, it works, but since the white space between it and the return expression is not included in the latter span, I had to hack a function to included it. I works for my tests, but I don't know if it's robust enough:Some(())
, but it leaves an empty line (see betweenSome(());
and `} else {:Is there a way to remove this? Or is it fine like this?
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.
Creating such a function is a valid option and the more I think about it probably also the cleanest option. 👍
I would suggest adding it as an util function similar to
utils::position_before_rarrow
. This is probably something that will be reused in the future. You can then use this byutils::line_span
with the expression span.All of these steps can probably also be done by the util function itself.
If you want to remove the entire line like you wanted in your example output you can simply use
utils::line_span
. The visualization will most likely still show the empty line though. The lint has multiple spans with suggestions and is therefor not automatically applicable anyways. The suggestion is therefor only for the user to see which changes should be done :).Another way would be to suggest a change that includes the second to last statement like this:
The suggestion would then show the suggestion like:
The suggestion is a bit cleaner but the code to get this suggestion a more complicated with more edge cases. These are the options I can think of. It might be worth to ask over on our Zulip channel if you want more suggestions :).
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.
Thank you again for the long reply.
To be honest, I think I prefer leaving an empty line. I am also worried that by trying to remove line, we might hit some weird edge cases. Just removing the the span of the return expression seems much safer even if the suggestion will not be as clean. So if you approve, I'll go this way.
About the utility function, a function
position_after_return()
would just be a call tofind()
, so I'm not sure it would be worth it. I could add this whole function, but I feel it is a bit too sketchy to go in utisl. :-P But you tell me. :-)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.
Generally using
ByteOffset
s in suggestions is almost always a bad idea. That is because it almost always assumes a specific formatting of the code. Also it may remove comments.In this case I would just suggest to remove the
Some(())
and just keep the empty line. If the user cares about formatting,rustfmt
will deal with that anyway. If not ¯\_(ツ)_/¯. Formatting shouldn't be a concern for Clippy.I also thought about adding a utils function, that produces a suggestion to remove an entire line before. But then I found this case:
which is valid formatting according to
rustfmt
with some configurations. Just removing the entire line or everythingrfind("\n")
would break the code.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.
Thank you for the comment.
Yes, I was a bit uncomfortable with using
ByteOffset
. However, without it, I don't know if there is a way to avoid suggesting replacingreturn Some(());
byreturn ;
. That white space it annoying. :-PAny alternative?
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.
Nope, just ignore that whitespace. Formatting shouldn't be a concern for Clippy.
If you want to use
ByteOffset
, always ask yourself, if there could be a (reasonable) case where it may break code. In this case, this is one: PlaygroundThere 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 part is now much simpler now that the inner type is found earlier.