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

Contains over `filter{}.count > 0` #2803

Closed
dirtydanee opened this issue Jul 8, 2019 · 1 comment · Fixed by #2841

Comments

@dirtydanee
Copy link
Contributor

commented Jul 8, 2019

New Issue Checklist

New rule request

On collections we can filter the elements based on a predicate, count them and compare against zero to check if there is at least one element satisfying that predicate. However, we can use the contain API to achieve the same result.

What should trigger?

struct Foo {
    let bar: Bool
}

let foos = [Foo(bar: false), Foo(bar: true)]
let canBar: Bool = foos.filter { $0.bar }.count > 0
struct Foo {
    let bar: Bool
}

let foos = ["1": Foo(bar: false),"2": Foo(bar: true)]
let canBar = foos.filter { $1.bar }.count > 0
let canNumber2 = [1, 2, 3].filter { $0 == 2 }.count > 0
let numbers: Set<Int> = [1, 2, 3]
numbers.filter { $0 == 2 }.count > 0

What should not trigger?

struct Foo {
    let bar: Bool
}

let foos = [Foo(bar: false), Foo(bar: true)]
let canBar: Bool = foos.contains(where: { $0.bar })
struct Foo {
    let bar: Bool
}

let foos = ["1": Foo(bar: false),"2": Foo(bar: true)]
let canBar = foos.contains { $1.bar }
let canNumber2 = [1, 2, 3].contains(2)
let numbers: Set<Int> = [1, 2, 3]
numbers.contains(2)

The rules should not be configurable and could be enabled by default.

@idrougge

This comment has been minimized.

Copy link

commented Jul 10, 2019

empty_count would already catch this, but a separate rule might offer better fixits.

marcelofabri added a commit that referenced this issue Aug 25, 2019
marcelofabri added a commit that referenced this issue Aug 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.