Skip to content

Commit

Permalink
Update generator to appease clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Dec 12, 2022
1 parent 09f6ed6 commit 5c70ac0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -128,6 +127,19 @@ internal class EndpointResolverGenerator(stdlib: List<CustomRuntimeFunction>, 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 {
Expand Down Expand Up @@ -190,6 +202,7 @@ internal class EndpointResolverGenerator(stdlib: List<CustomRuntimeFunction>, ru
fnsUsed: List<CustomRuntimeFunction>,
): 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 {
Expand Down Expand Up @@ -270,7 +283,7 @@ internal class EndpointResolverGenerator(stdlib: List<CustomRuntimeFunction>, 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5c70ac0

Please sign in to comment.