-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Reduce span highlighted code in unused_variables lint #50472
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
Comments
I'd like to take this issue. |
Gladly! This seems to be the place where the lint gets triggered: rust/src/librustc/middle/liveness.rs Lines 1542 to 1562 in 6f721f5
and the |
@11Takanori Do you need any help?
So you can get the sub-span's beginning |
@csmoe I'd prefer to somehow get the original span if possible, instead of creating new spans. Sadly, this is not as trivial. A short code dive revealed that the name we use here rust/src/librustc/middle/liveness.rs Lines 1530 to 1531 in 6f721f5
is from rust/src/librustc/middle/liveness.rs Line 1490 in 6f721f5
which calls rust/src/librustc/middle/liveness.rs Lines 330 to 337 in 6f721f5
so, our "variable name" might in fact be We should also try to add an applicability marker to this lint (landed in #50204, you can see it in use in #50454). |
@killercup ~~~I tried your
The sounds more plausible than deriving from mix-span, I'll explore more into your advice later since it's midnight. |
Not necessarily something we need to fix in this exact issue but I just wanted to mention that we should probably also add a macro-check to this lint. For example, this code: macro_rules! foo {
($x:ident) => {{ let $x = 42; }}
}
fn main() {
foo!(y);
} tells me this (playground):
This is somewhat confusing, but should also never be applied by rustfix as suggested. |
Is this issue difficult for a beginner like me? It will take me time to understand scope we need to fix. |
@11Takanori hard to say, I'm neither a regular contributor nor mentor for rustc. Other people did diagnostics adjustments as first contributions to the compiler. Maybe you and @csmoe can figure something out together? cc @Manishearth and @oli-obk who both know more on this than I do |
@11Takanori Are you still here? If you are interested in this issue, this is the instruction: rust/src/librustc/middle/liveness.rs Lines 1394 to 1404 in 8ff4b42
So the issue is caused by the wrong span( sp ) which represents a Pat , but what we want is the REAL name span.
Lines 1125 to 1129 in 8ff4b42
We can search all the info of a variable from a let statement(represented by Local ), as indicated by the comment of struct Local , those info might live in <pat> . Finally, we get it, all the info are wrapped in the Spanned struct:Line 877 in 8ff4b42
Lines 51 to 54 in 8ff4b42
Now we can derive an approcah from the analysis above:
Use
We'll step to applicability after |
Reduce span highlighted code in unused_variables lint Fixes #50472 - [X] reduce var span - [ ] mark applicable Before: ``` mut unused_mut_var ^^^^^^^^^^^^^^^^^^ ``` After: ``` mut unused_mut_var ^^^^^^^^^^^^^^ ```
Let's say I wrote this critical piece of code:
rustc gives me the following output:
Which is correct -- except for one little detail. Which is crucial, if you are not just any developer but actually rustfix.
You see, the
mut
inmut i
is highlighted, but the replacement is just for the binding name, not the mutability. (Why not the mutability? Because the mutability is covered by the next lint we trigger,unused_mut
.) And rustfix will rightfully complain if we try to replace parts of the code that we have already replaced.tl;dr We need to change the span in
unused_variables
to only cover thei
The text was updated successfully, but these errors were encountered: