Skip to content
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

Use original variable name in the suggestion #10175

Merged
merged 4 commits into from
May 18, 2023
Merged

Conversation

koka831
Copy link
Contributor

@koka831 koka831 commented Jan 7, 2023

Fix #10171


changelog: Sugg: [manual_let_else]: Now suggest the correct variable name
#10175

@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2023

r? @giraffate

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 7, 2023
@@ -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 };`
Copy link
Contributor

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.

Copy link
Contributor Author

@koka831 koka831 Jan 10, 2023

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,
};

Copy link
Contributor

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!

Copy link
Member

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.

Comment on lines 159 to 163
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})")
},
Copy link
Contributor Author

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

Copy link
Member

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 { _ };

Copy link
Member

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. 👍

Copy link
Contributor Author

@koka831 koka831 Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@est31 Thank you for the quick review!
I see the example above should be handled like below:

let SomeEnum::Val(foo, 0) = e() { v } else { ... };
               // ^ replaced ideally

In this PR, I made a change to keep left if TupleStruct has more than two arguments. ref
I hope it's enough for now:pray:

@@ -37,7 +37,6 @@ declare_clippy_lint! {
/// Could be written:
///
/// ```rust
/// # #![feature(let_else)]
Copy link
Contributor Author

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, yes.

@@ -248,4 +254,13 @@ fn not_fire() {
Some(value) => value,
_ => macro_call!(),
};

fn e() -> Variant {
Copy link
Member

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).

Copy link
Contributor Author

@koka831 koka831 Feb 1, 2023

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
@est31
Copy link
Member

est31 commented Mar 1, 2023

Friendly ping @giraffate -- This is ready for review (and IMO good to be merged)

@giraffate
Copy link
Contributor

Thanks for pinging! I'll review this in a few days.

Copy link
Contributor

@giraffate giraffate left a 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) {
Copy link
Contributor

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.

Suggested change
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 {

Copy link
Member

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

Copy link
Contributor

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?

@est31
Copy link
Member

est31 commented May 16, 2023

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.

@giraffate
Copy link
Contributor

@est31 Thanks for the reminder!

@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.

I'll fix it later. Thanks!

@giraffate
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented May 17, 2023

📌 Commit 07c8c50 has been approved by giraffate

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented May 18, 2023

⌛ Testing commit 07c8c50 with merge 7e18451...

@bors
Copy link
Collaborator

bors commented May 18, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: giraffate
Pushing 7e18451 to master...

@bors bors merged commit 7e18451 into rust-lang:master May 18, 2023
@koka831
Copy link
Contributor Author

koka831 commented Sep 25, 2023

@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:

@koka831 koka831 deleted the fix/10171 branch September 25, 2023 12:49
@est31
Copy link
Member

est31 commented Sep 25, 2023

@koka831 thanks for the update! In the meantime, I have made #10797 where I addressed the one concern for this PR and generally greatly improved the state of patterns in the PR and the follow up work.

@est31
Copy link
Member

est31 commented Sep 25, 2023

(Also great to hear that you are doing better now)

@giraffate
Copy link
Contributor

@koka831 No worries! I'm glad you're feeling better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

manual_let_else produces wrong variable name
5 participants