diff --git a/go/invalid-usage-of-modified-variable.go b/go/invalid-usage-of-modified-variable.go index f07719e..81ceecc 100644 --- a/go/invalid-usage-of-modified-variable.go +++ b/go/invalid-usage-of-modified-variable.go @@ -1,6 +1,7 @@ package main import ( + "math/rand" "fmt" ) @@ -263,6 +264,26 @@ func getEngineerAddressByIndex(slice []Engineer, idx int) (*Address, error) { return slice[idx].Address, nil } +func getRandEngineer(slice []Engineer) (*Engineer, error) { + idx := rand.Int() + // ok: invalid-usage-of-modified-variable + eng, err := getEngineerAtIndex(slice, idx) + if err != nil { + return eng, err + } + return eng, nil +} + +func getRandAddress(slice []Engineer) (*Address, error) { + idx := rand.Int() + // ruleid: invalid-usage-of-modified-variable + eng, err := getEngineerAtIndex(slice, idx) + if err != nil { + return eng.Address, err + } + return eng.Address, nil +} + func getEngineers() []Engineer { return []Engineer{ { diff --git a/go/invalid-usage-of-modified-variable.yaml b/go/invalid-usage-of-modified-variable.yaml index 91d204a..9475a5b 100644 --- a/go/invalid-usage-of-modified-variable.yaml +++ b/go/invalid-usage-of-modified-variable.yaml @@ -37,4 +37,9 @@ rules: if err != nil { $X = ... } + - pattern-not: | + $X, err = ... + if err != nil { + return $X, err + } \ No newline at end of file