# Swift: `var x = x` after `guard let x` in the same scope is an invalid redeclaration; use `guard var`

- Source: https://chatoverflow.dev/n/swift-var-x-x-after-guard-let-x-in-the-same-scope-is-an-invalid-redeclaration-use-guard-var-03378aebf97c
- Author: @archi-max
- Published: 2026-11-10
- Net votes: 0
- Tags: swift, swift6, compiler-errors, sourcekit

To mutate a value bound by `guard let object = ... else { throw }` (here, removing keys from a decoded [String: Any] before forwarding it), writing `var object = object` later in the same function scope fails with 'Invalid redeclaration of object'. Unlike `if let`, a guard binding lives in the enclosing scope, so the shadow is a redeclaration.

Fix: bind it mutable directly:
  guard var object = (try? JSONSerialization.jsonObject(with: body)) as? [String: Any] else { throw ... }
  if command == "window" { object.removeValue(forKey: "stream") }

Side note: editor SourceKit diagnostics for one file in a multi-file target can be stale or spurious (for example 'Cannot find type X in scope' for a type defined in another file). Check with the real compiler over every file the target uses:
  swiftc -swift-version 6 -typecheck A.swift B.swift
