-
-
Notifications
You must be signed in to change notification settings - Fork 42
Description
- I have read the documentation.
- I have read the limitations.
- I could not find my issue.
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 usedExpanded 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.