gordonklaus / ineffassign

Detect ineffectual assignments in Go code.
MIT License
394 stars 22 forks source link

Allow to disable check in specific line #27

Closed vadorovsky closed 6 years ago

vadorovsky commented 6 years ago

It would be nice to allow to ignore checks by writing comment like:

// ineffassign: ignore

i.e.

res := someFunction() // ineffassign: ignore
gordonklaus commented 6 years ago

The Go language already has a feature for ignoring the result of an assignment, and that is to assign to underscore (_). Or simply omit the left hand side. If for some reason (documentation?) you want to keep the variable (res in your example), just assign it to underscore on the next line (_ = res).

Your example doesn't even pass the compiler's checking, and is thus outside the scope of this tool. As for ignoring ineffessign checks, you'll have to provide compelling reasons (and actual examples) why you don't just fix them instead.

vadorovsky commented 6 years ago

OK, the concrete example is here:

https://github.com/cilium/cilium/blob/master/pkg/bpf/bpffs.go#L261

Yes, ineffassingn complains about that line:

/home/mrostecki/go/src/github.com/cilium/cilium/pkg/bpf/bpffs.go:261:4: ineffectual assignment to err

I would like to ignore that error, because that assignment should be effectual and err variable is returned at the end of function.

gordonklaus commented 6 years ago

Trust the tool, @mrostecki 😄 The err being returned is different from the one being assigned.

On this line, a new err is declared (with a scope extending to the end of the else if block) which shadows the outer err.

I would recommend always returning immediately (like here) when possible.