gordonklaus / ineffassign

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

False positive #2 #19

Closed ericlagergren closed 7 years ago

ericlagergren commented 7 years ago

This finds a false positive on line 25:

// +build ignore

package main

import (
    "math"
    "math/rand"
)

func main() {
    const tiny = 1e-30

    t := 0

    f := t
    if f == 0 {
        f = tiny
    }
    C := f
    D := 0.0
    delta := 0.0

    for i := 0; i < math.MaxInt64; i++ {
        t = rand.Intn()

        D = t.B + t.A*D
        if D == 0 {
            D = tiny
        }
        C = t.B + t.A/C
        if C == 0 {
            C = tiny
        }
        D = 1 / D
        delta = C * D
        f = f * delta
        if math.Abs(delta-1) <= eps {
            break
        }
    }
    return f
}
gordonklaus commented 7 years ago

The only ineffectual assignment I see flagged is on line 21: delta := 0.0. Which is a true positive because delta is not used before it is assigned to again. Perhaps delta should be declared inside the loop, when it is first assigned to.