Skip to content

[1.0] Incorrect variable scope resolution in switch context #1269

@dlsniper

Description

@dlsniper

In the following example, the var err declared in main() will be flagged as unused variable.
However, in the switch syntax it it used correctly.
If you look at the screenshots, it reveals how the shorthand assignment from withing the case statement
will take over as the one that declares the variable for the remaining of the code execution while in the switch statement.

package main

import "fmt"

func Demo() error {
    return fmt.Errorf("err")
}

func main() {
    var err error

    switch  {
    case 1==2:
        err := Demo()
        panic(err)
    default:
        err = Demo()
        panic(err)
    }
    //panic(err)
}

snapshot109

snapshot110

Thank you.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions