Incorrectly flags last switch case
Given the following toy.go:
package toy
import "math/rand"
func Foo() string {
switch rand.Intn(2) {
case 0:
return "zero"
case 1:
return "one"
}
panic("impossible")
}
and the following toy_test.go:
package toy
import (
"math/rand"
"testing"
)
func TestFoo_zero(t *testing.T) {
rand.Seed(0)
if s := Foo(); s != "zero" {
t.Fatalf(`%q != "zero"`, s)
}
}
func TestFoo_one(t *testing.T) {
rand.Seed(1)
if s := Foo(); s != "one" {
t.Fatalf(`%q != "one"`, s)
}
}
gobco reports:
Branch coverage: 3/4
toy.go:9:7: condition "rand.Intn(2) == 1" was once true but never false
although both cases are actually covered.
That's a tricky situation, and you need to read the message very carefully.
toy.go:9:7: condition "rand.Intn(2) == 1" was once true but never false
The "once true" refers to TestFoo_one. The "never false" says that the cases rand.Intn(2) == 0 and rand.Intn(2) == 1 are covered, but other values for rand.Intn(2) were never seen. This is not surprising since rand.Intn(2) is guaranteed to only ever return 0 or 1, never a higher number.
In other words, gobco is trying to tell you that you don't need the case 1 and could simply replace it with default:.
If you have an idea how to improve gobco's message to make it easier to understand, please tell me. Otherwise you can just close the issue since gobco is correct here.
Ah, right, thank you.
If you have an idea how to improve gobco's message to make it easier to understand, please tell me.
This would help:
toy.go:6:2: "switch rand.Intn(2)" never took the implicit default branch
I thought about the improved diagnostic again, and I'm not going to implement it.
- If the new message were to replace the current one, it wouldn't be beneficial to omit the text of the condition from the message.
- Adding a separate message for uncovered cases would generate two separate messages for the same underlying issue.
- The suggested message, although nicely worded, doesn't fit well into the set of existing messages, as all these messages pinpoint to a specific condition.