Skip to content

Fix false positives for unused_enumerated with result member access - #6544

Merged
SimplyDanny merged 6 commits into
realm:mainfrom
theamodhshetty:codex/fix-unused-enumerated-result-member
May 14, 2026
Merged

SimplyDanny merged 6 commits into
realm:mainfrom
theamodhshetty:codex/fix-unused-enumerated-result-member

Conversation

@theamodhshetty

Copy link
Copy Markdown
Contributor

Summary

  • avoid flagging .enumerated() when a higher-order call uses tuple members like ?.offset after the closure
  • keep tracking $0.element / $0.0 and seed the closure usage from result-member access on the surrounding call
  • add regression examples and the required changelog entry

Testing

  • swift test --filter UnusedEnumeratedRuleGeneratedTests
  • ./.build/debug/swiftlint lint Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnusedEnumeratedRule.swift CHANGELOG.md --strict
  • ./.build/debug/swiftlint lint --only-rule unused_enumerated /tmp/unused_enumerated_ok.swift --quiet
  • ./.build/debug/swiftlint lint --only-rule unused_enumerated /tmp/unused_enumerated_bad.swift --quiet

Closes #5881

@theamodhshetty theamodhshetty mentioned this pull request Mar 17, 2026
2 tasks done
@SwiftLintBot

SwiftLintBot commented Mar 17, 2026 •

Copy link
Copy Markdown
1 Warning
⚠️ This PR may need tests.
19 Messages
📖 Building this branch resulted in a binary size of 27639.02 KiB vs 27621.71 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.18 s vs 0.2 s on main (10% faster).
📖 Linting Alamofire with this PR took 0.2 s vs 0.19 s on main (5% slower).
📖 Linting Brave with this PR took 0.83 s vs 0.82 s on main (1% slower).
📖 Linting DuckDuckGo with this PR took 3.63 s vs 3.63 s on main (0% slower).
📖 Linting Firefox with this PR took 1.56 s vs 1.57 s on main (0% faster).
📖 Linting Kickstarter with this PR took 0.97 s vs 0.97 s on main (0% slower).
📖 Linting Moya with this PR took 0.16 s vs 0.16 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 0.37 s vs 0.35 s on main (5% slower).
📖 Linting Nimble with this PR took 0.19 s vs 0.16 s on main (18% slower).
📖 Linting PocketCasts with this PR took 0.96 s vs 0.94 s on main (2% slower).
📖 Linting Quick with this PR took 0.15 s vs 0.15 s on main (0% slower).
📖 Linting Realm with this PR took 0.4 s vs 0.4 s on main (0% slower).
📖 Linting Sourcery with this PR took 0.32 s vs 0.28 s on main (14% slower).
📖 Linting Swift with this PR took 0.5 s vs 0.49 s on main (2% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 3.88 s vs 3.87 s on main (0% slower).
📖 Linting VLC with this PR took 0.27 s vs 0.25 s on main (8% slower).
📖 Linting Wire with this PR took 2.01 s vs 2.02 s on main (0% faster).
📖 Linting WordPress with this PR took 1.37 s vs 1.37 s on main (0% slower).

Generated by 🚫 Danger

@theamodhshetty

Copy link
Copy Markdown
Contributor Author

Pushed a small follow-up in eb3575a after the first CI pass.

The original version reset nextClosureId / lastEnumeratedPosition inside the same if let that introduced shorthand bindings with those names, which newer matrix legs were treating as the shadowed locals instead of the stored properties. This update just renames the local bindings so the property resets stay unambiguous across toolchains.

@rgoldberg

Copy link
Copy Markdown

Does anything else need to be done to merge this PR?

@theamodhshetty

Copy link
Copy Markdown
Contributor Author

I don’t think so from my side. The follow-up for the CI/toolchain issue is already in, and the checks are green now. If there’s anything else you want adjusted before merge, I’m happy to make it.

@SimplyDanny SimplyDanny left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the contribution!

The idea looks good. I have a few remarks though ...

@theamodhshetty

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up in 7ba4ed1 that addresses the review points from SimplyDanny.

Changes in this pass:

  • collapsed the parentCall / trailingClosure setup into the cleaner form from the suggestions
  • reworked the closure tracking so only tracked closures carry an enumeratedPosition, instead of using a nullable position on every stack entry
  • tightened the result-member ancestor walk so it stops once we leave the optional/member-access chain, while still continuing past unrelated member names until it can see a later offset / element

Validation rerun:

  • swift test --filter UnusedEnumeratedRuleGeneratedTests
  • ./.build/debug/swiftlint lint Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnusedEnumeratedRule.swift --strict

If you want this split up differently, I can adjust it.

@theamodhshetty

Copy link
Copy Markdown
Contributor Author

Thanks for the follow-up review. I pushed 1b21940 with those cleanups.

What changed:

  • inlined the one-use closure stack helpers
  • moved result-member lookup to ExprSyntax and made it recurse through member access, optional chaining, force unwraps, and parenthesized single-expression tuples
  • added a non-triggering example for a parenthesized .first { ... } result followed by ?.offset

Validation rerun:

  • swift test --filter UnusedEnumeratedRuleGeneratedTests
  • ./.build/debug/swiftlint lint Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnusedEnumeratedRule.swift --strict

@SimplyDanny
SimplyDanny force-pushed the codex/fix-unused-enumerated-result-member branch from 6317104 to eafc716 Compare May 14, 2026 17:29
@SimplyDanny
SimplyDanny enabled auto-merge (squash) May 14, 2026 17:30
@SimplyDanny
SimplyDanny disabled auto-merge May 14, 2026 17:30
@SimplyDanny
SimplyDanny merged commit 98776e3 into realm:main May 14, 2026
18 checks passed
nadeemnali pushed a commit to nadeemnali/SwiftLint that referenced this pull request May 17, 2026
nadeemnali pushed a commit to nadeemnali/SwiftLint that referenced this pull request May 17, 2026
nadeemnali pushed a commit to nadeemnali/SwiftLint that referenced this pull request May 17, 2026
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.

False positive for unused_enumerated

4 participants