Skip to content

Rule request: Avoid Yoda conditions #1924

Closed
@marcelofabri

Description

New Issue Checklist

  • Updated SwiftLint to the latest version
    I searched for existing GitHub issues

Rule Request

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

In Swift, you need a var or let keyword to do an assignment inside a condition (if, guard, etc).
Thus, you only get the disadvantages of Yoda conditions, but no benefits.

  1. Provide several examples of what would and wouldn't trigger violations.
// should trigger
if 42 == foo {}
guard "str" == bar else { return }
while 10 == baz { }

// shouldn't trigger
if foo == 42 {}
guard bar == "str" else { return }
while baz == 10 { }
if foo == bar {}
  1. Should the rule be configurable, if so what parameters should be configurable?

Just the severity.

  1. Should the rule be opt-in or enabled by default? Why?
    See README.md for guidelines on when to mark a
    rule as opt-in.

I think this might cause some false positives, so I'd say opt-in. But it could be enabled by default if we can avoid those false positives.

Metadata

Metadata

Assignees

No one assigned

    Labels

    rule-requestRequests for a new rules.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

      Participants

      @jpsim@marcelofabri

      Issue actions

        Rule request: Avoid Yoda conditions · Issue #1924 · realm/SwiftLint