-
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
Message suggesting prefixing with underscore is unhelpful #66636
Comments
Hi, I'm a novice and would like to help ! I feel the same way about this message :) |
What do you think about mentioning the lifetime of the variable ?, like the following snippet from the Rust book : warning: unused variable `i` in the lifetime starting at 1:1 and ending at 5:1
--> src/main.rs:2:9
1 | / fn main() {
... |
2 | | let i = 3; // Lifetime for `i` starts. ────────────────┐
3 | | // │
... |
4 | | // │
5 | | } // Lifetime ends. ────────────────────────────────────┘
| |_^
| help: if the variable is meant to be unused, consider prefixing with an underscore: `_i`
|
= note: `#[warn(unused_variables)]` on by default Or maybe something like : note: ...the reference/variable is valid for the lifetime 'a as defined on the impl at 27:1...
--> src\lib.rs:27:1
|
27 | / impl<'a> MyImpl<'a>{
28 | |
29 | | pub fn myfunc(i: &String) -> MyImpl{
30 | | ...
... |
89 | | }
90 | | }
| |_^ What's your opinion ? Am I too novice ? |
Hello! I'm new to this repo and I'd like to take up this issue. |
@loris-intergalactique what does it have to do with lifetimes? |
@Kixunil When I wrote the message, I didn't think about encapsulated lifetimes, my bad - This could have made the hints maybe even less helpful. What I think could give an insight to programmers about what they tried to accomplish is actually mentionning the I think that in a huge code base this could make save some time, and help checking the code around. I imagine that if you had something like : warning: unused variable `bark` in the function new_dog()
--> src/main.rs:2:9
1 | / fn new_dog() {
... |
2 | | let bark = Bark::from("bark");
| | ^^^^ help: you probably wanted to use it somehow, check the code around to see if it's the case
| | help: if the variable is meant to be unused, consider prefixing with an underscore: `_bark`
|
= note: `#[warn(unused_variables)]` on by default You'd have more hints about what's going on. With only weeks of practicing Rust, I don't know every use cases yet. But I think that combining the nice message that you gave, as well as another hint on where this variable is could be nice++ What do you think about that ? |
Ah I think you mean to mention the scope in which the variable is introduced, right? That would be very interesting, I think. |
Yes, that's right ! I'm gonna try implenting it and testing it. I'm happy to make my first contribution ! :) Hi, just a quick edit to say that I am still on it. I have more work than usual, but I do want to make this contrib ! :) |
Are you still working on it @loris-intergalactique ? |
Hi @shika-blyat, I do but I have been having some issues working with the compiler. Maybe I should let this issue go, unassign myself and take the time to learn step by step how the compiler works before. If you feel like you can take this issue, do it. I'll contribute somewhere else when I'll be maturer with rust 😄 |
I think the suggested message is overly verbose. If we are to change it, let's do with something succinct:
This has the advantages of:
Feedback on keeping it short in general and on the exact wording is welcome. |
rust/src/librustc_passes/liveness.rs Line 1568 in 5f13820
I think this is the involved line. |
I'd be interested to make this contribution (i think i'm able to do it 😄), but i don't know if i should wait for some kind of confirmation. |
Yeah, that's why I asked about the exact wording before submitting the PR |
@aleksator I like that wording. It's not as helpful for beginners in programming as the other was, but maybe it's safe to assume that most Rust devs aren't beginners in programming. Might be fun to have an opt-in plugin/wrapper/mode/whatever for the complete beginners one day in the future. |
Submitted a PR , adding a comma that should have been there: |
Hi @aleksator, thanks for this ! May I ask how did you test it, and how did you use x.py ? My problem when working on this issue was not to find the location of the message that was solved with a quick grep, but on the underlying tests. Each time I compiled it in order to make tests, I stumbled upon multiple errors that were not really linked to what I changed. it really slowed me down.. |
@loris-intergalactique My whole process to solve this issue was: I searched for all occurrences of previous warning message using search and replace feature of my editor in the project folder, and then pressed "replace all". For testing things locally, I run: I created a simple project with I did not run the whole test suite for this PR (since I thought it shouldn't break anything after changing the output of all related test cases to use the new message), and submitted my changes to be tested by CI here. I did test the other PR locally (if you are interested, it was this one to help resolve build issues here), and the same technique could be used for this one as well. For that I run: If you notice unrelated build errors at this point, something like |
Awesome, thank you ! I know what I did wrong now 👍 |
@loris-intergalactique What was that? Maybe we can submit a PR to the documentation to make things clearer. |
I think that my issues are on me on this one ! hahaha
I learned a lot of things along the road though, so I'm still happy !😄 Edit : And I also became acquainted with the message that was secretly haunting me in some cases ! :
|
Good job guys! |
I believe that in the vast majority of cases when an unused variable is present, the author of the code intended to use it, but forgot. Thus the message suggesting to effectively turn off the warning is unhelpful.
I suggest changing it to something like:
While this doesn't concern me much personally, I believe it'd be much more helpful to novice programmers. To put it in extreme, imagine this kind of lint message: "Reference outlives borrowed value. Consider putting
#![allow(undefined_behavior)]
at the top of your module."And I must admit the current warning feels a bit strange to me too. ("Dear compiler, the content of your message is completely useless, but its presence saved my ass twice today, so thank you for that!")
Disclaimer: this is entirely based on my personal experience and may not be true in general. Feel free to close if there's a good evidence that I'm broken, not the compiler. :)
This issue has been assigned to @loris-intergalactique via this comment.
The text was updated successfully, but these errors were encountered: