Unverified Commit 24029ab2 authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Fix warning for unwrap_or_else in generated builders (#3307)

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._
parent fe367274
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -442,8 +442,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(
+11 −3
Original line number Diff line number Diff line
@@ -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
@@ -23,17 +26,22 @@ 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)
            }
        }
    }