From 068c63ca2030879584e7b602bd0648abab19cabe Mon Sep 17 00:00:00 2001 From: Julian Antonielli Date: Fri, 7 Oct 2022 15:43:29 +0100 Subject: [PATCH] Allow clippy::derive_partial_eq_without_eq on structs and builders --- .../smithy/rust/codegen/core/rustlang/RustType.kt | 1 + .../core/smithy/generators/BuilderGenerator.kt | 11 +++++++---- .../core/smithy/generators/StructureGenerator.kt | 9 +++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt index 24344b57c7..854fa430ed 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustType.kt @@ -387,6 +387,7 @@ sealed class Attribute { val AllowDeadCode = Custom("allow(dead_code)") val DocHidden = Custom("doc(hidden)") val DocInline = Custom("doc(inline)") + val DerivePartialEqWithoutEq = Custom("allow(clippy::derive_partial_eq_without_eq)") } data class Derives(val derives: Set) : Attribute() { diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/BuilderGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/BuilderGenerator.kt index c561b04c39..13f0df20b1 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/BuilderGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/BuilderGenerator.kt @@ -9,6 +9,7 @@ import software.amazon.smithy.codegen.core.Symbol import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.MemberShape import software.amazon.smithy.model.shapes.StructureShape +import software.amazon.smithy.rust.codegen.core.rustlang.Attribute import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWords import software.amazon.smithy.rust.codegen.core.rustlang.RustType import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter @@ -159,10 +160,12 @@ class BuilderGenerator( writer.docs("A builder for #D.", structureSymbol) // Matching derives to the main structure + `Default` since we are a builder and everything is optional. - val baseDerives = structureSymbol.expectRustMetadata().derives - val derives = baseDerives.derives.intersect(setOf(RuntimeType.Debug, RuntimeType.PartialEq, RuntimeType.Clone)) + RuntimeType.Default - baseDerives.copy(derives = derives).render(writer) - writer.rustBlock("pub struct $builderName") { + val containerMeta = structureSymbol.expectRustMetadata() + val derives = containerMeta.derives.copy(derives = containerMeta.derives.derives.intersect(setOf(RuntimeType.Debug, RuntimeType.PartialEq, RuntimeType.Clone)) + RuntimeType.Default) + val additionalAttributes = containerMeta.additionalAttributes.toMutableList(); + additionalAttributes.add(Attribute.DerivePartialEqWithoutEq) + containerMeta.copy(derives = derives, additionalAttributes = additionalAttributes).render(writer) + writer.rustBlock("struct $builderName") { for (member in members) { val memberName = symbolProvider.toMemberName(member) // All fields in the builder are optional. diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt index 5bf6e0a6b8..7f2fa9ae84 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt @@ -24,6 +24,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.isDeref import software.amazon.smithy.rust.codegen.core.rustlang.render import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock +import software.amazon.smithy.rust.codegen.core.rustlang.Attribute import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider @@ -160,10 +161,14 @@ open class StructureGenerator( writer.documentShape(shape, model) writer.deprecatedShape(shape) val withoutDebug = containerMeta.derives.copy(derives = containerMeta.derives.derives - RuntimeType.Debug) - containerMeta.copy(derives = withoutDebug).render(writer) + val additionalAttributes = containerMeta.additionalAttributes.toMutableList(); + additionalAttributes.add(Attribute.DerivePartialEqWithoutEq) + containerMeta.copy( + derives = withoutDebug, + additionalAttributes = additionalAttributes).render(writer) writer.rustBlock("struct $name ${lifetimeDeclaration()}") { - writer.forEachMember(members) { member, memberName, memberSymbol -> + writer.forEachMember(members) { member, memberName, memberSymbol -> renderStructureMember(writer, member, memberName, memberSymbol) } }