mdempsky / unconvert

Remove unnecessary type conversions from Go source
BSD 3-Clause "New" or "Revised" License
377 stars 26 forks source link

False positive with bit shifting #33

Closed schoentoon closed 5 years ago

schoentoon commented 6 years ago

Whenever I cast an int to a uint to use it to shift bits it'll give me a false positive. As an example I used the following code.

package main

import (
    "time"
    "fmt"
)

func main() {
    test := []string{"1", "2", "3"}
    for i, s := range test {
        time.Sleep(time.Duration(1 << uint(i) * time.Second))
        fmt.Printf("%d %s\n", (1 << uint(i)), s)
    }
}

unconvert will simply tell me that this conversion is useless .../unconvert/unconvert.go:11:27: unnecessary conversion While if I remove the uint() around the variable the compiler refuses to compile it .../unconvert/unconvert.go:12:33: invalid operation: shift count i (variable of type int) must be unsigned integer

mdempsky commented 5 years ago

It's warning that the time.Duration conversion is unnecessary. Your time.Sleep call is identical to:

time.Sleep(1 << uint(1) * time.Second)