Skip to content

Commit 035c99e

Browse files
authored
#[allow(unused_variables)] in JSON serializers when structure has no members (#2538)
Which is more informational than generating artificial code to suppress the Clippy warning.
1 parent 7e6f2c9 commit 035c99e

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ import software.amazon.smithy.model.shapes.StructureShape
2626
import software.amazon.smithy.model.shapes.TimestampShape
2727
import software.amazon.smithy.model.shapes.UnionShape
2828
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format.EPOCH_SECONDS
29+
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
2930
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
3031
import software.amazon.smithy.rust.codegen.core.rustlang.rust
3132
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
3233
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlockTemplate
3334
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
3435
import software.amazon.smithy.rust.codegen.core.rustlang.withBlock
36+
import software.amazon.smithy.rust.codegen.core.rustlang.writable
3537
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
3638
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
3739
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider
@@ -312,21 +314,25 @@ class JsonSerializerGenerator(
312314
context: StructContext,
313315
includedMembers: List<MemberShape>? = null,
314316
) {
315-
val structureSymbol = symbolProvider.toSymbol(context.shape)
316317
val structureSerializer = protocolFunctions.serializeFn(context.shape) { fnName ->
318+
val inner = context.copy(objectName = "object", localName = "input")
319+
val members = includedMembers ?: inner.shape.members()
320+
val allowUnusedVariables = writable {
321+
if (members.isEmpty()) { Attribute.AllowUnusedVariables.render(this) }
322+
}
317323
rustBlockTemplate(
318-
"pub fn $fnName(object: &mut #{JsonObjectWriter}, input: &#{Input}) -> Result<(), #{Error}>",
319-
"Input" to structureSymbol,
324+
"""
325+
pub fn $fnName(
326+
#{AllowUnusedVariables:W} object: &mut #{JsonObjectWriter},
327+
#{AllowUnusedVariables:W} input: &#{StructureSymbol},
328+
) -> Result<(), #{Error}>
329+
""",
330+
"StructureSymbol" to symbolProvider.toSymbol(context.shape),
331+
"AllowUnusedVariables" to allowUnusedVariables,
320332
*codegenScope,
321333
) {
322-
context.copy(objectName = "object", localName = "input").also { inner ->
323-
val members = includedMembers ?: inner.shape.members()
324-
if (members.isEmpty()) {
325-
rust("let (_, _) = (object, input);") // Suppress unused argument warnings
326-
}
327-
for (member in members) {
328-
serializeMember(MemberContext.structMember(inner, member, symbolProvider, jsonName))
329-
}
334+
for (member in members) {
335+
serializeMember(MemberContext.structMember(inner, member, symbolProvider, jsonName))
330336
}
331337
rust("Ok(())")
332338
}

0 commit comments

Comments
 (0)