[Bug] @CodedAt with enum with labeled associated values produces compile time warnings
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
Start by reading the macro expansion shown in the issue and tracing how @CodedAt generates the decoding condition for labeled associated values. Build the provided enum with Swift 6 and confirm the unused container warning. Done when the generated code for this valid input builds without that warning.
Written by the indexing model from the issue text.
Description
- I have read the documentation.
- I have read the limitations.
- I could not find my issue.
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:
- Create project with following
enum
@Codable
@CodedAt("type")
enum TypeObject {
case type1(number: Int)
}
- 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"
}
}
- Dominant language
- Swift
- Stars
- 779
- Forks
- 46
- Avg merge
- 9h 7m
- Merged PRs (30d)
- 2
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from SwiftyLab/MetaCodable
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 35/100
SwiftyLab/MetaCodable#82 · 6 comments ·
-
enhancement good first issue
Difficulty 4/5 3-5 days Newbie friendliness 32/100
SwiftyLab/MetaCodable#49 · 5 comments · 2 reactions ·
All issues in SwiftyLab/MetaCodable
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
521xueweihan/HelloGitHub#4131 ·
-
[Bug] Error: thaw@beta: thaw@beta: This cask does not run on macOS versions older than Golden Gate.Openmacos-27 ops
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
thaw-app/Thaw#1400 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ParetoSecurity/pareto-mac#324 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
duongductrong/Snapzy#691 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100