-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Use original variable name in the suggestion #10175
Conversation
r? @giraffate (rustbot has picked a reviewer for you, use r? to override) |
@@ -43,7 +43,7 @@ LL | / let v = match f() { | |||
LL | | Ok(v) => v, | |||
LL | | Err(_) => return, | |||
LL | | }; | |||
| |______^ help: consider writing: `let Ok(v) = f() else { return };` | |||
| |______^ help: consider writing: `let Some(v) = f() else { return };` |
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 suggestion gives the compile error because of mismatched types.
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.
Thank you for quick review, and I realized I've misunderstood the problem...
I'm sorry for bothering you, since my approach cannot handle cases like that so may I close the PR for now?
let v = match build_enum() {
_ => continue,
Variant::Bar(v) | Variant::Baz(v) => v,
};
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.
Okay, no worries! Anyway, thanks for your try to improve Clippy!
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.
Yeah pattern editing is tough. rust-analyzer
has library functionality that can edit patterns, and it uses that library functionality for its "replace manual let else with let-else
" tool. It's pretty cool.
For |
patterns, the additional challenge is that they have to be surrounded by (...)
.
Btw, if you want to fix this PR, you should remove "renamings of the bindings" from the list in the comments.
Since let_else feature has been stable in 1.65.0, it's now unnecessary
clippy_lints/src/manual_let_else.rs
Outdated
PatKind::TupleStruct(ref w, ..) => { | ||
let sn_wrapper = cx.sess().source_map().span_to_snippet(w.span()).unwrap_or_default(); | ||
let (sn_inner, _) = snippet_with_context(cx, local.span, span.ctxt(), "", &mut app); | ||
format!("{sn_wrapper}({sn_inner})") | ||
}, |
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.
@giraffate @est31 Thank you for your advice:)
Finally, it's addressed by separating the PatKind::Or
case from the PatKind::TupleStruct
case, which we should fixed in the PR.
Could you review it again? thanks
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 is absolutely not enough but it will improve the situation at least for the Some
and Ok
cases. The biggest issue I think is that tuple structs with multiple arguments are not supported, as in:
let foo = if let SomeEnum::Val(v, 0) = e() { v } else { _ };
Will be turned into:
let SomeEnum::Val(v) = e() else { _ };
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.
But I think the PR should be accepted. 👍
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.
@@ -37,7 +37,6 @@ declare_clippy_lint! { | |||
/// Could be written: | |||
/// | |||
/// ```rust | |||
/// # #![feature(let_else)] |
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.
IMHO let_else
has been stable since 1.65 so it can be removed?
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.
Absolutely, yes.
tests/ui/manual_let_else.rs
Outdated
@@ -248,4 +254,13 @@ fn not_fire() { | |||
Some(value) => value, | |||
_ => macro_call!(), | |||
}; | |||
|
|||
fn e() -> Variant { |
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.
Just a note: this is inside a function named not_fire
. At least if you follow how I have originally designed the file,
the test should be moved to the end of the function do_fire
(as should the Issue 9440 snippet above).
Maybe clippy conventions are different and this design is unique among clippy, idk. I did it back then because it seemed to me that this design makes it very easy to verify that the not_fire
section does indeed not fire, you just look at the last emitted lint's line number and see that it is prior to the line number of the not_fire
function. I'd have mixed firing and not firing had clippy included ERROR
lines as the rustc testsuite does (which is IMO a good idea in general because in clippy you often have // should fire
or // should not fire
comments that do the same but are not machine verified).
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 like your design. (Sorry for bothering, I've just overlooked it, I usually add new specs at the end of files.)
I edited the last commit and moved them into fire()
.
@est31 Thank you for your review:)
update spec fix: move specs in fire
Friendly ping @giraffate -- This is ready for review (and IMO good to be merged) |
Thanks for pinging! I'll review this in a few days. |
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.
Overall looks good, thanks!
I made one comment for formatting.
local.els.is_none() && | ||
local.ty.is_none() && | ||
init.span.ctxt() == stmt.span.ctxt() && | ||
let Some(if_let_or_match) = IfLetOrMatch::parse(cx, init) { |
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 don't know why rustfmt doesn't work for this case, but it would be better to deepen the indent.
let Some(if_let_or_match) = IfLetOrMatch::parse(cx, init) { | |
let Some(if_let_or_match) = IfLetOrMatch::parse(cx, init) | |
{ | |
match if_let_or_match { |
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.
rustfmt doesn't support let chains right now: rust-lang/rustfmt#5203
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.
@koka831 Sorry for the my late reviewing. Can this be addressed?
I think it's not being formatted by rustfmt due to rust-lang/rustfmt#5203 @giraffate given how @koka831's github activity is quite interrupted (I dearly hope that nothing bad has happened to them), maybe it would be the best to press the "commit suggestion" button and then approve the PR? Alternatively, merge it with this formatting issue and then fix it in a later PR. Edit: I offer to file that PR. |
@est31 Thanks for the reminder!
I'll fix it later. Thanks! |
@bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
@est31 @giraffate I'm sorry for not being able to respond for a while due to health issues. Now that I have recovered, I hope to be active again:pray: |
(Also great to hear that you are doing better now) |
@koka831 No worries! I'm glad you're feeling better! |
Fix #10171
changelog: Sugg: [
manual_let_else
]: Now suggest the correct variable name#10175