Skip to content

Commit aeb3b36

Browse files
author
Fahad Zubair
committed
Clippy warnings removed on constraints, k.into() if gated
1 parent 0fc4e4c commit aeb3b36

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/CollectionConstraintViolationGenerator.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class CollectionConstraintViolationGenerator(
7777
// and is for use by the framework.
7878
rustTemplate(
7979
"""
80+
##[allow(clippy::enum_variant_names)]
8081
##[derive(Debug, PartialEq)]
8182
${constraintViolationVisibility.toRustQualifier()} enum $constraintViolationName {
8283
#{ConstraintViolationVariants:W}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedMapGenerator.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators
77

88
import software.amazon.smithy.codegen.core.Symbol
99
import software.amazon.smithy.model.shapes.MapShape
10+
import software.amazon.smithy.model.shapes.StringShape
1011
import software.amazon.smithy.model.shapes.StructureShape
1112
import software.amazon.smithy.model.shapes.UnionShape
1213
import software.amazon.smithy.model.traits.LengthTrait
@@ -21,6 +22,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.expectRustMetadata
2122
import software.amazon.smithy.rust.codegen.core.util.expectTrait
2223
import software.amazon.smithy.rust.codegen.server.smithy.PubCrateConstraintViolationSymbolProvider
2324
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
25+
import software.amazon.smithy.rust.codegen.server.smithy.typeNameContainsNonPublicType
2426

2527
/**
2628
* [ConstrainedMapGenerator] generates a wrapper tuple newtype holding a constrained `std::collections::HashMap`.
@@ -130,14 +132,22 @@ class ConstrainedMapGenerator(
130132
valueShape !is StructureShape &&
131133
valueShape !is UnionShape
132134
) {
135+
val keyShape = model.expectShape(shape.key.target, StringShape::class.java)
136+
val keyNeedsConversion = keyShape.typeNameContainsNonPublicType(model, symbolProvider, publicConstrainedTypes)
137+
val key = if (keyNeedsConversion) {
138+
"k.into()"
139+
} else {
140+
"k"
141+
}
142+
133143
writer.rustTemplate(
134144
"""
135145
impl #{From}<$name> for #{FullyUnconstrainedSymbol} {
136146
fn from(value: $name) -> Self {
137147
value
138148
.into_inner()
139149
.into_iter()
140-
.map(|(k, v)| (k.into(), v.into()))
150+
.map(|(k, v)| ($key, v.into()))
141151
.collect()
142152
}
143153
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/MapConstraintViolationGenerator.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class MapConstraintViolationGenerator(
7676
// and is for use by the framework.
7777
rustTemplate(
7878
"""
79+
##[allow(clippy::enum_variant_names)]
7980
##[derive(Debug, PartialEq)]
8081
pub${ if (constraintViolationVisibility == Visibility.PUBCRATE) " (crate) " else "" } enum $constraintViolationName {
8182
${if (shape.hasTrait<LengthTrait>()) "Length(usize)," else ""}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerBuilderConstraintViolations.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class ServerBuilderConstraintViolations(
7171
writer.docs("Holds one variant for each of the ways the builder can fail.")
7272
if (nonExhaustive) Attribute.NonExhaustive.render(writer)
7373
val constraintViolationSymbolName = constraintViolationSymbolProvider.toSymbol(shape).name
74-
writer.rustBlock("pub${if (visibility == Visibility.PUBCRATE) " (crate) " else ""} enum $constraintViolationSymbolName") {
74+
writer.rustBlock("""
75+
##[allow(clippy::enum_variant_names)]
76+
pub${if (visibility == Visibility.PUBCRATE) " (crate) " else ""} enum $constraintViolationSymbolName""") {
7577
renderConstraintViolations(writer)
7678
}
7779

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/UnconstrainedUnionGenerator.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ class UnconstrainedUnionGenerator(
139139
constraintViolationSymbol
140140
) {
141141
Attribute(derive(RuntimeType.Debug, RuntimeType.PartialEq)).render(this)
142-
rustBlock("pub${if (constraintViolationVisibility == Visibility.PUBCRATE) " (crate)" else ""} enum $constraintViolationName") {
142+
rustBlock("""
143+
##[allow(clippy::enum_variant_names)]
144+
pub${if (constraintViolationVisibility == Visibility.PUBCRATE) " (crate)" else ""} enum $constraintViolationName""") {
143145
constraintViolations().forEach { renderConstraintViolation(this, it) }
144146
}
145147

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/transformers/ConstrainedMemberTransform.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import java.util.*
2222
import software.amazon.smithy.rust.codegen.core.util.UNREACHABLE
2323
import software.amazon.smithy.rust.codegen.core.util.orNull
2424
import software.amazon.smithy.rust.codegen.server.smithy.allConstraintTraits
25-
import software.amazon.smithy.rust.codegen.server.smithy.transformers.ConstrainedMemberTransform.makeNonConstrained
2625

2726
/**
2827
* Transforms all member shapes that have constraints on them into equivalent non-constrained
@@ -86,10 +85,8 @@ object ConstrainedMemberTransform {
8685
.flatMap { it.constrainedMembers() }
8786
.mapNotNull {
8887
val transformation = it.makeNonConstrained(model, additionalNames)
89-
if (transformation != null) {
90-
// Keep record of new names that have been generated to ensure none of them regenerated.
91-
additionalNames.add(transformation.newShape.id)
92-
}
88+
// Keep record of new names that have been generated to ensure none of them regenerated.
89+
additionalNames.add(transformation.newShape.id)
9390

9491
transformation
9592
}
@@ -169,31 +166,31 @@ object ConstrainedMemberTransform {
169166
private fun MemberShape.makeNonConstrained(
170167
model: Model,
171168
additionalNames: MutableSet<ShapeId>,
172-
): MemberShapeTransformation? {
173-
val (constraintTraits, otherTraits) = this.allTraits.values
169+
): MemberShapeTransformation {
170+
val (memberConstraintTraits, otherTraits) = this.allTraits.values
174171
.partition {
175172
memberConstraintTraitsToOverride.contains(it.javaClass)
176173
}
177174

178-
// No transformation required in case the member shape has no constraints.
179-
if (constraintTraits.isEmpty())
180-
return null
175+
check(memberConstraintTraits.isNotEmpty()) {
176+
"There must at least be one member constraint on the shape"
177+
}
181178

182179
// Build a new shape similar to the target of the constrained member shape. It should
183180
// have all of the original constraints that have not been overridden, and the ones
184181
// that this member shape overrides.
185182
val targetShape = model.expectShape(this.target)
186183
if (targetShape !is ToSmithyBuilder<*>)
187-
UNREACHABLE("member target shapes will always be buildable")
184+
UNREACHABLE("Member target shapes will always be buildable")
188185

189186
return when (val builder = targetShape.toBuilder()) {
190187
is AbstractShapeBuilder<*, *> -> {
191188
// Use the target builder to create a new standalone shape that would
192189
// be added to the model later on. Keep all existing traits on the target
193190
// but replace the ones that are overridden on the member shape.
194-
val nonOverriddenTraitsOnTarget =
191+
val nonOverriddenConstraintTraits =
195192
builder.allTraits.values.filter { existingTrait ->
196-
constraintTraits.none { it.toShapeId() == existingTrait.toShapeId() }
193+
memberConstraintTraits.none { it.toShapeId() == existingTrait.toShapeId() }
197194
}
198195

199196
// Add a synthetic constraint on all new shapes being defined, that would link
@@ -203,7 +200,7 @@ object ConstrainedMemberTransform {
203200

204201
// Combine target traits, overridden traits and the synthetic trait
205202
val newTraits =
206-
nonOverriddenTraitsOnTarget + constraintTraits + syntheticTrait
203+
nonOverriddenConstraintTraits + memberConstraintTraits + syntheticTrait
207204

208205
// Create a new unique standalone shape that will be added to the model later on
209206
val shapeId = overriddenShapeId(model, additionalNames, this.id)

0 commit comments

Comments
 (0)