Skip to content

Commit 4597059

Browse files
author
Fahad Zubair
committed
Generate alternate code to overcome clippy warnings
1 parent 855a457 commit 4597059

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

buildSrc/src/main/kotlin/CodegenTestCommon.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ fun Project.registerGenerateCargoConfigTomlTask(outputDir: File) {
291291
.writeText(
292292
"""
293293
[build]
294-
rustflags = ["--cfg", "aws_sdk_unstable"]
294+
rustflags = ["--deny", "warnings", "--cfg", "aws_sdk_unstable"]
295295
""".trimIndent(),
296296
)
297297
}

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,21 @@ class ServerBuilderGenerator(
351351
if (!constrainedTypeHoldsFinalType(member)) varExpr = "($varExpr).into()"
352352

353353
if (wrapInMaybeConstrained) {
354-
conditionalBlock("input.map(##[allow(clippy::redundant_closure)] |v| ", ")", conditional = symbol.isOptional()) {
355-
conditionalBlock("Box::new(", ")", conditional = hasBox) {
356-
rust("$maybeConstrainedVariant($varExpr)")
354+
// Temporary fix for `#[allow(clippy::redundant_closure)]` not working in `rustc` version 1.82.
355+
// The issue occurs specifically when we generate code in the form:
356+
// ```rust
357+
// input.map(|v| MaybeConstrained(v))
358+
// ```
359+
// This pattern triggers a clippy warning about redundant closures, even with the allow attribute.
360+
// For this specific pattern, we directly use the constructor function reference.
361+
// Other cases like `Box::new(v)` or `v.into()` are valid closure usages and remain unchanged.
362+
if (symbol.isOptional() && varExpr == "v") {
363+
rust("input.map($maybeConstrainedVariant)")
364+
} else {
365+
conditionalBlock("input.map(##[allow(clippy::redundant_closure)] |v| ", ")", conditional = symbol.isOptional()) {
366+
conditionalBlock("Box::new(", ")", conditional = hasBox) {
367+
rust("$maybeConstrainedVariant($varExpr)")
368+
}
357369
}
358370
}
359371
} else {

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

+28-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import software.amazon.smithy.model.shapes.LongShape
2727
import software.amazon.smithy.model.shapes.MapShape
2828
import software.amazon.smithy.model.shapes.MemberShape
2929
import software.amazon.smithy.model.shapes.NumberShape
30+
import software.amazon.smithy.model.shapes.Shape
3031
import software.amazon.smithy.model.shapes.ShortShape
3132
import software.amazon.smithy.model.shapes.StringShape
3233
import software.amazon.smithy.model.shapes.TimestampShape
@@ -84,8 +85,18 @@ fun generateFallbackCodeToDefaultValue(
8485
symbolProvider: RustSymbolProvider,
8586
publicConstrainedTypes: Boolean,
8687
) {
87-
var defaultValue = defaultValue(model, runtimeConfig, symbolProvider, member)
8888
val targetShape = model.expectShape(member.target)
89+
// Temporary fix for `#[allow(clippy::redundant_closure)]` not working in `rustc` version 1.82.
90+
// The issue occurs specifically when we generate code in the form:
91+
// ```rust
92+
// .unwrap_or_else(HashMap::new())
93+
// ```
94+
if (isTargetListOrMap(targetShape, member)) {
95+
writer.rustTemplate(".unwrap_or_default()")
96+
return
97+
}
98+
99+
var defaultValue = defaultValue(model, runtimeConfig, symbolProvider, member)
89100
val targetSymbol = symbolProvider.toSymbol(targetShape)
90101
// We need an .into() conversion to create defaults for the server types. A larger scale refactoring could store this information in the
91102
// symbol, however, retrieving it in this manner works for the moment.
@@ -125,6 +136,22 @@ fun generateFallbackCodeToDefaultValue(
125136
}
126137
}
127138

139+
private fun isTargetListOrMap(
140+
targetShape: Shape?,
141+
member: MemberShape,
142+
): Boolean {
143+
if (targetShape is ListShape) {
144+
val node = member.expectTrait<DefaultTrait>().toNode()!!
145+
check(node is ArrayNode && node.isEmpty)
146+
return true
147+
} else if (targetShape is MapShape) {
148+
val node = member.expectTrait<DefaultTrait>().toNode()!!
149+
check(node is ObjectNode && node.isEmpty)
150+
return true
151+
}
152+
return false
153+
}
154+
128155
/**
129156
* Returns a writable to construct a Rust value of the correct type holding the modeled `@default` value on the
130157
* [member] shape.

0 commit comments

Comments
 (0)