diff --git a/CHANGELOG.md b/CHANGELOG.md index 91f7e08865..6dc5807cb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,11 @@ [nandhinisubbu](https://github.com/nandhinisubbu) [#6304](https://github.com/realm/SwiftLint/issues/6304) +* Update `implicit_return` rule to trigger violation for if/switch statements with + single return expression in all branches. + [nandhinisubbu](https://github.com/nandhinisubbu) + [#6167](https://github.com/realm/SwiftLint/issues/6167) + ### Bug Fixes * Ignore function, initializer and subscript declarations alike when the diff --git a/Source/SwiftLintBuiltInRules/Models/ImportUsage.swift b/Source/SwiftLintBuiltInRules/Models/ImportUsage.swift index 496a44adae..532683aebf 100644 --- a/Source/SwiftLintBuiltInRules/Models/ImportUsage.swift +++ b/Source/SwiftLintBuiltInRules/Models/ImportUsage.swift @@ -11,9 +11,9 @@ enum ImportUsage { var violationRange: NSRange? { switch self { case .unused(_, let range): - return range + range case .missing: - return nil + nil } } @@ -21,9 +21,9 @@ enum ImportUsage { var violationReason: String? { switch self { case .unused: - return nil + nil case .missing(let module): - return "Missing import for referenced module '\(module)'" + "Missing import for referenced module '\(module)'" } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExtensionAccessModifierRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExtensionAccessModifierRule.swift index 4ccb056a31..51918693be 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExtensionAccessModifierRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExtensionAccessModifierRule.swift @@ -179,9 +179,9 @@ private extension ExtensionAccessModifierRule { static func from(tokenKind: TokenKind?) -> Self { switch tokenKind { case nil: - return .implicit + .implicit case let value?: - return .explicit(value) + .explicit(value) } } @@ -238,12 +238,12 @@ private extension MemberBlockSyntax { func expandingIfConfigs() -> [DeclSyntax] { members.flatMap { member in if let ifConfig = member.decl.as(IfConfigDeclSyntax.self) { - return ifConfig.clauses.flatMap { clause in + return ifConfig.clauses.flatMap { clause -> [DeclSyntax] in switch clause.elements { case .decls(let decls): - return decls.map(\.decl) + decls.map(\.decl) default: - return [] + [] } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ImplicitlyUnwrappedOptionalRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ImplicitlyUnwrappedOptionalRule.swift index feb337f970..5794a4462e 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ImplicitlyUnwrappedOptionalRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ImplicitlyUnwrappedOptionalRule.swift @@ -52,11 +52,11 @@ private extension ImplicitlyUnwrappedOptionalRule { override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind { switch configuration.mode { case .all: - return .visitChildren + .visitChildren case .allExceptIBOutlets: - return node.isIBOutlet ? .skipChildren : .visitChildren + node.isIBOutlet ? .skipChildren : .visitChildren case .weakExceptIBOutlets: - return (node.isIBOutlet || node.weakOrUnownedModifier == nil) ? .skipChildren : .visitChildren + (node.isIBOutlet || node.weakOrUnownedModifier == nil) ? .skipChildren : .visitChildren } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/StaticOperatorRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/StaticOperatorRule.swift index 54da156dcd..4c9b40d92b 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/StaticOperatorRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/StaticOperatorRule.swift @@ -98,9 +98,9 @@ private extension FunctionDeclSyntax { var isOperator: Bool { switch name.tokenKind { case .binaryOperator: - return true + true default: - return false + false } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/SyntacticSugarRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/SyntacticSugarRule.swift index e9b96ae9cd..11c15447db 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/SyntacticSugarRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/SyntacticSugarRule.swift @@ -61,20 +61,20 @@ private enum SugaredType: String { var sugaredExample: String { switch self { case .optional: - return "Int?" + "Int?" case .array: - return "[Int]" + "[Int]" case .dictionary: - return "[String: Int]" + "[String: Int]" } } var desugaredExample: String { switch self { case .optional, .array: - return "\(rawValue)" + "\(rawValue)" case .dictionary: - return "\(rawValue)" + "\(rawValue)" } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift index ec84fe2f15..99a77bc2d5 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift @@ -134,9 +134,9 @@ private extension TriviaPiece { var isComment: Bool { switch self { case .lineComment, .blockComment, .docLineComment, .docBlockComment: - return true + true default: - return false + false } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTSpecificMatcherRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTSpecificMatcherRule.swift index 5c87648b29..f3ecdcf95b 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTSpecificMatcherRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/XCTSpecificMatcherRule.swift @@ -112,13 +112,13 @@ private enum TwoArgsXCTAssert: String { private func suggestion(for protectedArgument: String, hasOptional: Bool) -> String? { switch (self, protectedArgument, hasOptional) { - case (.equal, "true", false): return "XCTAssertTrue" - case (.equal, "false", false): return "XCTAssertFalse" - case (.equal, "nil", _): return "XCTAssertNil" - case (.notEqual, "true", false): return "XCTAssertFalse" - case (.notEqual, "false", false): return "XCTAssertTrue" - case (.notEqual, "nil", _): return "XCTAssertNotNil" - default: return nil + case (.equal, "true", false): "XCTAssertTrue" + case (.equal, "false", false): "XCTAssertFalse" + case (.equal, "nil", _): "XCTAssertNil" + case (.notEqual, "true", false): "XCTAssertFalse" + case (.notEqual, "false", false): "XCTAssertTrue" + case (.notEqual, "nil", _): "XCTAssertNotNil" + default: nil } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/AccessibilityLabelForImageRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/AccessibilityLabelForImageRule.swift index 281059ba66..1a3b7f5ce9 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/AccessibilityLabelForImageRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/AccessibilityLabelForImageRule.swift @@ -204,11 +204,11 @@ private extension FunctionCallExprSyntax { func isDirectAccessibilityModifier(_ name: String) -> Bool { switch name { case "accessibilityHidden": - return arguments.first?.expression.as(BooleanLiteralExprSyntax.self)?.literal.tokenKind == .keyword(.true) + arguments.first?.expression.as(BooleanLiteralExprSyntax.self)?.literal.tokenKind == .keyword(.true) case "accessibilityLabel", "accessibilityValue", "accessibilityHint": - return true + true case "accessibility": - return arguments.contains { arg in + arguments.contains { arg in guard let label = arg.label?.text else { return false } if ["label", "value", "hint"].contains(label) { return true } if label == "hidden" { @@ -217,7 +217,7 @@ private extension FunctionCallExprSyntax { return false } default: - return false + false } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/DeploymentTargetRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/DeploymentTargetRule.swift index c392de8a49..1e5da85cb2 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/DeploymentTargetRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/DeploymentTargetRule.swift @@ -25,11 +25,11 @@ private enum AvailabilityType { var displayString: String { switch self { case .condition: - return "condition" + "condition" case .attribute: - return "attribute" + "attribute" case .negativeCondition: - return "negative condition" + "negative condition" } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift index 6541f275f3..ee85e52750 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedImportRule.swift @@ -49,9 +49,9 @@ struct UnusedImportRule: CorrectableRule, AnalyzerRule { let missingImports = importUsages.compactMap { importUsage -> String? in switch importUsage { case .unused: - return nil + nil case .missing(let module): - return module + module } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedSetterValueRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedSetterValueRule.swift index 0c75a4f8c6..f537e4f256 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedSetterValueRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/UnusedSetterValueRule.swift @@ -190,9 +190,9 @@ private extension Either { var modifiers: DeclModifierListSyntax? { switch self { case .left(let left): - return left.modifiers + left.modifiers case .right(let right): - return right.modifiers + right.modifiers } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift b/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift index 465f592127..d1f4641f39 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift @@ -201,9 +201,9 @@ private extension TokenSyntax { var binaryOperator: String? { switch tokenKind { case .binaryOperator(let str): - return str + str default: - return nil + nil } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NestingConfiguration.swift b/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NestingConfiguration.swift index 2600c45ab6..5ce62f6bf3 100644 --- a/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NestingConfiguration.swift +++ b/Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NestingConfiguration.swift @@ -30,8 +30,8 @@ struct NestingConfiguration: RuleConfiguration { func threshold(with config: Severity, for severity: ViolationSeverity) -> Int { switch severity { - case .error: return config.error ?? config.warning - case .warning: return config.warning + case .error: config.error ?? config.warning + case .warning: config.warning } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/FileHeaderRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/FileHeaderRule.swift index a218d12a8a..893fa5b9de 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/FileHeaderRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/FileHeaderRule.swift @@ -215,9 +215,9 @@ private extension TriviaPiece { var isDocComment: Bool { switch self { case .docLineComment, .docBlockComment: - return true + true default: - return false + false } } @@ -225,9 +225,9 @@ private extension TriviaPiece { switch self { case .lineComment(let text), .blockComment(let text), .docLineComment(let text), .docBlockComment(let text): - return text + text default: - return nil + nil } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitGetterRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitGetterRule.swift index 494a0533cf..88d9ba9929 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitGetterRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitGetterRule.swift @@ -20,9 +20,9 @@ private enum ViolationKind { var violationDescription: String { switch self { case .subscript: - return "Computed read-only subscripts should avoid using the get keyword" + "Computed read-only subscripts should avoid using the get keyword" case .property: - return "Computed read-only properties should avoid using the get keyword" + "Computed read-only properties should avoid using the get keyword" } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitReturnRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitReturnRule.swift index 92aacced6d..860c698d3d 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitReturnRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitReturnRule.swift @@ -62,9 +62,105 @@ private extension ImplicitReturnRule { } private func collectViolation(in itemList: CodeBlockItemListSyntax) { - guard let returnStmt = itemList.onlyElement?.item.as(ReturnStmtSyntax.self) else { + guard let onlyItem = itemList.onlyElement else { return } + + // Case 1: Direct return statement + if let returnStmt = onlyItem.item.as(ReturnStmtSyntax.self) { + addViolation(for: returnStmt) + return + } + + // Case 2: Expression statement containing if or switch + if let exprStmt = onlyItem.item.as(ExpressionStmtSyntax.self) { + analyzeExpressionForImplicitReturns(exprStmt.expression) + } + } + + private func analyzeExpressionForImplicitReturns(_ expr: ExprSyntax) { + if let ifExpr = expr.as(IfExprSyntax.self) { + analyzeIfExpression(ifExpr) + } else if let switchExpr = expr.as(SwitchExprSyntax.self) { + analyzeSwitchExpression(switchExpr) + } + } + + private func analyzeIfExpression(_ ifExpr: IfExprSyntax) { + guard checkIfAllBranchesCanUseImplicitReturn(ifExpr) else { return } + + let returnStatements = extractAllReturnStatements(from: ifExpr) + returnStatements.forEach { addViolation(for: $0) } + } + + private func analyzeSwitchExpression(_ switchExpr: SwitchExprSyntax) { + guard checkIfAllCasesCanUseImplicitReturn(switchExpr) else { return } + + let returnStatements = extractAllReturnStatements(from: switchExpr) + returnStatements.forEach { addViolation(for: $0) } + } + + private func extractAllReturnStatements(from ifExpr: IfExprSyntax) -> [ReturnStmtSyntax] { + var statements: [ReturnStmtSyntax] = [] + + // Extract from main if body + if let returnStmt = getSingleReturnStatement(from: ifExpr.body.statements) { + statements.append(returnStmt) + } + + // Extract from else body (recursively handle nested if-else) + if let elseBody = ifExpr.elseBody { + switch elseBody { + case .codeBlock(let codeBlock): + if let returnStmt = getSingleReturnStatement(from: codeBlock.statements) { + statements.append(returnStmt) + } + case .ifExpr(let nestedIfExpr): + statements.append(contentsOf: extractAllReturnStatements(from: nestedIfExpr)) + } + } + + return statements + } + + private func extractAllReturnStatements(from switchExpr: SwitchExprSyntax) -> [ReturnStmtSyntax] { + switchExpr.cases.compactMap { caseItem in + guard let switchCase = caseItem.as(SwitchCaseSyntax.self) else { return nil } + return getSingleReturnStatement(from: switchCase.statements) + } + } + + private func checkIfAllBranchesCanUseImplicitReturn(_ ifExpr: IfExprSyntax) -> Bool { + guard isSingleReturnStatement(ifExpr.body.statements), + let elseBody = ifExpr.elseBody else { + return false + } + + switch elseBody { + case .codeBlock(let codeBlock): + return isSingleReturnStatement(codeBlock.statements) + case .ifExpr(let nestedIfExpr): + return checkIfAllBranchesCanUseImplicitReturn(nestedIfExpr) + } + } + + private func checkIfAllCasesCanUseImplicitReturn(_ switchExpr: SwitchExprSyntax) -> Bool { + !switchExpr.cases.isEmpty && + switchExpr.cases.allSatisfy { caseItem in + guard let switchCase = caseItem.as(SwitchCaseSyntax.self) else { return false } + return isSingleReturnStatement(switchCase.statements) + } + } + + private func isSingleReturnStatement(_ statements: CodeBlockItemListSyntax) -> Bool { + getSingleReturnStatement(from: statements) != nil + } + + private func getSingleReturnStatement(from statements: CodeBlockItemListSyntax) -> ReturnStmtSyntax? { + statements.onlyElement?.item.as(ReturnStmtSyntax.self) + } + + private func addViolation(for returnStmt: ReturnStmtSyntax) { let returnKeyword = returnStmt.returnKeyword violations.append( at: returnKeyword.positionAfterSkippingLeadingTrivia, diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitReturnRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitReturnRuleExamples.swift index df92fb92da..6756b3fdc9 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitReturnRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ImplicitReturnRuleExamples.swift @@ -1,3 +1,5 @@ +// swiftlint:disable file_length +// swiftlint:disable:next type_body_length struct ImplicitReturnRuleExamples { struct ClosureExamples { static let nonTriggeringExamples = [ @@ -103,6 +105,24 @@ struct ImplicitReturnRuleExamples { func g() -> Int { 4 } } """), + Example(""" + func f(condition: Bool) -> Int { + if condition { + let x = 1 + return x + } else { + return 2 + } + } + """), + Example(""" + func f(condition: Bool) -> Int? { + if condition { + return 1 + } + return nil + } + """), ] static let triggeringExamples = [ @@ -119,6 +139,27 @@ struct ImplicitReturnRuleExamples { Example(""" func f() { ↓return } """), + Example(""" + func f(condition: Bool) -> Int { + if condition { + ↓return 1 + } else { + ↓return 2 + } + } + """), + Example(""" + func f(value: Int) -> String { + switch value { + case 0: + ↓return "zero" + case 1: + ↓return "one" + default: + ↓return "other" + } + } + """), ] static let corrections = [ diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/IndentationWidthRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/IndentationWidthRule.swift index 2c8f304de7..26232274a5 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/IndentationWidthRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/IndentationWidthRule.swift @@ -10,8 +10,8 @@ struct IndentationWidthRule: OptInRule { func spacesEquivalent(indentationWidth: Int) -> Int { switch self { - case let .tabs(tabs): return tabs * indentationWidth - case let .spaces(spaces): return spaces + case let .tabs(tabs): tabs * indentationWidth + case let .spaces(spaces): spaces } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/NumberSeparatorRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/NumberSeparatorRule.swift index bbeaa6007d..e4015ce178 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/NumberSeparatorRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/NumberSeparatorRule.swift @@ -80,22 +80,22 @@ private enum NumberSeparatorViolation { var reason: String { switch self { - case .missingSeparator: return NumberSeparatorRule.missingSeparatorsReason - case .misplacedSeparator: return NumberSeparatorRule.misplacedSeparatorsReason + case .missingSeparator: NumberSeparatorRule.missingSeparatorsReason + case .misplacedSeparator: NumberSeparatorRule.misplacedSeparatorsReason } } var position: AbsolutePosition { switch self { - case let .missingSeparator(position, _): return position - case let .misplacedSeparator(position, _): return position + case let .missingSeparator(position, _): position + case let .misplacedSeparator(position, _): position } } var correction: String { switch self { - case let .missingSeparator(_, correction): return correction - case let .misplacedSeparator(_, correction): return correction + case let .missingSeparator(_, correction): correction + case let .misplacedSeparator(_, correction): correction } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/OperatorUsageWhitespaceRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/OperatorUsageWhitespaceRule.swift index 2cbdf8cbbb..fb8d1ba26e 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/OperatorUsageWhitespaceRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/OperatorUsageWhitespaceRule.swift @@ -220,10 +220,10 @@ private extension Trivia { contains { (element: TriviaPiece) in switch element { case .blockComment, .docLineComment, .docBlockComment, .lineComment: - return true + true case .backslashes, .carriageReturnLineFeeds, .carriageReturns, .formfeeds, .newlines, .pounds, .spaces, .tabs, .unexpectedText, .verticalTabs: - return false + false } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift index fd65e1e32b..8ba7c304a6 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/PreferSelfInStaticReferencesRule.swift @@ -23,9 +23,9 @@ private extension PreferSelfInStaticReferencesRule { var parentName: String? { switch self { - case let .likeClass(name): return name - case let .likeStruct(name): return name - case .skipReferences: return nil + case let .likeClass(name): name + case let .likeStruct(name): name + case .skipReferences: nil } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ShorthandOperatorRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ShorthandOperatorRule.swift index 7912bf25a5..bb56b1bd5e 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ShorthandOperatorRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ShorthandOperatorRule.swift @@ -72,9 +72,9 @@ private extension TokenSyntax { var binaryOperator: String? { switch tokenKind { case .binaryOperator(let str): - return str + str default: - return nil + nil } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/StatementPositionRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/StatementPositionRule.swift index 11cbf7dd71..6a5a7b468d 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/StatementPositionRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/StatementPositionRule.swift @@ -64,9 +64,9 @@ struct StatementPositionRule: CorrectableRule { func validate(file: SwiftLintFile) -> [StyleViolation] { switch configuration.statementMode { case .default: - return defaultValidate(file: file) + defaultValidate(file: file) case .uncuddledElse: - return uncuddledValidate(file: file) + uncuddledValidate(file: file) } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/TrailingWhitespaceRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/TrailingWhitespaceRule.swift index 81cc4201c3..33dfa251cf 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/TrailingWhitespaceRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/TrailingWhitespaceRule.swift @@ -321,9 +321,9 @@ private extension TriviaPiece { var isBlockComment: Bool { switch self { case .blockComment, .docBlockComment: - return true + true default: - return false + false } } } diff --git a/Source/SwiftLintCore/Extensions/SwiftDeclarationAttributeKind+Swiftlint.swift b/Source/SwiftLintCore/Extensions/SwiftDeclarationAttributeKind+Swiftlint.swift index e06d10eefc..5cd3a06dd7 100644 --- a/Source/SwiftLintCore/Extensions/SwiftDeclarationAttributeKind+Swiftlint.swift +++ b/Source/SwiftLintCore/Extensions/SwiftDeclarationAttributeKind+Swiftlint.swift @@ -43,7 +43,7 @@ public extension SwiftDeclarationAttributeKind { public var swiftDeclarationAttributeKinds: Set { switch self { case .acl: - return [ + [ .private, .fileprivate, .internal, @@ -51,7 +51,7 @@ public extension SwiftDeclarationAttributeKind { .open, ] case .setterACL: - return [ + [ .setterPrivate, .setterFilePrivate, .setterInternal, @@ -59,28 +59,28 @@ public extension SwiftDeclarationAttributeKind { .setterOpen, ] case .mutators: - return [ + [ .mutating, .nonmutating, ] case .override: - return [.override] + [.override] case .owned: - return [.weak] + [.weak] case .final: - return [.final] + [.final] case .typeMethods: - return [] + [] case .required: - return [.required] + [.required] case .convenience: - return [.convenience] + [.convenience] case .lazy: - return [.lazy] + [.lazy] case .dynamic: - return [.dynamic] + [.dynamic] case .atPrefixed: - return [ + [ .objc, .nonobjc, .objcMembers, diff --git a/Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift b/Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift index 0a8bb9c83d..2fbb25c527 100644 --- a/Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift +++ b/Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift @@ -126,9 +126,9 @@ public extension DeclModifierSyntax { switch name.tokenKind { case .keyword(.open), .keyword(.public), .keyword(.package), .keyword(.internal), .keyword(.fileprivate), .keyword(.private): - return name.tokenKind + name.tokenKind default: - return nil + nil } } @@ -325,9 +325,9 @@ public extension TriviaPiece { var isHorizontalWhitespace: Bool { switch self { case .spaces, .tabs: - return true + true default: - return false + false } } } diff --git a/Source/SwiftLintCore/Extensions/SyntaxClassification+isComment.swift b/Source/SwiftLintCore/Extensions/SyntaxClassification+isComment.swift index e579c65d4b..81b625b42f 100644 --- a/Source/SwiftLintCore/Extensions/SyntaxClassification+isComment.swift +++ b/Source/SwiftLintCore/Extensions/SyntaxClassification+isComment.swift @@ -5,10 +5,10 @@ public extension SyntaxClassification { var isComment: Bool { switch self { case .lineComment, .docLineComment, .blockComment, .docBlockComment: - return true + true case .none, .keyword, .identifier, .type, .operator, .dollarIdentifier, .integerLiteral, .argumentLabel, .floatLiteral, .stringLiteral, .ifConfigDirective, .attribute, .editorPlaceholder, .regexLiteral: - return false + false } } } diff --git a/Source/SwiftLintCore/Helpers/SwiftSyntaxKindBridge.swift b/Source/SwiftLintCore/Helpers/SwiftSyntaxKindBridge.swift index 637930d2ff..074ad9d766 100644 --- a/Source/SwiftLintCore/Helpers/SwiftSyntaxKindBridge.swift +++ b/Source/SwiftLintCore/Helpers/SwiftSyntaxKindBridge.swift @@ -11,33 +11,33 @@ public enum SwiftSyntaxKindBridge { // swiftlint:disable:previous cyclomatic_complexity switch classification { case .attribute: - return .attributeID + .attributeID case .blockComment, .lineComment: - return .comment + .comment case .docBlockComment, .docLineComment: - return .docComment + .docComment case .dollarIdentifier, .identifier: - return .identifier + .identifier case .editorPlaceholder: - return .placeholder + .placeholder case .floatLiteral, .integerLiteral: - return .number + .number case .ifConfigDirective: - return .poundDirectiveKeyword + .poundDirectiveKeyword case .keyword: - return .keyword + .keyword case .none, .regexLiteral: - return nil + nil case .operator: - return .operator + .operator case .stringLiteral: - return .string + .string case .type: - return .typeidentifier + .typeidentifier case .argumentLabel: - return .argument + .argument @unknown default: - return nil + nil } } diff --git a/Source/SwiftLintCore/Models/AccessControlLevel.swift b/Source/SwiftLintCore/Models/AccessControlLevel.swift index 6570f916d7..3fd4499fc9 100644 --- a/Source/SwiftLintCore/Models/AccessControlLevel.swift +++ b/Source/SwiftLintCore/Models/AccessControlLevel.swift @@ -39,12 +39,12 @@ public enum AccessControlLevel: String, CustomStringConvertible, Sendable { public var description: String { switch self { - case .private: return "private" - case .fileprivate: return "fileprivate" - case .internal: return "internal" - case .package: return "package" - case .public: return "public" - case .open: return "open" + case .private: "private" + case .fileprivate: "fileprivate" + case .internal: "internal" + case .package: "package" + case .public: "public" + case .open: "open" } } } @@ -52,12 +52,12 @@ public enum AccessControlLevel: String, CustomStringConvertible, Sendable { extension AccessControlLevel: Comparable { private var priority: Int { switch self { - case .private: return 1 - case .fileprivate: return 2 - case .internal: return 3 - case .package: return 4 - case .public: return 5 - case .open: return 6 + case .private: 1 + case .fileprivate: 2 + case .internal: 3 + case .package: 4 + case .public: 5 + case .open: 6 } } diff --git a/Source/SwiftLintCore/Models/Command.swift b/Source/SwiftLintCore/Models/Command.swift index 40a2e60f89..f6bafced10 100644 --- a/Source/SwiftLintCore/Models/Command.swift +++ b/Source/SwiftLintCore/Models/Command.swift @@ -15,9 +15,9 @@ public struct Command: Equatable { /// state prior to the current action. package func inverse() -> Self { switch self { - case .enable: return .disable - case .disable: return .enable - case .invalid: return .invalid + case .enable: .disable + case .disable: .enable + case .invalid: .invalid } } } diff --git a/Source/SwiftLintCore/Models/Issue.swift b/Source/SwiftLintCore/Models/Issue.swift index 86e0190b34..8e77b413fb 100644 --- a/Source/SwiftLintCore/Models/Issue.swift +++ b/Source/SwiftLintCore/Models/Issue.swift @@ -119,11 +119,11 @@ public enum Issue: LocalizedError, Equatable { public var errorDescription: String? { switch self { case .genericError: - return "error: \(message)" + "error: \(message)" case .genericWarning: - return "warning: \(message)" + "warning: \(message)" default: - return Self.genericWarning(message).errorDescription + Self.genericWarning(message).errorDescription } } diff --git a/Source/SwiftLintCore/Models/Location.swift b/Source/SwiftLintCore/Models/Location.swift index 4bb174207c..27abd82a34 100644 --- a/Source/SwiftLintCore/Models/Location.swift +++ b/Source/SwiftLintCore/Models/Location.swift @@ -95,11 +95,11 @@ private extension Optional where Wrapped: Comparable { static func < (lhs: Optional, rhs: Optional) -> Bool { switch (lhs, rhs) { case let (lhs?, rhs?): - return lhs < rhs + lhs < rhs case (nil, _?): - return true + true default: - return false + false } } } diff --git a/Source/SwiftLintCore/Models/RuleConfigurationDescription.swift b/Source/SwiftLintCore/Models/RuleConfigurationDescription.swift index b7e5de3b62..76922be927 100644 --- a/Source/SwiftLintCore/Models/RuleConfigurationDescription.swift +++ b/Source/SwiftLintCore/Models/RuleConfigurationDescription.swift @@ -193,13 +193,13 @@ extension OptionType: Documentable { public func markdown() -> String { switch self { case .empty, .flag, .symbol, .integer, .float, .severity: - return yaml() + yaml() case let .string(value): - return """ + value + """ + """ + value + """ case let .list(options): - return "[" + options.map { $0.markdown() }.joined(separator: ", ") + "]" + "[" + options.map { $0.markdown() }.joined(separator: ", ") + "]" case let .nested(value): - return value.markdown() + value.markdown() } } diff --git a/Source/SwiftLintCore/Models/RuleIdentifier.swift b/Source/SwiftLintCore/Models/RuleIdentifier.swift index 5fd1a692f1..7d051054c9 100644 --- a/Source/SwiftLintCore/Models/RuleIdentifier.swift +++ b/Source/SwiftLintCore/Models/RuleIdentifier.swift @@ -18,10 +18,10 @@ public enum RuleIdentifier: Hashable, ExpressibleByStringLiteral, Comparable, Se public var stringRepresentation: String { switch self { case .all: - return Self.allStringRepresentation + Self.allStringRepresentation case .single(let identifier): - return identifier + identifier } } diff --git a/Source/SwiftLintFramework/Configuration/Configuration+Remote.swift b/Source/SwiftLintFramework/Configuration/Configuration+Remote.swift index c083290c8a..af28ab7ca9 100644 --- a/Source/SwiftLintFramework/Configuration/Configuration+Remote.swift +++ b/Source/SwiftLintFramework/Configuration/Configuration+Remote.swift @@ -33,10 +33,10 @@ internal extension Configuration.FileGraph.FilePath { ) throws -> String { switch self { case let .existing(path): - return path + path case let .promised(urlString): - return try resolve( + try resolve( urlString: urlString, remoteConfigTimeout: remoteConfigTimeout, remoteConfigTimeoutIfCached: remoteConfigTimeoutIfCached diff --git a/Source/SwiftLintFramework/Configuration/Configuration+RulesMode.swift b/Source/SwiftLintFramework/Configuration/Configuration+RulesMode.swift index cfc5902261..17d7cca5ea 100644 --- a/Source/SwiftLintFramework/Configuration/Configuration+RulesMode.swift +++ b/Source/SwiftLintFramework/Configuration/Configuration+RulesMode.swift @@ -99,19 +99,19 @@ public extension Configuration { internal func applied(aliasResolver: (String) -> String) -> Self { switch self { case let .defaultConfiguration(disabled, optIn): - return .defaultConfiguration( + .defaultConfiguration( disabled: Set(disabled.map(aliasResolver)), optIn: Set(optIn.map(aliasResolver)) ) case let .onlyConfiguration(onlyRules): - return .onlyConfiguration(Set(onlyRules.map(aliasResolver))) + .onlyConfiguration(Set(onlyRules.map(aliasResolver))) case let .onlyCommandLine(onlyRules): - return .onlyCommandLine(Set(onlyRules.map(aliasResolver))) + .onlyCommandLine(Set(onlyRules.map(aliasResolver))) case .allCommandLine: - return .allCommandLine + .allCommandLine } } diff --git a/Source/SwiftLintFramework/Configuration/Configuration+RulesWrapper.swift b/Source/SwiftLintFramework/Configuration/Configuration+RulesWrapper.swift index 36b2e9bb1b..0c237be766 100644 --- a/Source/SwiftLintFramework/Configuration/Configuration+RulesWrapper.swift +++ b/Source/SwiftLintFramework/Configuration/Configuration+RulesWrapper.swift @@ -83,11 +83,11 @@ internal extension Configuration { lazy var disabledRuleIdentifiers: [String] = { switch mode { case let .defaultConfiguration(disabled, _): - return validate(ruleIds: disabled, valid: validRuleIdentifiers, silent: true) + validate(ruleIds: disabled, valid: validRuleIdentifiers, silent: true) .sorted(by: <) case let .onlyConfiguration(onlyRules), let .onlyCommandLine(onlyRules): - return validate( + validate( ruleIds: Set(allRulesWrapped .map { type(of: $0.rule).identifier } .filter { !onlyRules.contains($0) }), @@ -96,7 +96,7 @@ internal extension Configuration { ).sorted(by: <) case .allCommandLine: - return [] + [] } }() diff --git a/Source/SwiftLintFramework/LintOrAnalyzeCommand.swift b/Source/SwiftLintFramework/LintOrAnalyzeCommand.swift index 4af32083a4..b480b5153c 100644 --- a/Source/SwiftLintFramework/LintOrAnalyzeCommand.swift +++ b/Source/SwiftLintFramework/LintOrAnalyzeCommand.swift @@ -12,18 +12,18 @@ package enum LintOrAnalyzeMode { package var imperative: String { switch self { case .lint: - return "lint" + "lint" case .analyze: - return "analyze" + "analyze" } } package var verb: String { switch self { case .lint: - return "linting" + "linting" case .analyze: - return "analyzing" + "analyzing" } } } diff --git a/Source/SwiftLintFramework/LintableFilesVisitor.swift b/Source/SwiftLintFramework/LintableFilesVisitor.swift index 1a6742b864..098a471490 100644 --- a/Source/SwiftLintFramework/LintableFilesVisitor.swift +++ b/Source/SwiftLintFramework/LintableFilesVisitor.swift @@ -203,15 +203,15 @@ private enum CompileCommandsLoadError: LocalizedError { var errorDescription: String? { switch self { case let .nonExistentFile(path): - return "Could not read compile commands file at '\(path)'" + "Could not read compile commands file at '\(path)'" case let .malformedCommands(path): - return "Compile commands file at '\(path)' isn't in the correct format" + "Compile commands file at '\(path)' isn't in the correct format" case let .malformedFile(path, index): - return "Missing or invalid (must be a string) 'file' key in \(path) at index \(index)" + "Missing or invalid (must be a string) 'file' key in \(path) at index \(index)" case let .malformedArguments(path, index): - return "Missing or invalid (must be an array of strings) 'arguments' key in \(path) at index \(index)" + "Missing or invalid (must be an array of strings) 'arguments' key in \(path) at index \(index)" case let .missingFileInArguments(path, index, arguments): - return "Entry in \(path) at index \(index) has 'arguments' which do not contain the 'file': \(arguments)" + "Entry in \(path) at index \(index) has 'arguments' which do not contain the 'file': \(arguments)" } } } diff --git a/Source/SwiftLintFramework/Reporters/MarkdownReporter.swift b/Source/SwiftLintFramework/Reporters/MarkdownReporter.swift index 642f30faab..6de809d30e 100644 --- a/Source/SwiftLintFramework/Reporters/MarkdownReporter.swift +++ b/Source/SwiftLintFramework/Reporters/MarkdownReporter.swift @@ -36,9 +36,9 @@ struct MarkdownReporter: Reporter { private static func severity(for severity: ViolationSeverity) -> String { switch severity { case .error: - return ":stop\\_sign:" + ":stop\\_sign:" case .warning: - return ":warning:" + ":warning:" } } } diff --git a/Source/SwiftLintFramework/SwiftLintError.swift b/Source/SwiftLintFramework/SwiftLintError.swift index da6ee8fd49..cb4c888550 100644 --- a/Source/SwiftLintFramework/SwiftLintError.swift +++ b/Source/SwiftLintFramework/SwiftLintError.swift @@ -6,7 +6,7 @@ package enum SwiftLintError: LocalizedError { package var errorDescription: String? { switch self { case .usageError(let description): - return description + description } } } diff --git a/Source/swiftlint/Common/LintOrAnalyzeArguments.swift b/Source/swiftlint/Common/LintOrAnalyzeArguments.swift index b47e4162a6..3e304ba648 100644 --- a/Source/swiftlint/Common/LintOrAnalyzeArguments.swift +++ b/Source/swiftlint/Common/LintOrAnalyzeArguments.swift @@ -8,9 +8,9 @@ enum LeniencyOptions: String, EnumerableFlag { static func help(for value: Self) -> ArgumentHelp? { switch value { case .strict: - return "Upgrades warnings to serious violations (errors)." + "Upgrades warnings to serious violations (errors)." case .lenient: - return "Downgrades serious violations to warnings, warning threshold is disabled." + "Downgrades serious violations to warnings, warning threshold is disabled." } } }