dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.21k stars 377 forks source link

SA4006: check misses some seemingly-simple cases #1609

Closed cespare closed 1 month ago

cespare commented 1 month ago

I'm on 2024.1.1 but I also checked on master.

I'm on linux/amd64 but I don't think it matters.

Here's a small program that yields no warnings in staticcheck:

package main

func main() {
    var x int
    x = 3 // 1: could be flagged
    x = 5
    _ = x

    var y int
    for range 10 {
        y = y + 5 // 2: could be flagged
    }
}

I've noted two lines where a variable is written that is later unconditionally clobbered (case 1) or just never read (case 2). ISTM that SA4006 could be made to recognize these situations. Feels like it shouldn't be too difficult in an SSA-based analysis but I haven't tried implementing it so maybe it's harder than I think.

arp242 commented 1 month ago

I wonder if that first example should be flagged. Intuitively I would say that it should silence these kind of errors if you explicitly do _ = x .

cespare commented 1 month ago

The _ = x wasn't the important part there. You can substitute fmt.Println(x) if you want.

dominikh commented 1 month ago

This is probably #191.

cespare commented 1 month ago

Yep, looks like a duplicate of #191.