Skip to content

[Bug] @CodedAt with enum with labeled associated values produces compile time warnings #169

Open

Description

@LeoNavel

Hi, it seems that #167 introduced new compile time warning for enums with labeled associated values, for example following code now produces warning - Immutable value 'container' was never used; consider replacing with '_' or removing it

@Codable
@CodedAt("type")
enum TypeObject {
    case type1(number: Int)
}

To Reproduce
Steps to reproduce the behavior:

  1. Create project with following enum
@Codable
@CodedAt("type")
enum TypeObject {
    case type1(number: Int)
}
  1. build

Expected behavior

MetaCodable should generate code that does not produce Swift compiler warnings (especially unused variable warnings) for valid inputs.

Environment (please complete the following information, remove ones not applicable):

  • OS: macOS
  • Version 27
  • Xcode 27.2
  • Swift 6

Additional context

expanded macro:

extension TypeObject: Decodable {
    init(from decoder: any Decoder) throws {
        var typeContainer: KeyedDecodingContainer<CodingKeys>?
        let container = try? decoder.container(keyedBy: CodingKeys.self)
        if let container = container {
            typeContainer = container
        } else {
            typeContainer = nil
        }
        if let typeContainer = typeContainer, let container = container { // this `let container` produces warning
            let typeString: String?
            do {
                typeString = try typeContainer.decodeIfPresent(String.self, forKey: CodingKeys.type) ?? nil
            } catch {
                typeString = nil
            }
            if let typeString = typeString {
                switch typeString {
                case "type1":
                    let number: Int
                    let container = try decoder.container(keyedBy: CodingKeys.self)
                    number = try container.decode(Int.self, forKey: CodingKeys.number)
                    self = .type1(number: number)
                    return
                default:
                    break
                }
            }
        }
        let context = DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Couldn't match any cases."
        )
        throw DecodingError.typeMismatch(Self.self, context)
    }
}

extension TypeObject: Encodable {
    func encode(to encoder: any Encoder) throws {
        let container = encoder.container(keyedBy: CodingKeys.self)
        var typeContainer = container
        switch self {
        case .type1(number: let number):
            try typeContainer.encode("type1", forKey: CodingKeys.type)
            var container = encoder.container(keyedBy: CodingKeys.self)
            try container.encode(number, forKey: CodingKeys.number)
        }
    }
}

extension TypeObject {
    enum CodingKeys: String, CodingKey {
        case type = "type"
        case number = "number"
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions