Skip to content

Fix false positives in redundant_discardable_let rule #6075

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

### Bug Fixes

* None.
* Fix false positives in `redundant_discardable_let` when `ignore_swiftui_view_bodies` is enabled.
[kaseken](https://github.com/kaseken)

## 0.59.1: Crisp Spring Clean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ struct RedundantDiscardableLetRule: Rule {
return Text("Hello, World!")
}
""", configuration: ["ignore_swiftui_view_bodies": true]),
Example("""
@ViewBuilder
func bar() -> some View {
let _ = foo()
Text("Hello, World!")
}
""", configuration: ["ignore_swiftui_view_bodies": true]),
Example("""
#Preview {
let _ = foo()
Text("Hello, World!")
}
""", configuration: ["ignore_swiftui_view_bodies": true]),
Example("""
static var previews: some View {
let _ = foo()
Text("Hello, World!")
}
""", configuration: ["ignore_swiftui_view_bodies": true]),
],
triggeringExamples: [
Example("↓let _ = foo()"),
Expand All @@ -32,6 +51,25 @@ struct RedundantDiscardableLetRule: Rule {
Text("Hello, World!")
}
"""),
Example("""
@ViewBuilder
func bar() -> some View {
let _ = foo()
return Text("Hello, World!")
}
"""),
Example("""
#Preview {
let _ = foo()
return Text("Hello, World!")
}
"""),
Example("""
static var previews: some View {
let _ = foo()
Text("Hello, World!")
}
"""),
],
corrections: [
Example("↓let _ = foo()"): Example("_ = foo()"),
Expand All @@ -50,23 +88,32 @@ private extension RedundantDiscardableLetRule {
private var codeBlockScopes = Stack<CodeBlockKind>()

override func visit(_ node: AccessorBlockSyntax) -> SyntaxVisitorContinueKind {
codeBlockScopes.push(node.isViewBody ? .view : .normal)
codeBlockScopes.push(node.isViewBody || node.isPreviewProviderBody ? .view : .normal)
return .visitChildren
}

override func visitPost(_: AccessorBlockSyntax) {
codeBlockScopes.pop()
}

override func visit(_: CodeBlockSyntax) -> SyntaxVisitorContinueKind {
codeBlockScopes.push(.normal)
override func visit(_ node: CodeBlockSyntax) -> SyntaxVisitorContinueKind {
codeBlockScopes.push(node.isViewBuilderFunctionBody ? .view : .normal)
return .visitChildren
}

override func visitPost(_: CodeBlockSyntax) {
codeBlockScopes.pop()
}

override func visit(_ node: ClosureExprSyntax) -> SyntaxVisitorContinueKind {
codeBlockScopes.push(node.isPreviewMacroBody ? .view : .normal)
return .visitChildren
}

override func visitPost(_: ClosureExprSyntax) {
codeBlockScopes.pop()
}

override func visitPost(_ node: VariableDeclSyntax) {
if codeBlockScopes.peek() != .view || !configuration.ignoreSwiftUIViewBodies,
node.bindingSpecifier.tokenKind == .keyword(.let),
Expand Down Expand Up @@ -100,4 +147,56 @@ private extension AccessorBlockSyntax {
}
return false
}

var isPreviewProviderBody: Bool {
guard let binding = parent?.as(PatternBindingSyntax.self),
binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text == "previews",
let bindingList = binding.parent?.as(PatternBindingListSyntax.self),
let variableDecl = bindingList.parent?.as(VariableDeclSyntax.self) else {
return false
}

guard variableDecl.modifiers.contains(keyword: .static) &&
variableDecl.bindingSpecifier.tokenKind == .keyword(.var) else {
return false
}

if let type = binding.typeAnnotation?.type.as(SomeOrAnyTypeSyntax.self) {
return type.someOrAnySpecifier.text == "some" &&
type.constraint.as(IdentifierTypeSyntax.self)?.name.text == "View"
}

return false
}
}

private extension CodeBlockSyntax {
var isViewBuilderFunctionBody: Bool {
parent?.as(FunctionDeclSyntax.self)?.isViewBuilderFunction == true
}
}

private extension FunctionDeclSyntax {
var isViewBuilderFunction: Bool {
guard attributes.contains(attributeNamed: "ViewBuilder") else {
return false
}

guard let returnType = signature.returnClause?.type.as(SomeOrAnyTypeSyntax.self) else {
return false
}

return returnType.someOrAnySpecifier.text == "some" &&
returnType.constraint.as(IdentifierTypeSyntax.self)?.name.text == "View"
}
}

private extension ClosureExprSyntax {
var isPreviewMacroBody: Bool {
if let macroExpansionExpr = parent?.as(MacroExpansionExprSyntax.self),
macroExpansionExpr.macroName.text == "Preview" {
return true
}
return false
}
}