Swiftlint: Rule request: No explicit `== true` or `== false`

Created on 8 May 2017  Â·  9Comments  Â·  Source: realm/SwiftLint

I occasionally come across code like this:

if condition == true { … }

which just be written as:

if condition { … }

I'd love to have a rule that disallowed == true and == false. (And != true, != false, which I haven't seen, but would be far worse.)

rule-request

Most helpful comment

For the case where comparing with Optional<Bool> . I think it's clearer to use ?? true|false operator than just compare with == true|false.
If I look again at @SDGGiesbrecht 's example at the call site.

if someFunction() == true {
}

It's not clear for the reader that someFunction() might return nil.

But

if someFunction() ?? false {
}

It's very clear that the function might return nil and also clear what to do in that case.

All 9 comments

Agreed. This is a good starter rule to write if someone's interested in contributing to SwiftLint but finds other requested rules daunting.

Might want to also validate this rule with the boolean literal on the left hand side:

if true == condition {}
if false != condition {}

This is a good starter rule.

I’m not so sure. It’s more complicated than it looks at first glance.

For example, what about Bool??

func someFunction() -> Bool? {
    return true
}

if someFunction() == true {
    // Should the rule trigger?
}

if someFunction() {
    // This will not compile.
}

if someFunction() ?? false {
    // This would satisfy both the rule and the compiler,
    // but is this really what we want?
}

I'd like to add to also throw a violation when comparing to the same object.

if someObj == someObj {} // always true
if objA != objA {} // always false

Ah, cursed optionals strike again! @masters3d could you please file a new ticket for that, as its implementation could be entirely different than the original request.

For the case where comparing with Optional<Bool> . I think it's clearer to use ?? true|false operator than just compare with == true|false.
If I look again at @SDGGiesbrecht 's example at the call site.

if someFunction() == true {
}

It's not clear for the reader that someFunction() might return nil.

But

if someFunction() ?? false {
}

It's very clear that the function might return nil and also clear what to do in that case.

You could also use an extension (since Swift 3.1):

extension Optional where Wrapped == Bool {
    var isTrue: Bool {
        switch self {
        case .none:
            return false
        case .some(let value):
            return value
        }
    }
}

But that extension assumes nil is equivalent to false which isn't.

IMO it doesn't assume. It'd be the same as using == true or ?? false. If you need another behavior based on if it's nil, it's just a matter of don't using the extension.

Ternary operations should also be considered;

index < activeIndex ? true : false
Was this page helpful?
0 / 5 - 0 ratings

Related issues

castus picture castus  Â·  3Comments

ziryanov picture ziryanov  Â·  3Comments

bourquep picture bourquep  Â·  3Comments

muzamilhassan1987 picture muzamilhassan1987  Â·  3Comments

zntfdr picture zntfdr  Â·  3Comments