awslabs / aws-sdk-swift

Apache License 2.0
386 stars 75 forks source link

Correct unmutated/unused variable warnings when building SDK #1770

Open jbelkins opened 1 week ago

jbelkins commented 1 week ago

Describe the bug

When building the SDK, the following warnings are received:

/Users/jbelkins/Code/Xcode/aws-sdk-swift/Sources/Services/AWSWAFV2/Sources/AWSWAFV2/Models.swift:12427:13: warning: variable 'value' was never mutated; consider changing to 'let' constant
12425 |     static func read(from reader: SmithyJSON.Reader) throws -> WAFV2ClientTypes.NotStatement {
12426 |         guard reader.hasContent else { throw SmithyReadWrite.ReaderError.requiredValueNotPresent }
12427 |         var value = WAFV2ClientTypes.NotStatement()
      |             `- warning: variable 'value' was never mutated; consider changing to 'let' constant
12428 |         value.statement = try reader["Statement"].readIfPresent(with: WAFV2ClientTypes.Statement.read(from:))
12429 |         return value
/Users/jbelkins/Code/Xcode/aws-sdk-swift/Sources/Services/AWSQBusiness/Sources/AWSQBusiness/Models.swift:9946:39: warning: immutable value 'value' was never used; consider replacing with '_' or removing it
 9944 |                 try writer["payloadFieldNameSeparator"].write(value.payloadFieldNameSeparator, with: SmithyReadWrite.WritingClosures.writeString(value:to:))
 9945 |                 payload = try writer.data()
 9946 |             case .endofinputevent(let value):
      |                                       `- warning: immutable value 'value' was never used; consider replacing with '_' or removing it
 9947 |                 headers.append(.init(name: ":event-type", value: .string("endOfInputEvent")))
 9948 |             case .authchallengeresponseevent(let value):

Correct codegen so that these warnings are not received.

Expected Behavior

SDK should build without warnings.

Current Behavior

SDK builds with the warnings listed above.

Reproduction Steps

Build the AWSWAFV2 and AWSQBusiness services Observe the compiler warnings

Possible Solution

No response

Additional Information/Context

No response

AWS SWIFT SDK version used

1.0.3 / latest

Compiler and Version used

Xcode 16.0

Operating System and version

macOS 14

jbelkins commented 6 days ago

In the case of AWSWAFV2 above, the cause is that the property being modified is @Indirect:

    public struct NotStatement: Swift.Sendable {
        /// The statement to negate. You can use any statement that can be nested.
        /// This member is required.
        @Indirect public var statement: WAFV2ClientTypes.Statement?

        public init(
            statement: WAFV2ClientTypes.Statement? = nil
        )
        {
            self.statement = statement
        }
    }
jbelkins commented 6 days ago

In the case of AWSQBusiness, the value is a struct with no members that is being written to an event stream.

    public struct EndOfInputEvent: Swift.Sendable {

        public init() { }
    }

The generated code only sends an :event-type in this event message:

 headers.append(.init(name: ":event-type", value: .string("endOfInputEvent")))

It is worth investigation to see if this creates a valid message on the event stream.