nicklockwood / SwiftFormat

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

`redundantOptionalBinding` doesn't seem to recognize statements in closures #1734

Closed oslo-slin closed 2 weeks ago

oslo-slin commented 2 weeks ago

Hello,

Wanted to post and see if anyone has encountered any similar issues. In the current project's codebase I'm working on, we have been using guard let self = self else { return } as a statement to capture weak self for optional binding purposes. Using the latest patch 0.54.0, it seems like SwiftFormat isn't able to capture such statement.

Attaching a code snippet of the actual piece Screenshot 2024-06-14 at 3 52 06 PM

We have been using a configuration file as the approach to specify rules, the below is what the current set up:

# file options
--exclude **/fastlane,**/Pods,**/Pod,**/vendor,**/TemplateMVP,**/TemplateModule,**/ZeplinStyleGuideExporter

# disabled rules
--disable all

# enabled rules (specifically)
--enable acronyms
--enable blankLineAfterImports
--enable blankLinesBetweenChainedFunctions
--enable blankLinesBetweenScopes
--enable braces
--enable conditionalAssignment
--enable consecutiveBlankLines
--enable consecutiveSpaces
--enable consistentSwitchCaseSpacing
--enable duplicateImports
--enable elseOnSameLine
--enable emptyBraces
--enable enumNamespaces
--enable extensionAccessControl
--enable fileHeader
--enable headerFileName
--enable hoistAwait
--enable hoistPatternLet
--enable hoistTry
--enable indent
--enable initCoderUnavailable
--enable leadingDelimiters
--enable linebreakAtEndOfFile
--enable modifierorder
--enable numberFormatting
--enable preferForLoop
--enable preferKeyPath
--enable redundantBackticks
--enable redundantBreak
--enable redundantClosure
--enable redundantExtensionACL
--enable redundantFileprivate
--enable redundantGet
--enable redundantInternal
--enable redundantInit
--enable redundantLet
--enable redundantLetError
--enable redundantNilInit
--enable redundantObjc
--enable redundantOptionalBinding
--enable redundantParens
--enable redundantPattern
--enable redundantRawValues
--enable redundantReturn
--enable redundantSelf
--enable redundantStaticSelf
--enable redundantType
--enable redundantTypedThrows
--enable redundantVoidReturnType
--enable semicolons
--enable sortDeclarations
--enable sortImports
--enable sortTypeAliases
--enable spaceAroundBraces
--enable spaceAroundBrackets
--enable spaceAroundComments
--enable spaceAroundGenerics
--enable spaceAroundOperators
--enable spaceAroundParens
--enable spaceInsideBraces
--enable spaceInsideBrackets
--enable spaceInsideComments
--enable spaceInsideGenerics
--enable spaceInsideParens
--enable strongOutlets
--enable strongifiedSelf
--enable todos
--enable trailingClosures
--enable trailingCommas
--enable trailingSpace
--enable typeSugar
--enable unusedArguments
--enable void
--enable wrap
--enable wrapArguments
--enable wrapAttributes
--enable wrapLoopBodies
--enable wrapMultilineStatementBraces
--enable wrapSingleLineComments

# format options
--allman false
--binarygrouping 4,8
--closingparen balanced
--commas inline
--comments indent
--decimalgrouping 3,6
--elseposition same-line
# ... some other rules below, too lengthy so they aren't included

Thanks in advance if you'd be able to get to it, appreciate your time!

calda commented 2 weeks ago

Do you specify a --swiftversion in the config file? This functionality was added in Swift 5.7 so is only enabled if a --swiftversion of 5.7 or later is specified.

calda commented 2 weeks ago

I tested the sample code you shared with redundantOptionalBinding enabled and --swiftversion 5.7, and it removed the redundant = self as expected.

oslo-slin commented 2 weeks ago

@calda Thanks for getting back to this thread. Specifying the minimum minor version of Swift 5.7 was indeed the key to enforce redundant optional binding, resolving / closing this issue. Appreciate your help!