esimonov / ifshort

Go linter for checking that your code uses short syntax for if-statements whenever possible.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

False positive when variable is used in select statement

armsnyder opened this issue · comments

False positive:

package main

import (
	"flag"
	"fmt"
	"time"
)

func main() {
	delayPtr := flag.Duration("delay", 0, "")
	flag.Parse()
	delay := *delayPtr
	if delay == 0 {
		delay = time.Second
	}
	select {
	case <-time.After(delay):
		fmt.Println("hello")
	}
}

Linter error:

example/main.go:12:2: variable 'delay' is only used in the if-statement (example/main.go:13:2); consider using short syntax (ifshort)
	delay := *delayPtr
	^

In a larger example, you can imaging a for-select loop where there is another case like checking if a context is cancelled.

This is possibly related to #17 except instead of assigning to the variable in the select statement, it is read.