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 receiver(f func()) { // ERROR "can inline receiver" "f does not escape"
    29  	f()
    30  }
    31  
    32  func argument() {} // ERROR "can inline argument"
    33  
    34  func test(b *testing.B, localsink, cond int) { // ERROR ".*"
    35  	for i := 0; i < b.N; i++ {
    36  		caninline(1) // ERROR "inlining call to caninline"
    37  	}
    38  	somethingptr := &something
    39  	for b.Loop() { // ERROR "inlining call to testing\.\(\*B\)\.Loop"
    40  		caninline(1)                 // ERROR "inlining call to caninline" "function result will be kept alive"
    41  		caninlineNoRet(1)            // ERROR "inlining call to caninlineNoRet" "function arg will be kept alive"
    42  		caninlineVariadic(1)         // ERROR "inlining call to caninlineVariadic" "function arg will be kept alive" ".* does not escape"
    43  		caninlineVariadic(localsink) // ERROR "inlining call to caninlineVariadic" "localsink will be kept alive" ".* does not escape"
    44  		localsink = caninline(1)     // ERROR "inlining call to caninline" "localsink will be kept alive"
    45  		localsink += 5               // ERROR "localsink will be kept alive"
    46  		localsink, cond = 1, 2       // ERROR "localsink will be kept alive" "cond will be kept alive"
    47  		*somethingptr = 1            // ERROR "dereference will be kept alive"
    48  		if cond > 0 {
    49  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    50  		}
    51  		switch cond {
    52  		case 2:
    53  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    54  		}
    55  		{
    56  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    57  		}
    58  
    59  		receiver(argument) // ERROR inlining call to receiver" "function arg will be kept alive"
    60  	}
    61  }
    62  

View as plain text