go-tools icon indicating copy to clipboard operation
go-tools copied to clipboard

errcheck: bufio.NewScanner return values that never get .Err() called

Open tv42 opened this issue 7 years ago • 1 comments

There's (apparently) a common mistake where people forget to check bufio.Scanner errors after the loop ends:

func foo(r io.Reader) {
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

Any return value of bufio.NewScanner that doesn't see a call for scanner.Err is a mistake.

To keep analysis simple and avoid false positives, maybe bail out on complex scenarios like where the scanner value is passed outside of the local scope.

tv42 avatar Sep 20 '18 23:09 tv42

See also https://github.com/golang/go/issues/17747.

mdlayher avatar Mar 09 '22 20:03 mdlayher