Skip to content

redundant_discardable_let rule causes a compilation error for things that are wrapped in a result-builder annotation #3855

@jeremypearson

Description

@jeremypearson

New Issue Checklist

Describe the bug

redundant_discardable_let rule causes a compilation error for things that are wrapped in a result-builder annotation.

Here's a link to the rule on GitHub which contains the bug: https://github.com/realm/SwiftLint/blob/master/Source/SwiftLintFramework/Rules/Style/RedundantDiscardableLetRule.swift

It's unsafe, for say, the body property of a struct which conforms to the View protocol from SwiftUI (and which therefore has a body property that's implicitly-annotated with the @ViewBuilder result-builder annotation), to replace this code snippet:

// Inside struct conforming to `View` protocol from `SwiftUI`.
var body: some View {
    let _ = 10

    Text("Hello, world!")
        .padding()
}

with this code snippet:

// Inside struct conforming to `View` protocol from `SwiftUI`.
var body: some View {
    _ = 10

    Text("Hello, world!")
        .padding()
}

as making such a replacement, would result in a compilation error. In this specific example, that compilation error will be: "Type '()' cannot conform to 'View'".

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint /Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift
Linting Swift files at paths /Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift
Linting 'ContentView.swift' (1/1)
/Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift:13:9: warning: Redundant Discardable Let Violation: Prefer `_ = foo()` over `let _ = foo()` when discarding a result from a function. (redundant_discardable_let)
Done linting! Found 1 violation, 0 serious in 1 file.

Environments

  • SwiftLint version: 0.46.2

  • Installation method used: Homebrew

  • Paste your configuration file:
    N/A - default settings were used.

  • Nested configurations are not being used.

  • Xcode version: Xcode 13.2 Build version 13C5066c

  • Running the command: swiftlint lint --path /Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift --no-cache --enable-all-rules produced the following output:

/Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift:12:5: warning: Explicit ACL Violation: All declarations should specify Access Control Level keywords explicitly. (explicit_acl)
/Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift:11:1: warning: Explicit ACL Violation: All declarations should specify Access Control Level keywords explicitly. (explicit_acl)
/Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift:21:5: warning: Explicit ACL Violation: All declarations should specify Access Control Level keywords explicitly. (explicit_acl)
/Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift:20:1: warning: Explicit ACL Violation: All declarations should specify Access Control Level keywords explicitly. (explicit_acl)
/Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift:11:1: warning: Explicit Top Level ACL Violation: Top-level declarations should specify Access Control Level keywords explicitly. (explicit_top_level_acl)
/Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift:20:1: warning: Explicit Top Level ACL Violation: Top-level declarations should specify Access Control Level keywords explicitly. (explicit_top_level_acl)
/Users/jeremy/dev/xcode/SwiftLintBugDemoApp/SwiftLintBugDemoApp/ContentView.swift:13:9: warning: Redundant Discardable Let Violation: Prefer `_ = foo()` over `let _ = foo()` when discarding a result from a function. (redundant_discardable_let)
Done linting! Found 7 violations, 0 serious in 1 file.
import SwiftUI

struct ContentView: View {
    var body: some View {
        let _ = 10 // This line triggers a violation.

        Text("Hello, world!")
            .padding()
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptable-false-positiveFalse positives caused by rules that are unavoidable due to missing type information.bugUnexpected and reproducible misbehavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions