Merge pull request #2433 from DPrakashhh/fix/no-computed-property-for-void-2430

Fix #2430: Hide 'Convert to computed property' for Void functions
This commit is contained in:
Alex Hoppen
2026-01-08 16:59:14 +01:00
committed by GitHub
2 changed files with 57 additions and 3 deletions

View File

@@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
@@ -117,10 +117,14 @@ extension ConvertZeroParameterFunctionToComputedProperty: SyntaxRefactoringCodeA
package static var title: String { "Convert to computed property" }
static func nodeToRefactor(in scope: SyntaxCodeActionScope) -> Input? {
return scope.innermostNodeContainingRange?.findParentOfSelf(
let functionDecl = scope.innermostNodeContainingRange?.findParentOfSelf(
ofType: FunctionDeclSyntax.self,
stoppingIf: { $0.is(CodeBlockSyntax.self) || $0.is(MemberBlockSyntax.self) }
)
guard let functionDecl, !(functionDecl.signature.returnClause?.type.isVoid ?? true) else {
return nil
}
return functionDecl
}
}
@@ -178,3 +182,18 @@ extension [SourceEdit] {
)
}
}
// MARK: - Helper Extensions
private extension TypeSyntax {
var isVoid: Bool {
switch self.as(TypeSyntaxEnum.self) {
case .identifierType(let identifierType) where identifierType.name.text == "Void":
return true
case .tupleType(let tupleType) where tupleType.elements.isEmpty:
return true
default:
return false
}
}
}

View File

@@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
@@ -1506,6 +1506,41 @@ final class CodeActionTests: SourceKitLSPTestCase {
}
}
func testConvertZeroParameterFunctionToComputedPropertyNotOfferedForImplicitVoid() async throws {
try await assertCodeActions(
"""
1⃣func test()2⃣ { }3
""",
ranges: [("1", "2")],
exhaustive: false
) { _, _ in
[]
}
}
func testConvertZeroParameterFunctionToComputedPropertyNotOfferedForExplicitVoid() async throws {
try await assertCodeActions(
"""
1⃣func test() -> Void2⃣ { }3
""",
ranges: [("1", "2")],
exhaustive: false
) { _, _ in
[]
}
}
func testConvertZeroParameterFunctionToComputedPropertyNotOfferedForEmptyTuple() async throws {
try await assertCodeActions(
"""
1⃣func test() -> ()2⃣ { }3
""",
ranges: [("1", "2")],
exhaustive: false
) { _, _ in
[]
}
}
func testConvertComputedPropertyToZeroParameterFunction() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: clientCapabilitiesWithCodeActionSupport)
let uri = DocumentURI(for: .swift)