Skip to content

Commit

Permalink
Add redundantProperty rule (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
calda authored Jun 14, 2024
1 parent f37baa7 commit 7a89953
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3601,6 +3601,42 @@ _You can enable the following settings in Xcode by running [this script](resourc

</details>

* <a id='redundant-property'></a>(<a href='#redundant-property'>link</a>) **Avoid defining properties that are then returned immediately.** Instead, return the value directly. [![SwiftFormat: redundantProperty](https://img.shields.io/badge/SwiftFormat-redundantProperty-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#redundantProperty)

<details>

### Why?

Property declarations that are immediately returned are typically redundant and unnecessary. Sometimes these are unintentionally created as the byproduct of refactoring. Cleaning them up automatically simplifies the code. In some cases this also results in the `return` keyword itself being unnecessary, further simplifying the code.

```swift
// WRONG
var spaceship: Spaceship {
let spaceship = spaceshipBuilder.build(warpDrive: warpDriveBuilder.build())
return spaceship
}

// RIGHT
var spaceship: Spaceship {
spaceshipBuilder.build(warpDrive: warpDriveBuilder.build())
}

// WRONG
var spaceship: Spaceship {
let warpDrive = warpDriveBuilder.build()
let spaceship = spaceshipBuilder.build(warpDrive: warpDrive)
return spaceship
}

// RIGHT
var spaceship: Spaceship {
let warpDrive = warpDriveBuilder.build()
return spaceshipBuilder.build(warpDrive: warpDrive)
}
```

</details>

**[ back to top](#table-of-contents)**

## File Organization
Expand Down
1 change: 1 addition & 0 deletions Sources/AirbnbSwiftFormatTool/airbnb.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
--rules redundantVoidReturnType
--rules redundantOptionalBinding
--rules redundantInternal
--rules redundantProperty
--rules unusedArguments
--rules spaceInsideBrackets
--rules spaceInsideBraces
Expand Down

0 comments on commit 7a89953

Please sign in to comment.