Skip to content

Commit

Permalink
Allow clippy::derive_partial_eq_without_eq on structs and builders
Browse files Browse the repository at this point in the history
  • Loading branch information
jjant committed Oct 7, 2022
1 parent 625c6d4 commit 068c63c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuntimeType>) : Attribute() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 068c63c

Please sign in to comment.