SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

False positive on `redundant_discardable_let` rule in SwiftUI `@ViewBuilder`

Open ObjectiveCesar opened this issue 6 months ago • 2 comments
trafficstars

New Issue Checklist

Bug Description

The rule redundant_discardable_let is falsely triggered inside SwiftUI @ViewBuilder implementation, although the option ignore_swiftui_view_bodies is set to true. Using the --fix option for this violation will remove the let, and previews won't compile. The option is working properly inside SwiftUI var body: some View declarations.

// This triggers a violation:
@ViewBuilder
func viewBuilder() -> some View {
    let _ = "Test Builder"
    EmptyView()
}

Removing the let will break the View conformance. Adding explicit return to EmptyView will break the result builder ViewBuilder.

$ swiftlint --strict --progress

TestFile.swift:60:9: error: Redundant Discardable Let Violation: Prefer `_ = foo()` over `let _ = foo()` when discarding a result from a function (redundant_discardable_let)

Environment

  • SwiftLint version: 0.59.1
  • Xcode version: Xcode 16.2, Build version 16C5032a
  • Installation via Homebrew
  • Configuration file:
# Default rule configuration
redundant_discardable_let:
    ignore_swiftui_view_bodies: true

No nested configurations.

Imho the exception for var body: some View should also apply to @ViewBuilder implementations since they follow the same rules as the var body: some View declarations.

ObjectiveCesar avatar Apr 24 '25 10:04 ObjectiveCesar

This false positive also effects the #Preview macro and PreviewProvider implementations.

ObjectiveCesar avatar Apr 25 '25 18:04 ObjectiveCesar

This issue also occurs in our project, so I’ve opened a pull request that could address it:

https://github.com/realm/SwiftLint/pull/6075

kaseken avatar May 11 '25 08:05 kaseken