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

for_where with single if for enumed enumerated array #1968

Closed
pacification opened this Issue Dec 5, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@pacification

pacification commented Dec 5, 2017

New Issue Checklist

Bug Report

When i try to enumerate array that contains enum values and find the index of some one with single if, swiftlint shows me for_where violation with single if warning. Probably for this situation there should be no warning?

import Foundation

enum Value {
    case valueA, valueB
}

func firstIndexOfValueB(in array: [Value]) -> Int? {
    for (index, value) in array.enumerated() { // `for_where` with single `if` warning here
        if case .valueB = value {
            return index
        }
    }

    return nil
}

print(firstIndexOfValueB(in: [.valueA, .valueA, .valueB]) ?? -1)

@marcelofabri marcelofabri added the bug label Dec 5, 2017

@marcelofabri

This comment has been minimized.

Show comment
Hide comment
@marcelofabri

marcelofabri Dec 5, 2017

Collaborator

In your case you could just use where value == .valueB, but I agree that the rule shouldn't warn if it's a pattern match if.

Collaborator

marcelofabri commented Dec 5, 2017

In your case you could just use where value == .valueB, but I agree that the rule shouldn't warn if it's a pattern match if.

@pacification

This comment has been minimized.

Show comment
Hide comment
@pacification

pacification Dec 5, 2017

Hi.

In your case you could just use where value == .valueB

Sure, this works fine for this case. But when we add some associated value like .valueB(number: Int) we cannot just type where value == .valueB for free - we should implement Equatable protocol. So, hope this will be fixed :)

pacification commented Dec 5, 2017

Hi.

In your case you could just use where value == .valueB

Sure, this works fine for this case. But when we add some associated value like .valueB(number: Int) we cannot just type where value == .valueB for free - we should implement Equatable protocol. So, hope this will be fixed :)

@jpsim

This comment has been minimized.

Show comment
Hide comment
@jpsim

jpsim Dec 20, 2017

Collaborator

Agree, this is a bug, but the sample shared in the first comment isn't why. Here's a sample that can't be changed to using a for where clause:

enum Value {
    case valueA, valueB(a: Int)
}

func firstIndexOfValueB(in array: [Value]) -> Int? {
    for (index, value) in array.enumerated() { // `for_where` with single `if` warning here
        if case .valueB(_) = value {
            return index
        }
    }
    return nil
}
Collaborator

jpsim commented Dec 20, 2017

Agree, this is a bug, but the sample shared in the first comment isn't why. Here's a sample that can't be changed to using a for where clause:

enum Value {
    case valueA, valueB(a: Int)
}

func firstIndexOfValueB(in array: [Value]) -> Int? {
    for (index, value) in array.enumerated() { // `for_where` with single `if` warning here
        if case .valueB(_) = value {
            return index
        }
    }
    return nil
}
@tomquist

This comment has been minimized.

Show comment
Hide comment
@tomquist

tomquist Feb 8, 2018

Contributor

I think this change was not necessary and should be undone. This is not a bug as the example can be changed to:

enum Value {
    case valueA, valueB(a: Int)
}

func firstIndexOfValueB(in array: [Value]) -> Int? {
    for case let (index, .valueB) in array.enumerated() {
        return index
    }
    return nil
}

Pattern matching also works in for loops.

Contributor

tomquist commented Feb 8, 2018

I think this change was not necessary and should be undone. This is not a bug as the example can be changed to:

enum Value {
    case valueA, valueB(a: Int)
}

func firstIndexOfValueB(in array: [Value]) -> Int? {
    for case let (index, .valueB) in array.enumerated() {
        return index
    }
    return nil
}

Pattern matching also works in for loops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment