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

empty_count: false positive when input argument called count #2012

Closed
yas375 opened this issue Jan 20, 2018 · 5 comments
Closed

empty_count: false positive when input argument called count #2012

yas375 opened this issue Jan 20, 2018 · 5 comments
Labels
duplicate Issues that already exist in other terms.

Comments

@yas375
Copy link

yas375 commented Jan 20, 2018

Bug Report

$ echo "private func upcomingRequests(count: Int) {\nguard count > 0 else {" | swiftlint lint --no-cache --use-stdin --enable-all-rules
Loading configuration from '.swiftlint.yml'
<nopath>:2:7: error: Empty Count Violation: Prefer checking `isEmpty` over comparing `count` to zero. (empty_count)
Done linting! Found 1 violation, 1 serious in 1 file.

Environment

  • SwiftLint: 0.24.2
  • Installation method used: Homebrew
  • .swiftlint.yml:
opt_in_rules:
  - empty_count

This code triggers error message:

private func upcomingRequests(count: Int) {
    guard count > 0 else {
      return
    }
    // ...
}

2018-01-19_2345

@marcelofabri
Copy link
Collaborator

Duplicate of #827.

@marcelofabri marcelofabri added the duplicate Issues that already exist in other terms. label Jan 22, 2018
@yas375
Copy link
Author

yas375 commented Jan 22, 2018

@marcelofabri I did search before opening this issue and I did found the one you referenced and that one didn't seem to be the same. That issue is about the fact that NSMapTable doesn't have isEmpty, so the SwiftLint should warn about switching to isEmpty for it. While the issue I'm experiencing is that I'm getting lint error if I have a variable called count and if I compare it with zero. Maybe consider updating that PR's description to mention both bugs. Hope it helps. And sorry again if it felt as duplicate.

@SDGGiesbrecht
Copy link
Contributor

@yas375, the underlying issue is the same. (Though it is admittedly not obvious until further into the comments, so do not worry about it.)

Basically, SwiftLint only has access to the name of the symbol count; it has no way of knowing what the symbol describes (i.e. Variable or property? Property on what type?). This makes both issues unfixable.

The only thing to do is follow @jpsim’s advice,

[B]e aware of the false‐positives, and don’t use opt‐in rules unless you’re aware of their tradeoffs and find them acceptable.

Please know that we marked this as a duplicate to help organize the list of issues, not to label you as too lazy to search. ;) We are thankful for your report.

@yas375
Copy link
Author

yas375 commented Jan 22, 2018

Thanks Jeremy 👍

@yo1995
Copy link

yo1995 commented Jul 24, 2020

FYI: I run into the same linter error when checking against NSDictionary.count. Switching to NSDictionary.allKeys.isEmpty workaround the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues that already exist in other terms.
Projects
None yet
Development

No branches or pull requests

4 participants