|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | + |
| 15 | +/// Single-expression functions, closures, subscripts can omit `return` statement. |
| 16 | +/// |
| 17 | +/// Lint: `func <name>() { return ... }` and similar single expression constructs will yield a lint error. |
| 18 | +/// |
| 19 | +/// Format: `func <name>() { return ... }` constructs will be replaced with |
| 20 | +/// equivalent `func <name>() { ... }` constructs. |
| 21 | +@_spi(Rules) |
| 22 | +public final class OmitExplicitReturns: SyntaxFormatRule { |
| 23 | + public override class var isOptIn: Bool { return true } |
| 24 | + |
| 25 | + public override func visit(_ node: FunctionDeclSyntax) -> DeclSyntax { |
| 26 | + let decl = super.visit(node) |
| 27 | + |
| 28 | + // func <name>() -> <Type> { return ... } |
| 29 | + guard var funcDecl = decl.as(FunctionDeclSyntax.self), |
| 30 | + let body = funcDecl.body, |
| 31 | + let returnStmt = containsSingleReturn(body.statements) else { |
| 32 | + return decl |
| 33 | + } |
| 34 | + |
| 35 | + funcDecl.body?.statements = rewrapReturnedExpression(returnStmt) |
| 36 | + diagnose(.omitReturnStatement, on: returnStmt, severity: .refactoring) |
| 37 | + return DeclSyntax(funcDecl) |
| 38 | + } |
| 39 | + |
| 40 | + public override func visit(_ node: SubscriptDeclSyntax) -> DeclSyntax { |
| 41 | + let decl = super.visit(node) |
| 42 | + |
| 43 | + guard var subscriptDecl = decl.as(SubscriptDeclSyntax.self), |
| 44 | + let accessorBlock = subscriptDecl.accessorBlock, |
| 45 | + // We are assuming valid Swift code here where only |
| 46 | + // one `get { ... }` is allowed. |
| 47 | + let transformed = transformAccessorBlock(accessorBlock) else { |
| 48 | + return decl |
| 49 | + } |
| 50 | + |
| 51 | + subscriptDecl.accessorBlock = transformed |
| 52 | + return DeclSyntax(subscriptDecl) |
| 53 | + } |
| 54 | + |
| 55 | + public override func visit(_ node: PatternBindingSyntax) -> PatternBindingSyntax { |
| 56 | + var binding = node |
| 57 | + |
| 58 | + guard let accessorBlock = binding.accessorBlock, |
| 59 | + let transformed = transformAccessorBlock(accessorBlock) else { |
| 60 | + return node |
| 61 | + } |
| 62 | + |
| 63 | + binding.accessorBlock = transformed |
| 64 | + return binding |
| 65 | + } |
| 66 | + |
| 67 | + public override func visit(_ node: ClosureExprSyntax) -> ExprSyntax { |
| 68 | + let expr = super.visit(node) |
| 69 | + |
| 70 | + // test { return ... } |
| 71 | + guard var closureExpr = expr.as(ClosureExprSyntax.self), |
| 72 | + let returnStmt = containsSingleReturn(closureExpr.statements) else { |
| 73 | + return expr |
| 74 | + } |
| 75 | + |
| 76 | + closureExpr.statements = rewrapReturnedExpression(returnStmt) |
| 77 | + diagnose(.omitReturnStatement, on: returnStmt, severity: .refactoring) |
| 78 | + return ExprSyntax(closureExpr) |
| 79 | + } |
| 80 | + |
| 81 | + private func transformAccessorBlock(_ accessorBlock: AccessorBlockSyntax) -> AccessorBlockSyntax? { |
| 82 | + // We are assuming valid Swift code here where only |
| 83 | + // one `get { ... }` is allowed. |
| 84 | + switch accessorBlock.accessors { |
| 85 | + case .accessors(let accessors): |
| 86 | + guard var getter = accessors.filter({ |
| 87 | + $0.accessorSpecifier.tokenKind == .keyword(.get) |
| 88 | + }).first else { |
| 89 | + return nil |
| 90 | + } |
| 91 | + |
| 92 | + guard let body = getter.body, |
| 93 | + let returnStmt = containsSingleReturn(body.statements) else { |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + guard let getterAt = accessors.firstIndex(where: { |
| 98 | + $0.accessorSpecifier.tokenKind == .keyword(.get) |
| 99 | + }) else { |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + getter.body?.statements = rewrapReturnedExpression(returnStmt) |
| 104 | + |
| 105 | + diagnose(.omitReturnStatement, on: returnStmt, severity: .refactoring) |
| 106 | + |
| 107 | + var newBlock = accessorBlock |
| 108 | + newBlock.accessors = .accessors(accessors.with(\.[getterAt], getter)) |
| 109 | + return newBlock |
| 110 | + |
| 111 | + case .getter(let getter): |
| 112 | + guard let returnStmt = containsSingleReturn(getter) else { |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + diagnose(.omitReturnStatement, on: returnStmt, severity: .refactoring) |
| 117 | + |
| 118 | + var newBlock = accessorBlock |
| 119 | + newBlock.accessors = .getter(rewrapReturnedExpression(returnStmt)) |
| 120 | + return newBlock |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private func containsSingleReturn(_ body: CodeBlockItemListSyntax) -> ReturnStmtSyntax? { |
| 125 | + guard let element = body.firstAndOnly?.as(CodeBlockItemSyntax.self), |
| 126 | + let returnStmt = element.item.as(ReturnStmtSyntax.self) else |
| 127 | + { |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + return !returnStmt.children(viewMode: .all).isEmpty && returnStmt.expression != nil ? returnStmt : nil |
| 132 | + } |
| 133 | + |
| 134 | + private func rewrapReturnedExpression(_ returnStmt: ReturnStmtSyntax) -> CodeBlockItemListSyntax { |
| 135 | + CodeBlockItemListSyntax([ |
| 136 | + CodeBlockItemSyntax( |
| 137 | + leadingTrivia: returnStmt.leadingTrivia, |
| 138 | + item: .expr(returnStmt.expression!), |
| 139 | + semicolon: nil, |
| 140 | + trailingTrivia: returnStmt.trailingTrivia) |
| 141 | + ]) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +extension Finding.Message { |
| 146 | + public static let omitReturnStatement: Finding.Message = |
| 147 | + "'return' can be omitted because body consists of a single expression" |
| 148 | +} |
0 commit comments