Skip to content

Check for else statement during where clause substitution #380

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

Merged
merged 1 commit into from
Jul 28, 2022

Conversation

chuckluck44
Copy link
Contributor

Summary

The UseWhereClausesInForLoops rule fails to check for an else keyword when replacing a single if statement. The formatted statement drops the behavior defined in the else body completely.

Example

for i in [0, 1, 2, 3] {
   if i > 30 {
     print(i)
   } else {
     print(i)
   }
 }

// Is formatted as:

for i in [0, 1, 2, 3] where i > 30 {
   print(i)
 }

Solution

This fix adds an additional check to ensure that there is no else case attached to the if statement. Unit tests were updated to cover this behavior.

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

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

This looks straightforward and obviously correct. Thanks for the fix!

@allevato allevato merged commit 0e9a5b9 into swiftlang:main Jul 28, 2022
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.

2 participants