Open
Description
Summary
G115 is still reporting false positives even after #1189 and #1194.
Known false positives
I'm writing these down without much investigation whether it is possible to implement fixes for them.
- any kind of arithmetic done on the checked variable after the check:
func foo(a int) uint {
if a != 3 || a != 4 {
panic("not supported")
}
// the value being passed here is different than the one checked
// so it is tricky to determine if the check is valid
//
// this will be difficult to implement without introducing false negatives
return uint(a-1) // false positive
}
- explicit value checks are lacking:
func foo(a int) uint {
if a == 3 || a != 4 {
// see that sneaky NEQ there?
return uint(a) // false negative
}
panic("not supported")
}
- binary truncation (reported by @stephenc):
func foo(a int) uint16 {
return uint16(a &0xffff) // false positive
}
- builtin min and max functions (reported by @ben-krieger):
func foo() uint16 {
a, b := 1234, 2345
result := min(a,b)
return uint(result) // false positive
}
- loop indices (reported by @PlasmaPower):
func foo(myArr []string) {
// these seem to have a similar implementation difficulty to the arithmetic example
for i, _ := range myArr {
_ = uint64(i) // false positive
}
for i := 0; i < 10; i++ {
_ = uint64(i) // false positive
}
for i := randIntMightBeNegativeEven(); i >= 0; i-- {
_ = uint64(i) // false positive
}
}
Notes:
I recommend opening multiple small PRs that fix one issue at a time.