Skip to content

Commit

Permalink
Fix warning for unwrap_or_else in generated builders (#3307)
Browse files Browse the repository at this point in the history
While attempting to upgrade to Smithy 1.42, I noticed a warning in the
codegen-client integration tests:
```
warning: unnecessary closure used to substitute value for `Option::None`
   --> json_rpc10/rust-client-codegen/src/operation/operation_with_defaults/_operation_with_defaults_output.rs:477:31
    |
477 |             default_int_enum: self.default_int_enum.unwrap_or_else(|| 1),
    |                               ^^^^^^^^^^^^^^^^^^^^^^--------------------
    |                                                     |
    |                                                     help: use `unwrap_or(..)` instead: `unwrap_or(1)`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
```

This change fixes these warnings by switching between unwrap_or_else and
unwrap_or based on the shape.

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
  • Loading branch information
jdisanti authored Dec 14, 2023
1 parent b180199 commit ec30bef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ class BuilderGenerator(
if (default != null) {
if (default.isRustDefault) {
rust(".unwrap_or_default()")
} else if (default.complexType) {
rust(".unwrap_or_else(|| #T)", default.expr)
} else {
rust(".unwrap_or_else(#T)", default.expr)
rust(".unwrap_or(#T)", default.expr)
}
} else {
withBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package software.amazon.smithy.rust.codegen.core.smithy.generators

import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.BooleanShape
import software.amazon.smithy.model.shapes.EnumShape
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.NumberShape
import software.amazon.smithy.model.shapes.SimpleShape
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.rust
Expand All @@ -23,17 +26,21 @@ class DefaultValueGenerator(
) {
private val instantiator = PrimitiveInstantiator(runtimeConfig, symbolProvider)

data class DefaultValue(val isRustDefault: Boolean, val expr: Writable)
data class DefaultValue(val isRustDefault: Boolean, val expr: Writable, val complexType: Boolean)

/** Returns the default value as set by the defaultValue trait */
fun defaultValue(member: MemberShape): DefaultValue? {
val target = model.expectShape(member.target)
val complexType = when (target) {
is NumberShape, is EnumShape, is BooleanShape -> false
else -> true
}
return when (val default = symbolProvider.toSymbol(member).defaultValue()) {
is Default.NoDefault -> null
is Default.RustDefault -> DefaultValue(isRustDefault = true, writable("Default::default"))
is Default.RustDefault -> DefaultValue(isRustDefault = true, writable("Default::default"), complexType)
is Default.NonZeroDefault -> {
val instantiation = instantiator.instantiate(target as SimpleShape, default.value)
DefaultValue(isRustDefault = false, writable { rust("||#T", instantiation) })
DefaultValue(isRustDefault = false, writable { rust("#T", instantiation) }, complexType)
}
}
}
Expand Down

0 comments on commit ec30bef

Please sign in to comment.