• Russ Cox's avatar
    cmd/cover: change covered block for switch/select case to exclude expression · a4a4d430
    Russ Cox authored
    Consider a switch like
    
    	switch x {
    	case foo:
    		f()
    		g()
    	}
    
    Before, the coverage annotation for the block calling f and g included
    in its position span the text for 'case foo:'. This looks nice in the coverage
    report, but it breaks the invariant that coverage blocks are disjoint if
    you have a more complex expression like:
    
    	switch x {
    	case func() int { return foo }():
    		f()
    		g()
    	}
    
    Then the coverage analysis wants to annotate the func literal body,
    which overlaps with the case body, because the case body is considered
    to begin at the case token.
    
    Change the annotation for a case body to start just after the colon of
    the case clause, avoiding any potential conflict with complex case
    expressions. Could have started at the colon instead, but it seemed
    less weird to start just after it.
    
    Fixes #16540.
    
    Change-Id: I1fec4bc2a53c7092e649dc0d4be1680a697cb79b
    Reviewed-on: https://go-review.googlesource.com/32612
    Run-TryBot: Russ Cox <rsc@golang.org>
    Reviewed-by: default avatarAndrew Gerrand <adg@golang.org>
    a4a4d430
test.go 4.82 KB