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

Fix closure argument list parsing bug in preferForLoop rule #1712

Merged
merged 1 commit into from
Jun 3, 2024

Conversation

calda
Copy link
Collaborator

@calda calda commented Jun 2, 2024

This PR fixes #1709.

This code was being parsed incorrectly:

dict.forEach { (header: (key: String, value: String)) in
    print(header.key)
    print(header.value)
}

so was unexpectedly converted to:

for (header, value) in dict {
    print(header.key)
    print(header.value)
}

instead of:

for header in dict {
    print(header.key)
    print(header.value)
}

Copy link

codecov bot commented Jun 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.20%. Comparing base (f487996) to head (adea102).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1712      +/-   ##
===========================================
+ Coverage    95.13%   95.20%   +0.07%     
===========================================
  Files           20       20              
  Lines        23124    23135      +11     
===========================================
+ Hits         21998    22025      +27     
+ Misses        1126     1110      -16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nicklockwood
Copy link
Owner

I think maybe this only works by coincidence? The key/value labels happen to match the defaults in Dictionary, but what if different labels had been used instead?

@calda
Copy link
Collaborator Author

calda commented Jun 2, 2024

Just checked -- if you reassign the labels, the compiler emits a warning suggesting you not do this:

let dict = ["foo": "bar"]

// ⚠️ warning: tuple conversion from '(key: String, value: String)' to '(a: String, b: String)' mismatches labels
dict.forEach { (header: (a: String, b: String)) in
    print(header.a)
    print(header.b)
}

So while renaming tuple keys in that way is technically possible, it seems like an anti-pattern that I think is ok for us to not support.

@alistra
Copy link

alistra commented Jun 2, 2024

LGTM

@nicklockwood nicklockwood merged commit 56950a5 into nicklockwood:develop Jun 3, 2024
9 checks passed
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.

None yet

3 participants