Skip to content

Conversation

@miguel-jimenez-0529
Copy link
Contributor

Summary

Add redundantViewBuilder rule to remove redundant @ViewBuilder attributes in SwiftUI code.

@ViewBuilder is implicit on View.body properties and ViewModifier.body(content:) functions, and is unnecessary on single-expression properties or functions.

// WRONG
struct PlanetView: View {
  @ViewBuilder
  var body: some View {
    Text("Hello, World!")
  }

  @ViewBuilder
  var subtitle: some View {
    Text("Subtitle")
  }
}

// RIGHT
struct PlanetView: View {
  var body: some View {
    Text("Hello, World!")
  }

  var subtitle: some View {
    Text("Subtitle")
  }
}

// ALSO RIGHT: @ViewBuilder is necessary for conditionals
struct ConditionalView: View {
  var showDetails: Bool

  var body: some View {
    title
  }

  @ViewBuilder
  var title: some View {
    if showDetails {
      Text("Details")
    } else {
      Text("Summary")
    }
  }
}

Reasoning

  • @ViewBuilder is automatically applied by the compiler to View.body and ViewModifier.body(content:), so adding it explicitly is redundant
  • Single-expression properties and functions don't need @ViewBuilder since there's only one view being returned
  • Removing unnecessary attributes reduces visual noise and keeps code clean

SwiftFormat rule: nicklockwood/SwiftFormat#2297

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Member

@calda calda left a comment

Choose a reason for hiding this comment

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

Perfect, thank you!

@calda calda enabled auto-merge (squash) December 20, 2025 04:32
@calda calda merged commit b9b815f into master Dec 20, 2025
6 checks passed
@calda calda deleted the redundant-viewbuilder branch December 20, 2025 04:32
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.

3 participants