Skip to content
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

Add redundantProperty rule #273

Merged
merged 1 commit into from
Jun 14, 2024
Merged

Add redundantProperty rule #273

merged 1 commit into from
Jun 14, 2024

Conversation

calda
Copy link
Member

@calda calda commented Jun 7, 2024

Summary

This PR proposes a new rule to avoid defining properties that are then returned immediately. Instead, return the value directly.

Autocorrect support is implemented in the SwiftFormat redundantProperty rule, which as added in nicklockwood/SwiftFormat#1654.

// 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)
}

Reasoning

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.

Copy link
Contributor

@mannylopez mannylopez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. Can't think of a reason why we wouldn't want to simplify this.

@calda calda merged commit 7a89953 into master Jun 14, 2024
5 checks passed
@calda calda deleted the cal--redundantProperty branch June 14, 2024 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants