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

[0.55.1] False positive on unused_element where element and index are used separately in subsequent blocks #5600

Open
2 tasks done
michaeleustace opened this issue May 22, 2024 · 2 comments
Assignees
Labels

Comments

@michaeleustace
Copy link

New Issue Checklist

Describe the bug

The rule should not trigger when the enumerated index and element are used in separate subsequent blocks

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.55.1
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? Cocoapods
  • Paste your configuration file:
# insert yaml contents here
  • Are you using nested configurations? No
    If so, paste their relative paths and respective contents.
  • Which Xcode version are you using (check xcodebuild -version)? 15.3
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
protocol Rule {
  func isValid() -> Bool
}

let rules: [Rule] = [rule1, rule2]
let messages = ["msg1", "msg2"]

// This triggers a violation:
rules.enumerated()
    .first { $0.element.isValid() == false }
    .flatMap { messages[$0.offset] }
@mildm8nnered mildm8nnered self-assigned this May 23, 2024
@michaeleustace michaeleustace changed the title [0.55.1] False positive on unused_element where element and index are used separately in subsequent blocks [0.55.1] False positive on unused_element where element and index are used separately in subsequent blocks May 23, 2024
@mildm8nnered
Copy link
Collaborator

So unfortunately we do only cover the first block - I couldn't find a good way to keep track of the variables through subsequent blocks - see #5498 for discussion. There were no examples of subsequent block usage in the open source projects that SwiftLint gets tested against, so we hoped that this would not affect many people.

If that particular idiom is common in your codebase, you could disable the rule locally, or in your configuration.

@SimplyDanny
Copy link
Collaborator

While the advice the rule is providing might be misleading in this case, it still highlights an issue in the code. Because it could indeed be rewritten entirely without the .enumerated() as:

rules.firstIndex { !$0.isValid() }.flatMap { messages[$0] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants