Skip to content

[Bug] Unused container variable warning in code generated by MetaCodable macros #154

@LeoNavel

Description

@LeoNavel

Describe the bug

The most recent version (v1.6.0) of MetaCodable generates Swift code that triggers a compile-time warning about an unused variable inside macro expansions.

Specifically, the generated Decodable implementation contains a variable that is never used, causing this warning:

  • Immutable value 'container' was never used; consider replacing with '_' or removing it

This warning appears even for a minimal example and pollutes builds with warnings originating from generated code.

To Reproduce

Minimal example:

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

Build the project and observe the warning produced by the macro-generated code.

Expected behavior

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

Actual Behavior

The macro expands into code that contains an unused variable, producing a warning.

For example, the generated Decodable implementation contains this line:

if let typeContainer = typeContainer, let container = container {
    // warning: Immutable value 'container' was never used

Expanded Code

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 { // warning here
            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":
                    self = .type1
                    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:
            break
        }
    }
}

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

Screenshots

Not applicable.

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

  • OS: macOS
  • Version: 26.2
  • Xcode: 26.2
  • Swift: 6.2.3

Additional context

This looks like a code generation issue where a temporary variable is emitted but never used. It’s not a functional bug, but it introduces warnings from generated code, which makes warning-clean builds impossible on the user side.

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