Source file test/bloop.go

     1  // errorcheck -0 -m=2
     2  
     3  // Copyright 2025 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test keeping statements results in testing.B.Loop alive.
     8  // See issue #61515, #73137.
     9  
    10  package foo
    11  
    12  import "testing"
    13  
    14  func caninline(x int) int { // ERROR "can inline caninline"
    15  	return x
    16  }
    17  
    18  var something int
    19  
    20  func caninlineNoRet(x int) { // ERROR "can inline caninlineNoRet"
    21  	something = x
    22  }
    23  
    24  func caninlineVariadic(x ...int) { // ERROR "can inline caninlineVariadic" "x does not escape"
    25  	something = x[0]
    26  }
    27  
    28  func test(b *testing.B, localsink, cond int) { // ERROR ".*"
    29  	for i := 0; i < b.N; i++ {
    30  		caninline(1) // ERROR "inlining call to caninline"
    31  	}
    32  	somethingptr := &something
    33  	for b.Loop() { // ERROR "inlining call to testing\.\(\*B\)\.Loop"
    34  		caninline(1)                 // ERROR "inlining call to caninline" "function result will be kept alive"
    35  		caninlineNoRet(1)            // ERROR "inlining call to caninlineNoRet" "function arg will be kept alive"
    36  		caninlineVariadic(1)         // ERROR "inlining call to caninlineVariadic" "function arg will be kept alive" ".* does not escape"
    37  		caninlineVariadic(localsink) // ERROR "inlining call to caninlineVariadic" "localsink will be kept alive" ".* does not escape"
    38  		localsink = caninline(1)     // ERROR "inlining call to caninline" "localsink will be kept alive"
    39  		localsink += 5               // ERROR "localsink will be kept alive"
    40  		localsink, cond = 1, 2       // ERROR "localsink will be kept alive" "cond will be kept alive"
    41  		*somethingptr = 1            // ERROR "dereference will be kept alive"
    42  		if cond > 0 {
    43  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    44  		}
    45  		switch cond {
    46  		case 2:
    47  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    48  		}
    49  		{
    50  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    51  		}
    52  	}
    53  }
    54  

View as plain text