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

for_where with single if for enumed enumerated array #1968

Closed
2 tasks done
nineboxes opened this issue Dec 5, 2017 · 4 comments
Closed
2 tasks done

for_where with single if for enumed enumerated array #1968

nineboxes opened this issue Dec 5, 2017 · 4 comments
Labels
bug Unexpected and reproducible misbehavior.

Comments

@nineboxes
Copy link

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 Unexpected and reproducible misbehavior. label Dec 5, 2017
@marcelofabri
Copy link
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.

@nineboxes
Copy link
Author

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
Copy link
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
Copy link
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
Labels
bug Unexpected and reproducible misbehavior.
Projects
None yet
Development

No branches or pull requests

4 participants