---
title: User must specify return type via ->.
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > User must specify return type via ->.
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# User must specify return type via ->.

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `swift-code-style/specify-return-type`

**Language:** Swift

**Severity:** Error

**Category:** Code Style

## Description{% #description %}

In Swift, specifying a function's return type is crucial for readability, maintainability, and to avoid potential runtime errors. This rule enforces that the return type of a function should be explicitly stated using the '->' symbol when the function returns a value.

Not specifying a return type for a value-returning function can lead to confusion for other developers who might use your function, as they won't be able to predict what kind of value they should expect. Additionally, it can cause errors if the function returns a different type than expected. Functions that return nothing (Void) may omit the return type; for example, `func viewDidAppear(_ animated: Bool) { refresh() }` is compliant.

To adhere to this rule, specify the return type when your function returns a value: e.g. `func addTwoNums(num1: Int, num2: Int) -> Int`. For functions that return nothing, you may omit `-> Void`; adding it explicitly (e.g. `func doSomething() -> Void { }`) is also compliant.

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```swift
func addTwoNums(num1: Int, num2: Int) {
    return num1 + num2;
}
```

## Compliant Code Examples{% #compliant-code-examples %}

```swift
func addTwoNums(num1: Int, num2: Int) -> Int {
    return num1 + num2;
}
```

```swift
public func getValue(for name: String) -> String? {
    return value
}
```

```swift
func doSomethig() {
    something()
}
```

```swift
func checkCanEncode(value: Any?) throws {
    guard canEncodeNewValue else {
        throw EncodingError.invalidValue(value as Any, context)
    }
}
```

```swift
func nestedContainer() -> any SingleContainer {
    return new SingleValueContainer()
}
```

```swift
public func evaluate(_ trust: SecTrust, forHost host: String) throws {
    let pinnedKeysInServerKeys: Bool = {
        for serverPublicKey in trust.af.publicKeys {
            if keys.contains(serverPublicKey) {
                return true
            }
        }
        return false
    }()
    if !pinnedKeysInServerKeys {
        throw AFError.serverFailed()
    }
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 