Skip to content

Add redundantEmptyView rule#2470

Merged
calda merged 4 commits intonicklockwood:developfrom
mannylopez:manny-lopez--redundant-emptyview
Mar 20, 2026
Merged

Add redundantEmptyView rule#2470
calda merged 4 commits intonicklockwood:developfrom
mannylopez:manny-lopez--redundant-emptyview

Conversation

@mannylopez
Copy link
Contributor

@mannylopez mannylopez commented Mar 19, 2026

Adds a new redundantEmptyView formatting rule that removes redundant else { EmptyView() } branches in SwiftUI view builders.

In SwiftUI view builders, an if statement without an else branch implicitly produces no content when the condition is false, making an explicit else { EmptyView() } unnecessary.

var body: some View {
    if condition {
        Text("Hello")
-     } else {
-         EmptyView()
    }
}

@mannylopez mannylopez changed the title Manny lopez redundant emptyview Add redundantEmptyView rule Mar 19, 2026
@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 98.36066% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.30%. Comparing base (c7a265b) to head (b3169c5).
⚠️ Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
Sources/Rules/RedundantEmptyView.swift 98.36% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2470      +/-   ##
===========================================
+ Coverage    95.28%   95.30%   +0.01%     
===========================================
  Files          165      166       +1     
  Lines        25243    25304      +61     
===========================================
+ Hits         24053    24115      +62     
+ Misses        1190     1189       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@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.

Yes! Love it! Thank you @mannylopez

func expressionIsEmptyView(in expressionRange: ClosedRange<Int>) -> Bool {
var emptyViewIdentifierIndex = expressionRange.lowerBound

// Handle fully-qualified `SwiftUI.EmptyView()`
Copy link
Collaborator

Choose a reason for hiding this comment

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

We're missing a test case for this code path

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added testRemoveFullyQualifiedSwiftUIEmptyView

@calda
Copy link
Collaborator

calda commented Mar 19, 2026

I ran this on the Airbnb codebase and found one example where it causes a build failure. However the error seems like a compiler bug to me, not a "real" issue (the example uses a trailing closure, and converting the trailing closure to a non-trailing closure avoids the error, which definitely isn't supposed to happen).

I think it's ok to leave this enabled by default initially, but I will change it to be disabled by default if we get several bug reports like this.

@calda calda merged commit 869e16c into nicklockwood:develop Mar 20, 2026
14 checks passed
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.

2 participants