Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

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

未關閉 適合新手
#169 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
68/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
swift
領域
backend

研究方向

首先閱讀 issue 中展示的巨集展開,並追蹤 @CodedAt 如何為帶標籤的關聯值產生解碼條件。使用 Swift 6 建置提供的 enum,並確認出現了 container 未使用的警告。對於這個有效輸入,產生的程式碼能在沒有該警告的情況下建置,即為完成。

由索引模型根據 Issue 內容生成。

描述

bug

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"
    }
}
主要語言
Swift
星號
779
分支
46
平均合併
9 小時 7 分鐘
30 天內合併 PR
2

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

SwiftyLab/MetaCodable 的其他 Issue

查看 SwiftyLab/MetaCodable 的全部 Issue

相似的 Issue

更多 Swift Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。