Skip to content

Commit

Permalink
Silence superfluous_else rule for availability conditions (#5857)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Nov 16, 2024
1 parent 707a63d commit cafed07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
[SimplyDanny](https://github.com/SimplyDanny)
[#5787](https://github.com/realm/SwiftLint/issues/5787)

* Silence `superfluous_else` rule on `if` expressions with only a single
availability condition.
[SimplyDanny](https://github.com/SimplyDanny)
[#5833](https://github.com/realm/SwiftLint/issues/5833)

* Stop triggering the `control_statement` rule on closures being directly
called as conditions.
[SimplyDanny](https://github.com/SimplyDanny)
Expand Down
14 changes: 10 additions & 4 deletions Source/SwiftLintBuiltInRules/Rules/Style/SuperfluousElseRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ struct SuperfluousElseRule: OptInRule {
}
}
"""),
Example("""
if #available(iOS 13, *) {
return
} else {
deprecatedFunction()
}
"""),
],
triggeringExamples: [
Example("""
Expand Down Expand Up @@ -324,10 +331,9 @@ private extension SuperfluousElseRule {

private extension IfExprSyntax {
var superfluousElse: TokenSyntax? {
if elseKeyword == nil {
return nil
}
if !lastStatementExitsScope(in: body) {
guard elseKeyword != nil,
conditions.onlyElement?.condition.is(AvailabilityConditionSyntax.self) != true,
lastStatementExitsScope(in: body) else {
return nil
}
if let parent = parent?.as(IfExprSyntax.self) {
Expand Down

0 comments on commit cafed07

Please sign in to comment.