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  func caninlineMulti(x int) (int, int) { // ERROR "can inline caninlineMulti"
    19  	return x, x
    20  }
    21  
    22  var something int
    23  
    24  func caninlineNoRet(x int) { // ERROR "can inline caninlineNoRet"
    25  	something = x
    26  }
    27  
    28  func caninlineVariadic(x ...int) { // ERROR "can inline caninlineVariadic" "x does not escape"
    29  	something = x[0]
    30  }
    31  
    32  func receiver(f func()) { // ERROR "can inline receiver" "f does not escape"
    33  	f()
    34  }
    35  
    36  func argument() {} // ERROR "can inline argument"
    37  
    38  func test(b *testing.B, localsink, cond int) { // ERROR ".*"
    39  	for i := 0; i < b.N; i++ {
    40  		caninline(1) // ERROR "inlining call to caninline"
    41  	}
    42  	somethingptr := &something
    43  	for b.Loop() { // ERROR "inlining call to testing\.\(\*B\)\.Loop"
    44  		caninline(1)                 // ERROR "inlining call to caninline" "function result will be kept alive"
    45  		caninlineNoRet(1)            // ERROR "inlining call to caninlineNoRet" "function arg will be kept alive"
    46  		caninlineVariadic(1)         // ERROR "inlining call to caninlineVariadic" "function arg will be kept alive" ".* does not escape"
    47  		caninlineVariadic(localsink) // ERROR "inlining call to caninlineVariadic" "localsink will be kept alive" ".* does not escape"
    48  		localsink = caninline(1)     // ERROR "inlining call to caninline" "localsink will be kept alive"
    49  		localsink += 5               // ERROR "localsink will be kept alive"
    50  		localsink, cond = 1, 2       // ERROR "localsink will be kept alive" "cond will be kept alive"
    51  		*somethingptr = 1            // ERROR "dereference will be kept alive"
    52  		if cond > 0 {
    53  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    54  		}
    55  		switch cond {
    56  		case 2:
    57  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    58  		}
    59  		{
    60  			caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive"
    61  		}
    62  
    63  		_ = caninline(1) // ERROR "inlining call to caninline" ".*autotmp.* will be kept alive"
    64  
    65  		// An assign list stmt with a single rhs expression returning multiple values via a tuple.
    66  		_, _ = caninlineMulti(1) // ERROR "inlining call to caninlineMulti" ".*autotmp.* will be kept alive"
    67  		// An assign list stmt with multiple rhs expressions.
    68  		_, _ = caninline(1), caninline(2) // ERROR "inlining call to caninline" ".*autotmp.* will be kept alive"
    69  
    70  		receiver(argument) // ERROR inlining call to receiver" "function arg will be kept alive"
    71  	}
    72  }
    73  

View as plain text