Last active
November 18, 2022 18:05
-
-
Save loren-osborn/8f15cae5f95545d29c06105598883e0d to your computer and use it in GitHub Desktop.
This is a demo of a proposed exception to warning S1021 in `staticcheck`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Package main is a toy app demoing a defect in `staticcheck`s test for "S1021" | |
package main | |
import "fmt" | |
// Foo is a sample custom type | |
type Foo struct { | |
Value int | |
runabble func() | |
} | |
// NewFoo creates a new Foo | |
func NewFoo(v int, r func()) *Foo { | |
return &Foo{ | |
Value: v, | |
runabble: r, | |
} | |
} | |
// RunNow executes runnable | |
func (f *Foo) RunNow() { | |
f.runabble() | |
} | |
// Bar is another sample custom type | |
type Bar struct { | |
Value int | |
} | |
// ScaleFoo scales the value of the given Foo by this Bar | |
func (b *Bar) ScaleFoo(f *Foo) { | |
f.Value *= b.Value | |
} | |
func main() { | |
myBar := &Bar{Value: 5} | |
// This is where we expect `staticcheck` to report a S1021 warning, | |
// but in this case it shouldn't, because defining `myFoo` with `:=` | |
// on line 42 would prevent it from being used on line 43 where it isn't | |
// defined yet. | |
var myFoo *Foo | |
myFoo = NewFoo(7, func() { | |
myBar.ScaleFoo(myFoo) | |
}) | |
fmt.Printf("After creating myFoo, it looks like this: %#v\n", *myFoo) | |
myFoo.RunNow() | |
fmt.Printf("After running RunNow() on it, it looks like this: %#v\n", *myFoo) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment