nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 640 forks source link

redundantSelf breaks older swift versions #1032

Closed Cyberbeni closed 3 years ago

Cyberbeni commented 3 years ago

Version where it was correctly working: 0.48.11

Version where I noticed the issue: 0.48.14

command: swiftformat --swiftversion 3.0 test.swift

test.swift contents:

fileprivate struct _JSONKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
    private let container: [String: Any]
    public func nestedContainer<NestedKey>(keyedBy _: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> {
        decoder.codingPath.append(key)
        defer { self.decoder.codingPath.removeLast() }

        guard let value = self.container[key.stringValue] else {
            throw DecodingError.keyNotFound(key,
                                            DecodingError.Context(codingPath: codingPath,
                                                                    debugDescription: "Cannot get \(KeyedDecodingContainer<NestedKey>.self) -- no value found for key \(_errorDescription(of: key))"))
        }

        guard let dictionary = value as? [String: Any] else {
            throw DecodingError._typeMismatch(at: codingPath, expectation: [String: Any].self, reality: value)
        }

        let container = _JSONKeyedDecodingContainer<NestedKey>(referencing: decoder, wrapping: dictionary)
        return KeyedDecodingContainer(container)
    }
}

self is removed from line 7, causing the following error with Xcode 8.2: use of local variable 'container' before its declaration

nicklockwood commented 3 years ago

@Cyberbeni fixed in 0.48.15