Skip to content

Fix checking if newline is needed before else in let-else statement#5902

Merged
ytmimi merged 5 commits into
rust-lang:masterfrom
rhysd:issue-5901
Sep 9, 2023
Merged

Fix checking if newline is needed before else in let-else statement#5902
ytmimi merged 5 commits into
rust-lang:masterfrom
rhysd:issue-5901

Conversation

@rhysd

@rhysd rhysd commented Sep 6, 2023

Copy link
Copy Markdown
Contributor

Fixes #5901

@ytmimi

ytmimi commented Sep 6, 2023

Copy link
Copy Markdown
Contributor

@rhysd I haven't had a chance to review this yet, but I wanted to see how your changes handle this case:

fn issue5901() {
    #[cfg(target_os = "linux")]
    // some comment between attr and let-else
    let Some(x) = foo() else { return; };
}

fn issue5901() {
    #[cfg(target_os = "linux")]
    /* some comment between attr and let-else */
    let Some(x) = foo() else { return; };
}

@rhysd

rhysd commented Sep 6, 2023

Copy link
Copy Markdown
Contributor Author

Thank you for the point. I'll try to add new test.

@rhysd

rhysd commented Sep 6, 2023

Copy link
Copy Markdown
Contributor Author

I confirmed that this fix didn't work for the comment between the attributes and let-else statement. I'll have a look whether it can be fixed.

 fn main() {
     #[cfg(target_os = "linux")]
     // this is test
-    let x = 42 else { todo!() };
+    let x = 42
+    else {
+        todo!()
+    };
 }

@ytmimi

ytmimi commented Sep 6, 2023

Copy link
Copy Markdown
Contributor

@rhysd thanks for taking a look 🙏🏼. Before you keep working on this I also want to point out that any change needs to be version gated since let-else support was released as stable and we can't break our formatting guarantees.

@rhysd

rhysd commented Sep 6, 2023

Copy link
Copy Markdown
Contributor Author

@ytmimi Thanks for the information. I'll check the documentation. And I may find a way to strip things before let correctly. I'll update this branch once I add more tests and the version gate.

@rhysd

rhysd commented Sep 6, 2023

Copy link
Copy Markdown
Contributor Author

@ytmimi I force-pushed the fix. I added more tests and v1 version gate as well. Is there any place to add tests for v1 formatting? Currently only v2 formatting is tested. Please let me know if I should create some test case like tests/source/let_else_v1.rs.

@ytmimi

ytmimi commented Sep 6, 2023

Copy link
Copy Markdown
Contributor

You should create a let_else_v2.rs test for this. See the Create test cases section of the Contributing guide for details on how to add configuration values to tests via comments.

Comment thread src/items.rs Outdated
Comment thread src/items.rs
Comment thread tests/source/let_else.rs

@ytmimi ytmimi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks again for taking the time to look into this. I think you're on the right track with getting the let_kw_offset early on. As mentioned, there are two other calls that use the result below for their calculations and those need to be changed as well.

@rhysd

rhysd commented Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

@ytmimi Thank you for your review comments. I updated this branch to resolve them.

  • Preferred checking Version::Two to Version::One
  • Added test cases for both v1 and v2 formatting
  • Fixed calculating available space stripping things before let

Comment thread src/items.rs Outdated
Comment thread tests/source/let_else.rs Outdated
@rhysd

rhysd commented Sep 8, 2023

Copy link
Copy Markdown
Contributor Author

@ytmimi Thank you for your review comments again. I addressed them.

  • Simplified checking if the else block can be single-lined
  • Preferred todo!() expression to return; statement
  • Imported more v1 test cases into let_else_v2.rs

@ytmimi ytmimi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for helping to fix this issue and for your first contribution to rustfmt 🥳

@ytmimi

ytmimi commented Sep 8, 2023

Copy link
Copy Markdown
Contributor

As a last step I want to run our diff-check job, It should pass since the changes are version gated, but I still want to go through this process.

@ytmimi ytmimi merged commit 262feb3 into rust-lang:master Sep 9, 2023
@ytmimi ytmimi added the release-notes Needs an associated changelog entry label Sep 9, 2023
@ytmimi ytmimi removed the release-notes Needs an associated changelog entry label Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustfmt always inserts newline before else in let-else statement with attribute

3 participants