1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftSyntax
/// Every variable bound in a `case` pattern must have its own `let/var`.
///
/// For example, `case let .identifier(x, y)` is forbidden. Use
/// `case .identifier(let x, let y)` instead.
///
/// Lint: `case let .identifier(...)` will yield a lint error.
@_spi(Rules)
public final class UseLetInEveryBoundCaseVariable: SyntaxLintRule {
public override func visit(_ node: ValueBindingPatternSyntax) -> SyntaxVisitorContinueKind {
// Diagnose a pattern binding if it is a function call and the callee is a member access
// expression (e.g., `case let .x(y)` or `case let T.x(y)`).
if canDistributeLetVarThroughPattern(node.pattern) {
diagnose(.useLetInBoundCaseVariables, on: node)
}
return .visitChildren
}
/// Returns true if the given pattern is one that allows a `let/var` to be distributed
/// through to subpatterns.
private func canDistributeLetVarThroughPattern(_ pattern: PatternSyntax) -> Bool {
guard let exprPattern = pattern.as(ExpressionPatternSyntax.self) else { return false }
// Drill down into any optional patterns that we encounter (e.g., `case let .foo(x)?`).
var expression = exprPattern.expression
while true {
if let optionalExpr = expression.as(OptionalChainingExprSyntax.self) {
expression = optionalExpr.expression
} else if let forcedExpr = expression.as(ForceUnwrapExprSyntax.self) {
expression = forcedExpr.expression
} else {
break
}
}
// Enum cases are written as function calls on member access expressions. The arguments
// are the associated values, so the `let/var` can be distributed into those.
if let functionCall = expression.as(FunctionCallExprSyntax.self),
functionCall.calledExpression.is(MemberAccessExprSyntax.self)
{
return true
}
// A tuple expression can have the `let/var` distributed into the elements.
if expression.is(TupleExprSyntax.self) {
return true
}
// Otherwise, we're not sure this is a pattern we can distribute through.
return false
}
}
extension Finding.Message {
fileprivate static let useLetInBoundCaseVariables: Finding.Message =
"move this 'let' keyword inside the 'case' pattern, before each of the bound variables"
}
|