nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.63k stars 623 forks source link

Add redundantProperty rule #1654

Closed calda closed 3 months ago

calda commented 3 months ago

This PR adds a new redundantProperty rule, which removes a property declaration if that property is just returned immediately. For example:

  func foo() -> Foo {
-   let foo = Foo()
-   return foo
+   return Foo()
  }

Here's an example of a pattern I saw somewhat frequently in Airbnb's codebase:

func foo() -> Foo {
    let foo: Foo
    switch condition {
    case true:
        foo = Foo(bar)
    case false:
        foo = Foo(baaz)
    }

    return foo
}

The conditionalAssignment rule converts this to let foo = switch { ... }; return foo. Now with both conditionalAssignment and redundantProperty enabled, it's instead converted to an even simpler form:

func foo() -> Foo {
    switch condition {
    case true:
        Foo(bar)
    case false:
        Foo(baaz)
    }
}
codecov[bot] commented 3 months ago

Codecov Report

Attention: Patch coverage is 96.18209% with 52 lines in your changes are missing coverage. Please review.

Project coverage is 95.19%. Comparing base (088875d) to head (fe2ae2d). Report is 37 commits behind head on develop.

:exclamation: Current head fe2ae2d differs from pull request most recent head 89e0129. Consider uploading reports for the commit 89e0129 to get more accurate results

Files Patch % Lines
Sources/Options.swift 85.62% 23 Missing :warning:
Sources/GitHelpers.swift 82.85% 18 Missing :warning:
Sources/FormattingHelpers.swift 98.24% 4 Missing :warning:
Sources/ParsingHelpers.swift 97.40% 4 Missing :warning:
Sources/Inference.swift 93.93% 2 Missing :warning:
Sources/ShellHelpers.swift 96.42% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1654 +/- ## =========================================== + Coverage 95.15% 95.19% +0.04% =========================================== Files 20 20 Lines 22575 22601 +26 =========================================== + Hits 21482 21516 +34 + Misses 1093 1085 -8 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

nicklockwood commented 3 months ago

Oh, I love this one! 😍

nicklockwood commented 3 months ago

Worth noting: I'd like to enable this rule by default, but people do sometimes deliberately assign the result of a complex expression to a variable before returning so that they can use a breakpoint to inspect its value. I wonder if there's a heuristic to detect cases like that.

calda commented 3 months ago

Hmmmm, that's a reasonable use case but I can't think of a reliable heuristic for it. We could use some sort of token count or character count threshold that is enabled by default, if you think that's valuable.

calda commented 3 months ago

Updated to resolve merge conflicts with https://github.com/nicklockwood/SwiftFormat/pull/1655

btw, looks like there are test failures on develop starting with https://github.com/nicklockwood/SwiftFormat/commit/7c7941ee7f237b0928c00d0f58dc1240a52ac0eb