diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/generators/EndpointResolverGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/generators/EndpointResolverGenerator.kt index 0a87d9e81f0..cc0deda4acd 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/generators/EndpointResolverGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/generators/EndpointResolverGenerator.kt @@ -8,7 +8,6 @@ package software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators import software.amazon.smithy.rulesengine.language.Endpoint import software.amazon.smithy.rulesengine.language.EndpointRuleSet import software.amazon.smithy.rulesengine.language.eval.Type -import software.amazon.smithy.rulesengine.language.syntax.Identifier import software.amazon.smithy.rulesengine.language.syntax.expr.Expression import software.amazon.smithy.rulesengine.language.syntax.expr.Reference import software.amazon.smithy.rulesengine.language.syntax.fn.Function @@ -128,6 +127,19 @@ internal class EndpointResolverGenerator(stdlib: List, ru "EndpointError" to types.resolveEndpointError, "DiagnosticCollector" to endpointsLib("diagnostic").toType().member("DiagnosticCollector"), ) + + private val allowLintsForResolver = listOf( + // we generate if x { if y { if z { ... } } } + "clippy::collapsible_if", + // we generate `if (true) == expr { ... }` + "clippy::bool_comparison", + // we generate `if !(a == b)` + "clippy::nonminimal_bool", + // we generate `if x == "" { ... }` + "clippy::comparison_to_empty", + // we generate `if let Some(_) = ... { ... }` + "clippy::redundant_pattern_matching", + ) private val context = Context(registry, runtimeConfig) companion object { @@ -190,6 +202,7 @@ internal class EndpointResolverGenerator(stdlib: List, ru fnsUsed: List, ): RuntimeType { return RuntimeType.forInlineFun("resolve_endpoint", EndpointsImpl) { + allowLintsForResolver.map { Attribute.Custom("allow($it)") }.map { it.render(this) } rustTemplate( """ pub(super) fn resolve_endpoint($ParamsName: &#{Params}, $DiagnosticCollector: &mut #{DiagnosticCollector}, #{additional_args}) -> #{endpoint}::Result { @@ -270,7 +283,7 @@ internal class EndpointResolverGenerator(stdlib: List, ru // 2. the RHS returns a boolean which we need to gate on // 3. the RHS is infallible (e.g. uriEncode) val resultName = - (condition.result.orNull() ?: (fn as? Reference)?.name ?: Identifier.of("_")).rustName() + (condition.result.orNull() ?: (fn as? Reference)?.name)?.rustName() ?: "_" val target = generator.generate(fn) val next = generateRuleInternal(rule, rest) when { diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/rulesgen/ExpressionGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/rulesgen/ExpressionGenerator.kt index 63871613c80..f0bad1aac2e 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/rulesgen/ExpressionGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/rulesgen/ExpressionGenerator.kt @@ -49,9 +49,13 @@ class ExpressionGenerator( } override fun visitRef(ref: Reference) = writable { - rust(ref.name.rustName()) if (ownership == Ownership.Owned) { - rust(".to_owned()") + when (ref.type()) { + is Type.Bool -> rust("*${ref.name.rustName()}") + else -> rust("${ref.name.rustName()}.to_owned()") + } + } else { + rust(ref.name.rustName()) } } @@ -64,7 +68,7 @@ class ExpressionGenerator( is GetAttr.Part.Index -> rust(".get(${part.index()}).cloned()") // we end up with Option<&&T>, we need to get to Option<&T> } } - if (ownership == Ownership.Owned) { + if (ownership == Ownership.Owned && getAttr.type() != Type.bool()) { if (getAttr.type() is Type.Option) { rust(".map(|t|t.to_owned())") } else { diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/rulesgen/TemplateGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/rulesgen/TemplateGenerator.kt index e06d5d67185..4d4bae43a14 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/rulesgen/TemplateGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/rulesgen/TemplateGenerator.kt @@ -47,7 +47,11 @@ class TemplateGenerator( } override fun visitStaticElement(str: String) = writable { - rust("out.push_str(${str.dq()});") + when (str.length) { + 0 -> {} + 1 -> rust("out.push('$str');") + else -> rust("out.push_str(${str.dq()});") + } } override fun visitDynamicElement(expr: Expression) = writable { diff --git a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/endpoint/EndpointsDecoratorTest.kt b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/endpoint/EndpointsDecoratorTest.kt index 8830223856e..a6d1ea55e7c 100644 --- a/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/endpoint/EndpointsDecoratorTest.kt +++ b/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/endpoint/EndpointsDecoratorTest.kt @@ -35,7 +35,11 @@ class EndpointsDecoratorTest { @endpointRuleSet({ "version": "1.0", "rules": [{ - "conditions": [{"fn": "isSet", "argv": [{"ref":"Region"}]}], + "conditions": [ + {"fn": "isSet", "argv": [{"ref":"Region"}]}, + {"fn": "isSet", "argv": [{"ref":"ABoolParam"}]}, + {"fn": "booleanEquals", "argv": [{"ref": "ABoolParam"}, false]} + ], "type": "endpoint", "endpoint": { "url": "https://www.{Region}.example.com"