-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Include space in suggestion mut
in bindings
#47465
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
src/test/ui/lint/suggestions.stderr
Outdated
@@ -22,7 +22,7 @@ warning: variable does not need to be mutable | |||
--> $DIR/suggestions.rs:36:13 | |||
| | |||
36 | let mut a = (1); // should suggest no `mut`, no parens | |||
| ---^^ | |||
| ----^ |
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 did something similar in #45741
The terminal output should not be showing whitespace.
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 the opposite problem to that. The span needs to point to the whitespace as well, so that the suggestion removes that whitespace. The suggestion content does not have trailing whitespaces.
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.
Right. But the human readable output should not highlight the whitespace. Only json should have it
@@ -77,7 +77,7 @@ impl<'a, 'tcx> UnusedMutCx<'a, 'tcx> { | |||
continue | |||
} | |||
|
|||
let mut_span = tcx.sess.codemap().span_until_char(ids[0].2, ' '); | |||
let mut_span = tcx.sess.codemap().span_through_char(ids[0].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.
What if there is more than one space after the mut
? Also, and this is a bug with the existing code as well, what if the white-space after the mut
doesn't contain any spaces at all?
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 following highlights until the end of foo
.
fn main() {
let mut
foo = 1;
println!("{}", foo);
}
I don't think multiple spaces are an issue, because there'll be the same number of multiple spaces as there were before.
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 blocking issue. I'll take a look at 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.
The code now accounts for any whitespace characters, in any amount, so that the resulting code will always be one space. I could see this breaking formatting sometimes, but should never produce incorrect code or trailing whitespace in the resulting code.
☔ The latest upstream changes (presumably #47522) made this pull request unmergeable. Please resolve the merge conflicts. |
284639e
to
d3c0701
Compare
src/libsyntax/codemap.rs
Outdated
// get the bytes width of all the non-whitespace characters | ||
for (i, c) in snippet.chars().take_while(|c| !c.is_whitespace()).enumerate() { | ||
offset += c.len_utf8(); | ||
pos = i + 1; |
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 pos
variable doesn't seem right. It's storing the character index but it's being used as a byte index below. The following should work I think:
let mut offset = 0;
// get the bytes width of all the non-whitespace characters
for c in snippet.chars().take_while(|c| !c.is_whitespace()) {
offset += c.len_utf8();
}
// get the bytes width of all the whitespace characters after that
for c in snippet[offset..].chars().take_while(|c| c.is_whitespace()) {
offset += c.len_utf8();
}
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're right, I was thinking about slicing on a char slice, not the original string. Will change to your proposed code.
src/libsyntax/codemap.rs
Outdated
offset += c.len_utf8(); | ||
} | ||
// get the bytes width of all the whitespace characters after that | ||
for c in snippet[offset+1..].chars().take_while(|c| c.is_whitespace()) { |
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.
What is the +1
for?
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.
Unless I'm missing something, !snippet[offset].is_whitespace()
, right? Then snippet[offset+1].is_whitespace()
. I think. Waiting for CI to finish in order to be 100% sure.
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.
No, offset
is pointing to the byte immediately after the last non whitespace character.
e835bc2
to
1affbd4
Compare
Addressed review comments. I'm planning on having a follow up so that suggestions where the span is pointing at multiple lines only because of whitespace are shown on their own so that
becomes
|
/// the original `Span`. | ||
/// | ||
/// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned. | ||
pub fn span_until_non_whitespace(&self, sp: Span) -> Span { |
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: maybe we can implement this and span_through_char
in terms of some common
fn span_until_predicate(&self, predicate: impl Fn(char) -> bool) -> Span { ... }
?
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 see that the code is quite different. Never mind, not needed (yet).
46 | let mut a = (1); // should suggest no `mut`, no parens | ||
| ---^^ | ||
48 | let mut a = (1); // should suggest no `mut`, no parens | ||
| ----^ |
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.
doesn't it seem like this is underlining one character more than is needed?
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, hmm, I guess the point is that you would have to delete this character? Still, I feel like when we display it here we ought to strip trailing whitespace from the underlined stuff =)
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'm pushing that to a follow up PR. When the span is a single line, it'd make sense to do so, but on a multiline span it'd be better to just show the suggestion on its own, as proposed in the comment above.
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.
Ok.
@bors r+ |
📌 Commit 1affbd4 has been approved by |
⌛ Testing commit 1affbd4fecf60759132f2fba509eb438840b4448 with merge e0bde94f0ac5379da54e5c29a3cf3989b43e06c1... |
💔 Test failed - status-appveyor |
I find the error a bit bizarre:
@bors retry |
src/libsyntax/codemap.rs
Outdated
/// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned. | ||
pub fn span_until_non_whitespace(&self, sp: Span) -> Span { | ||
if let Ok(snippet) = self.span_to_snippet(sp) { | ||
let mut offset = 1; |
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 should start at 0. I believe this is what caused CI to fail because if snippet
is "MallocSizeOf"
it will panic the same way: playground.
⌛ Testing commit 1affbd4fecf60759132f2fba509eb438840b4448 with merge b4a099a5b09379e28b71f233f5de68c65f84e4c6... |
💔 Test failed - status-travis |
Failed again. Legit.
|
1affbd4
to
79f24fe
Compare
79f24fe
to
df412ce
Compare
@bors r=nikomatsakis |
📌 Commit df412ce has been approved by |
Include space in suggestion `mut` in bindings Fix #46614.
☀️ Test successful - status-appveyor, status-travis |
Fix #46614.