Skip to content

Omit explicit returns where possible #2146

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

Closed
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
1 change: 1 addition & 0 deletions .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"AlwaysUseLowerCamelCase": false,
"AmbiguousTrailingClosureOverload": false,
"NoBlockComments": false,
"OmitExplicitReturns": true,
"OrderedImports": true,
"UseLetInEveryBoundCaseVariable": false,
"UseSynthesizedInitializer": false
Expand Down
4 changes: 2 additions & 2 deletions CodeGeneration/Sources/SyntaxSupport/Child.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class Child {

/// A name of this child that's suitable to be used for variable or enum case names.
public var varOrCaseName: TokenSyntax {
return .identifier(lowercaseFirstWord(name: name))
.identifier(lowercaseFirstWord(name: name))
}

/// If this child has node choices, the type that the nested `SyntaxChildChoices` type should get.
Expand Down Expand Up @@ -137,7 +137,7 @@ public class Child {

/// Determines if this child has a deprecated name
public var hasDeprecatedName: Bool {
return deprecatedName != nil
deprecatedName != nil
}

/// If the child ends with "token" in the kind, it's considered a token node.
Expand Down
8 changes: 4 additions & 4 deletions CodeGeneration/Sources/SyntaxSupport/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Node {
/// A name for this node that is suitable to be used as a variables or enum
/// case's name.
public var varOrCaseName: TokenSyntax {
return kind.varOrCaseName
kind.varOrCaseName
}

/// If this is a layout node, return a view of the node that provides access
Expand Down Expand Up @@ -264,7 +264,7 @@ public struct LayoutNode {

/// Allow transparent accesss to the properties of the underlying `Node`.
public subscript<T>(dynamicMember keyPath: KeyPath<Node, T>) -> T {
return node[keyPath: keyPath]
node[keyPath: keyPath]
}

/// The children of the layout node.
Expand All @@ -281,7 +281,7 @@ public struct LayoutNode {

/// All children in the node that are not unexpected.
public var nonUnexpectedChildren: [Child] {
return children.filter { !$0.isUnexpectedNodes }
children.filter { !$0.isUnexpectedNodes }
}

/// Traits that the node conforms to.
Expand Down Expand Up @@ -328,7 +328,7 @@ public struct CollectionNode {

/// Allow transparent access to the properties of the underlying `Node`.
public subscript<T>(dynamicMember keyPath: KeyPath<Node, T>) -> T {
return node[keyPath: keyPath]
node[keyPath: keyPath]
}

/// The kinds the elements can have.
Expand Down
6 changes: 3 additions & 3 deletions CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public enum SyntaxNodeKind: String, CaseIterable {
/// A name for this node that is suitable to be used as a variables or enum
/// case's name.
public var varOrCaseName: TokenSyntax {
return .identifier(rawValue)
.identifier(rawValue)
}

/// The type name of this node in the SwiftSyntax module.
Expand All @@ -351,12 +351,12 @@ public enum SyntaxNodeKind: String, CaseIterable {
/// For base nodes, the name of the corresponding protocol to which all the
/// concrete nodes that have this base kind, conform.
public var protocolType: TypeSyntax {
return "\(syntaxType)Protocol"
"\(syntaxType)Protocol"
}

/// The name of this node at the `RawSyntax` level.
public var rawType: TypeSyntax {
return "Raw\(syntaxType)"
"Raw\(syntaxType)"
}

/// For base nodes, the name of the corresponding raw protocol to which all the
Expand Down
6 changes: 3 additions & 3 deletions CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public struct TokenSpec {
/// - name: A name of the token.
/// - text: An actual text of the punctuation token.
static func punctuator(name: String, text: String) -> TokenSpec {
return TokenSpec(
TokenSpec(
name: name,
nameForDiagnostics: text,
text: text,
Expand All @@ -89,7 +89,7 @@ public struct TokenSpec {
/// - name: A name of the token.
/// - text: An actual text of the pound keyword token.
static func poundKeyword(name: String, text: String) -> TokenSpec {
return TokenSpec(
TokenSpec(
name: name,
nameForDiagnostics: text,
text: text,
Expand All @@ -104,7 +104,7 @@ public struct TokenSpec {
/// - nameForDiagnostics: A name of the token that can be shown in diagnostics.
/// - text: An actual text of the token, if available.
static func other(name: String, nameForDiagnostics: String, text: String? = nil) -> TokenSpec {
return TokenSpec(
TokenSpec(
name: name,
nameForDiagnostics: nameForDiagnostics,
text: text,
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/Utils/CodeGenerationFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class CodeGenerationFormat: BasicFormat {
private func shouldBeSeparatedByTwoNewlines(node: CodeBlockItemSyntax) -> Bool {
// First item in the ``CodeBlockItemListSyntax`` don't need a newline or indentation if the parent is a ``SourceFileSyntax``.
// We want to group imports so newline between them should be omitted
return node.parent?.as(CodeBlockItemListSyntax.self)?.first == node || node.item.is(ImportDeclSyntax.self)
node.parent?.as(CodeBlockItemListSyntax.self)?.first == node || node.item.is(ImportDeclSyntax.self)
}

private func ensuringTwoLeadingNewlines<NodeType: SyntaxProtocol>(node: NodeType) -> NodeType {
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public extension Child {
}

var parameterType: TypeSyntax {
return self.buildableType.optionalWrapped(type: parameterBaseType)
self.buildableType.optionalWrapped(type: parameterBaseType)
}

var defaultValue: ExprSyntax? {
Expand Down
4 changes: 2 additions & 2 deletions CodeGeneration/Sources/Utils/SyntaxBuildableType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public struct SyntaxBuildableType: Hashable {
/// which will eventually get built from `SwiftSyntaxBuilder`. If the type
/// is optional, this terminates with a `?`.
public var syntax: TypeSyntax {
return optionalWrapped(type: syntaxBaseName)
optionalWrapped(type: syntaxBaseName)
}

/// The type that is used for parameters in SwiftSyntaxBuilder that take this
Expand All @@ -147,7 +147,7 @@ public struct SyntaxBuildableType: Hashable {
}

public var parameterType: TypeSyntax {
return optionalWrapped(type: parameterBaseType)
optionalWrapped(type: parameterBaseType)
}

/// Assuming that this is a collection type, the non-optional type of the result builder
Expand Down
4 changes: 2 additions & 2 deletions CodeGeneration/Sources/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ extension String {

/// Removes all empty lines from a multi-line string.
public var removingEmptyLines: String {
return
self

self
.split(whereSeparator: \.isNewline)
.filter { !$0.allSatisfy(\.isWhitespace) }
.joined(separator: "\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct GeneratedFileSpec {
let pathComponents: [String]
private let contentsGenerator: () -> String
var contents: String {
return self.contentsGenerator()
self.contentsGenerator()
}

init(_ pathComponents: [String], _ contents: @escaping @autoclosure () -> String) {
Expand All @@ -55,7 +55,7 @@ struct GeneratedFileSpec {
struct TemplateSpec {
let sourceFileGenerator: () -> SourceFileSyntax
var sourceFile: SourceFileSyntax {
return self.sourceFileGenerator()
self.sourceFileGenerator()
}
let module: String
let filename: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ import SyntaxSupport

extension Array where Element == Child {
var hasDeprecatedChild: Bool {
return self.contains(where: { $0.hasDeprecatedName })
self.contains(where: { $0.hasDeprecatedName })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fileprivate extension Node {
}

func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax {
return SourceFileSyntax {
SourceFileSyntax {
for node in SYNTAX_NODES where node.kind.isBase && nodesStartingWith.contains(node.kind.syntaxType.description.first!) {
DeclSyntax(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ let triviaPiecesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
}

fileprivate func generateIsHelpers(for pieceName: TokenSyntax) throws -> ExtensionDeclSyntax {
return try ExtensionDeclSyntax("extension \(pieceName)") {
try ExtensionDeclSyntax("extension \(pieceName)") {
DeclSyntax(
"""
/// Returns `true` if this piece is a newline, space or tab.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fileprivate extension ChildKind {

fileprivate extension Child {
func hasSameType(as other: Child) -> Bool {
return varOrCaseName.description == other.varOrCaseName.description && kind.hasSameType(as: other.kind) && isOptional == other.isOptional
varOrCaseName.description == other.varOrCaseName.description && kind.hasSameType(as: other.kind) && isOptional == other.isOptional
}

func isFollowedByColonToken(in node: LayoutNode) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct Context: Hashable {
var rawValue: UInt32

var kind: Context.Kind? {
return Context.Kind(rawValue: UInt8(self.rawValue & 0x1F))
Context.Kind(rawValue: UInt8(self.rawValue & 0x1F))
}
}

Expand Down Expand Up @@ -156,15 +156,15 @@ struct ModuleContext {

extension ModuleContext {
var isSwiftRefactorModule: Bool {
return self.name == "SwiftRefactor"
self.name == "SwiftRefactor"
}
}

struct RelativeDirectPointer<Pointee> {
var offset: Int32

func address(from pointer: UnsafeRawPointer) -> UnsafeRawPointer {
return pointer + UnsafeRawPointer.Stride(self.offset)
pointer + UnsafeRawPointer.Stride(self.offset)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class SourceEditorExtension: NSObject, XCSourceEditorExtension {
}

var commandDefinitions: [[XCSourceEditorCommandDefinitionKey: Any]] {
return RefactoringRegistry.shared.providers.map { provider in
RefactoringRegistry.shared.providers.map { provider in
return [
.classNameKey: SourceEditorCommand.className(),
.identifierKey: String(describing: provider),
Expand Down
2 changes: 1 addition & 1 deletion Examples/Sources/ExamplePlugin/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct MemberDeprecatedMacro: MemberAttributeMacro {
providingAttributesFor member: some SwiftSyntax.DeclSyntaxProtocol,
in context: some SwiftSyntaxMacros.MacroExpansionContext
) throws -> [SwiftSyntax.AttributeSyntax] {
return ["@available(*, deprecated)"]
["@available(*, deprecated)"]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public struct CodableKey: PeerMacro {
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
// Does nothing, used only to decorate members with data
return []
[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private let defaultOptionsEnumName = "Options"
extension LabeledExprListSyntax {
/// Retrieve the first element with the given label.
func first(labeled name: String) -> Element? {
return first { element in
first { element in
if let label = element.label, label.text == name {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extension DeclGroupSyntax {
/// Enumerate the stored properties that syntactically occur in this
/// declaration.
func storedProperties() -> [VariableDeclSyntax] {
return memberBlock.members.compactMap { member in
memberBlock.members.compactMap { member in
guard let variable = member.decl.as(VariableDeclSyntax.self),
variable.isStoredProperty
else {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Sources/MacroExamples/Playground/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ print("password: description=\(password) hashValue=\(password.hashValue)")
struct MyStruct {
@AddCompletionHandler
func f(a: Int, for b: String, _ value: Double) async -> String {
return b
b
}

@AddAsync
Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftBasicFormat/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ open class BasicFormat: SyntaxRewriter {
/// The trivia by which tokens should currently be indented.
public var currentIndentationLevel: Trivia {
// `decreaseIndentationLevel` guarantees that there is always one item on the stack.
return indentationStack.last!.indentation
indentationStack.last!.indentation
}

/// For every token that is being put on a new line but did not have
Expand All @@ -53,7 +53,7 @@ open class BasicFormat: SyntaxRewriter {

/// Whether we are currently visiting the subtree of a `StringLiteralExprSyntax`.
private var isInsideStringLiteral: Bool {
return stringLiteralNestingLevel > 0
stringLiteralNestingLevel > 0
}

public init(
Expand Down Expand Up @@ -90,7 +90,7 @@ open class BasicFormat: SyntaxRewriter {
open override func visit(_ node: UnexpectedNodesSyntax) -> UnexpectedNodesSyntax {
// Do not perform any formatting on unexpected nodes, the result won't make any
// sense as we rely on layout nodes to know what formatting to perform.
return node
node
}

open override func visitPre(_ node: Syntax) {
Expand Down Expand Up @@ -123,7 +123,7 @@ open class BasicFormat: SyntaxRewriter {
// MARK: - Helper functions

private func isInsideStringInterpolation(_ token: TokenSyntax) -> Bool {
return token.ancestorOrSelf { $0.as(ExpressionSegmentSyntax.self) } != nil
token.ancestorOrSelf { $0.as(ExpressionSegmentSyntax.self) } != nil
}

private func childrenSeparatedByNewline(_ node: Syntax) -> Bool {
Expand Down Expand Up @@ -383,7 +383,7 @@ open class BasicFormat: SyntaxRewriter {
/// will be mutated. Thus, if two tokens need to be separated by a space, it
/// will not be assumed that the space is added to an immutable previous node.
open func isMutable(_ token: TokenSyntax) -> Bool {
return true
true
}

/// Change the text of a token during formatting.
Expand All @@ -393,7 +393,7 @@ open class BasicFormat: SyntaxRewriter {
/// - Parameter token: The token whose text should be changed
/// - Returns: The new text or `nil` if the text should not be changed
open func transformTokenText(_ token: TokenSyntax) -> String? {
return nil
nil
}

/// Change the presence of a token during formatting.
Expand All @@ -403,7 +403,7 @@ open class BasicFormat: SyntaxRewriter {
/// - Parameter token: The token whose presence should be changed
/// - Returns: The new presence or `nil` if the presence should not be changed
open func transformTokenPresence(_ token: TokenSyntax) -> SourcePresence? {
return nil
nil
}

// MARK: - Formatting a token
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftBasicFormat/SyntaxProtocol+Formatted.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import SwiftSyntax
public extension SyntaxProtocol {
/// Build a syntax node from this `Buildable` and format it with the given format.
func formatted(using format: BasicFormat = BasicFormat()) -> Syntax {
return format.rewrite(self)
format.rewrite(self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ struct UnimplementedError: Error, CustomStringConvertible {
public extension PluginProvider {
var features: [PluginFeature] {
// No optional features by default.
return []
[]
}

func loadPluginLibrary(libraryPath: String, moduleName: String) throws {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDiagnostics/Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension FixIt {
oldNode: some SyntaxProtocol,
newNode: some SyntaxProtocol
) -> Self {
return FixIt(
FixIt(
message: message,
changes: [
.replace(oldNode: Syntax(oldNode), newNode: Syntax(newNode))
Expand Down
Loading