-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Rule request: No explicit == true
or == false
#1502
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 {} |
I’m not so sure. It’s more complicated than it looks at first glance. For example, what about 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 if someFunction() == true {
} It's not clear for the reader that someFunction() might return But if someFunction() ?? false {
} It's very clear that the function might return |
You could also use an extension Optional where Wrapped == Bool {
var isTrue: Bool {
switch self {
case .none:
return false
case .some(let value):
return value
}
}
} |
But that extension assumes |
IMO it doesn't assume. It'd be the same as using |
Ternary operations should also be considered; index < activeIndex ? true : false |
it is 2021 now. will this rule be added? |
I occasionally come across code like this:
which just be written as:
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.)The text was updated successfully, but these errors were encountered: