-
Notifications
You must be signed in to change notification settings - Fork 1.7k
WIP Suggestion creation macro #7986
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
Conversation
r? @llogiq (rust-highfive has picked a reviewer for you, use r? to override) |
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.
Cool idea! I like how you did this without using syn
. I do wonder how much this actually simplifies things though. Perhaps add another commit that uses it in a few lints once the tests pass so we can see the wins to be had?
36c2eeb
to
c419562
Compare
4997ffc
to
df0b229
Compare
☔ The latest upstream changes (presumably #8001) made this pull request unmergeable. Please resolve the merge conflicts. |
Anything needed or should I review this now? |
Still working away at it, slowly. Hopefully will be done this weekend. A good chunk of has been rewritten so there isn't really much of a point reviewing what's up there other than to see what the updated lints look like. |
I think a good goal here would be for the macro to expand into code that could be written without the macro. This would take a lot of complexity out of the macro implementation. For example, I would like to be able to write: let sugg = Suggestion::borrow( // adds '&' and parenthesis according to arg precedence
Suggestion::from_expr(cx, expr) // gets snippet
)
.position(cx, expr.hir_id) // adds outer parenthesis if needed when replacing `expr`
.to_string(); This is basically |
That would strictly be more work. Currently it's being handled like a format string with the addition of keeping track of what precedence level snippets are being inserted at, and what the lowest precedence level in the expression is.
This kind of output requires building an ast as something like That being said keeping |
Just playing around with syntax options:
There's also another option where the type of the expansion has a default value, and only needs to be specified sometimes:
I like the look of the format string versions, but it does mean any block expressions would need double curly braces. (e.g. The types could also be specified after out of band (e.g. |
I still wonder whether the macro is going to pull its weight in terms of complexity. While we have a number of rather complex suggestions, I doubt they add up to 1.2k lines of code. |
I have a few plans to shrink the amount of code, so there's still hope there. Ultimately I'd like an interface along the lines of lint_expr(cx, LINT_NAME, msg, expr, Applicability::MachineApplicable, sugg!($expr(foo) + 1)); This would cover a good chunk of lints which just replace one expression with another. Cover everything else with |
☔ The latest upstream changes (presumably #8266) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #8403) made this pull request unmergeable. Please resolve the merge conflicts. |
Sorry for letting you wait, I had a very busy week. This should be good to merge after yet another rebase. |
Sorry, I had a busy week. But another question arised: How does this mesh with the new diagnostics translation effort (which may include clippy in the future)? |
As far as I can tell this shouldn't be any more work than what we would currently need to do. |
☔ The latest upstream changes (presumably #9421) made this pull request unmergeable. Please resolve the merge conflicts. |
@Jarcho are you still interested in working on this? Or should it be closed? ヘ(^・ω・^=)~ |
Since there hasn't been any activity since the last comment asking about the status, I'm going to close this PR. @Jarcho you're welcome to reopen it or create a new one, if you're interested in continuing this work :D |
This is a minimal and hacky implementation right now, but it should get the idea across.
A large number of suggestions in clippy are basically extracting a snippet of an expression (or multiple), adding something around it, and then replacing an expression. This is a proc-macro made to simplify that as much as possible. The syntax is a regular rust expression with meta-variables to insert snippets. It should handle keeping track of precedence and inserting parenthesis around snippets as needed, as well as keeping track of the final precedence
Some examples:
changelog: None