Skip to content

False positive on redundant_discardable_let rule in SwiftUI @ViewBuilder #6063

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

Open
ObjectiveCesar opened this issue Apr 24, 2025 · 2 comments · May be fixed by #6075
Open

False positive on redundant_discardable_let rule in SwiftUI @ViewBuilder #6063

ObjectiveCesar opened this issue Apr 24, 2025 · 2 comments · May be fixed by #6075

Comments

@ObjectiveCesar
Copy link

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 ObjectiveCesar changed the title False positive on redundant_discardable_let in SwiftUI @ViewBuilder False positive on redundant_discardable_let rule in SwiftUI @ViewBuilder Apr 25, 2025
@ObjectiveCesar ObjectiveCesar changed the title False positive on redundant_discardable_let rule in SwiftUI @ViewBuilder False positive on redundant_discardable_let rule in SwiftUI @ViewBuilder Apr 25, 2025
@ObjectiveCesar
Copy link
Author

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

@kaseken
Copy link
Contributor

kaseken commented May 11, 2025

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

#6075

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 a pull request may close this issue.

2 participants