Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.

Metadata

ID: swift-code-style/guard-let-shorthand

Language: Unknown

Severity: Warning

Category: Code Style

Description

This rule identifies and flags redundant identifiers in optional binding within guard statements, such as guard let name = name else { ... }. In Swift, when the identifier on the left side of the binding is the same as the optional being unwrapped, you can omit the explicit assignment and write guard let name else { ... } instead. This simplification improves code readability by reducing unnecessary repetition.

To comply with this rule, simply omit the explicit assignment when unwrapping an optional with the same identifier, using guard let variable else { ... } rather than guard let variable = variable else { ... }. Always prefer this concise form unless you need to bind to a different variable name or perform type casting.

Notes

Non-Compliant Code Examples

guard let name = name else {
    // ...
}

guard let self = self else {
    // ...
}
    
guard interactionEnabled,
   let userTapLocation = userTapLocation else {
    // ...
}

guard let user = user, 
   var device = device else {
    // ...
}

Compliant Code Examples

guard let name else {
    // ...
}

guard let name = user?.name else {
    // ...
}
    
guard let user = user as? User else {
    // ...
}
 
guard var user = currentUser() else {
    // ...
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Integraciones sin problemas. Prueba Datadog Code Security