-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix println_empty_string suggestion caused error
#16201
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?
Conversation
|
rustbot has assigned @samueltardieu. Use |
…estion caused error when there is a `,` after arg
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 still fails if there are spaces, newlines, or comments before the comma. The fix should be more robust.
|
Reminder, once the PR becomes ready for a review, use |
979e76d to
f2820df
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
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 not robust: if there is no comma at all in the file, a span is built which extends to the next file in the source buffer which is wrong even though we discard it later.
Also, the following code will suggest a replacement which now does not compile, while it did before:
println!("" /* Let's break the Clippy lint, <= because of this comma */);Why not:
- Ensure that there is no comment inside the macro call and don't provide a suggestion in this case.
- Compute the span in the closure of
span_lint_and_then(), and only when we give a suggestion. - Use something simpler but more robust to compute the span, for example:
let closing_paren = cx.sess().source_map().span_extend_to_prev_char_before(macro_call.span.shrink_to_hi(), ')', false);
let mut span = format_args.span.with_hi(closing_paren.lo());(with the existing comma adjustment in the writeln!() case).
Tests should be added for comments in println!(), but also for writeln!() as right now it would fail the same way if a comment containing a comma is present just before the empty string.
It looks like it would be a more complete and solid fix.
What do you think?
It's reasonable. But how about using Or we just skip linting the situation you mentioned above. |
f2820df to
d2ed70c
Compare
println_empty_string suggestion caused error when there is a , after argprintln_empty_string and writeln_empty_string suggestion caused error
|
@rustbot ready |
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 a lot of complex code for practically no gain. What about my proposal to just not have an autofix if there are comments inside the macro call? You can still lint, you just have to replace the suggestion by a help message saying "remove the empty string" and let the user deal with it.
Note that I'm not proposing not to lint, but to not provide a suggestion (as in "autofix") in this case. |
d2ed70c to
f3dc7f1
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. |
println_empty_string and writeln_empty_string suggestion caused errorprintln_empty_string suggestion caused error
Thank you. I think using simpler but more robust way to compute span is good as you mentioned before. So I adopted your suggested code. However, I still think that using At least for Compared to carefully handling strings, operating on tokens is more reassuring. Just my two cents :) @rustbot ready |
Closes: #16167
changelog: [
println_empty_string]: fix suggestion caused error when there is a comma after arg.