-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Emit a lint for unused assign to temporary variables #71681
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
Emit a lint for unused assign to temporary variables #71681
Conversation
1eb0a15
to
d1a2097
Compare
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.
LGTM
It feels odd that assignment to a literal expression is allowed at all... |
@mark-i-m As Jonas Schievink pointed out in #68713 (comment), a field is a valid place expression. This wouldn't work if there wasn't the field access. This means the following code is invalid and gives you an "invalid left-hand side of assignment" error: Foo { x: false } = Foo { x: true }; But if you have a field access, the code is valid, though still not useful. Foo { x: false }.x = true; |
d1a2097
to
0729e9f
Compare
@estebank I tweaked the diagnostics according to your review. I didn't understand what you meant with " |
let mut path_str = String::new(); | ||
for (i, seg) in segments.iter().enumerate() { | ||
if i > 0 { | ||
path_str.push_str("::"); | ||
} | ||
if seg.ident.name != kw::PathRoot { | ||
path_str.push_str(&seg.ident.as_str()); | ||
} | ||
} |
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.
Is there any existing function that does this? I tried to search but didn't find anything but in rustdoc
(which I... err... copy-pasted 😄)
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.
Isn't the hir::Path
itself already printable? You can use @
to bind the entire path while also keeping the filtering and reference to segments
.
| --- ^ | ||
| | | ||
| this is not bound to any variable | ||
| help: consider introducing a local variable: `let mut foo = FOO;` |
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 will cause the final code applied by rustfix
to be let mut foo = FOO;.x = 2;
, which is incorrect.
lint.build("unused assignement to temporary") | ||
.span_label(lhs.span, "this is not bound to any variable") | ||
.note("in Rust, constants are not associated with a specific memory location, and are inlined wherever they are used") | ||
.span_suggestion( |
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.
Can we change this to be span_suggestion_verbose
? This will make the suggestion not be a label, but show you the final code instead.
@LeSeulArtichaut Ping from triage, could you address the comments above? thanks. |
@estebank I haven't found a way to have the suggestion for introducing a local variable. Would that be OK to leave a FIXME and merge this without the suggestion? |
Had a discussion with the author, closing this due to inactivity and we will revisit it later when there's a better idea about this. |
Resolves partially #16789.
r? @estebank cc @Dylan-DPC