github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/cmd/vet/testdata/lostcancel.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package testdata
     6  
     7  import (
     8  	"context"
     9  	"log"
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  // Check the three functions and assignment forms (var, :=, =) we look for.
    15  // (Do these early: line numbers are fragile.)
    16  func _() {
    17  	var ctx, cancel = context.WithCancel() // ERROR "the cancel function is not used on all paths \(possible context leak\)"
    18  } // ERROR "this return statement may be reached without using the cancel var defined on line 17"
    19  
    20  func _() {
    21  	ctx, cancel2 := context.WithDeadline() // ERROR "the cancel2 function is not used..."
    22  } // ERROR "may be reached without using the cancel2 var defined on line 21"
    23  
    24  func _() {
    25  	var ctx context.Context
    26  	var cancel3 func()
    27  	ctx, cancel3 = context.WithTimeout() // ERROR "function is not used..."
    28  } // ERROR "this return statement may be reached without using the cancel3 var defined on line 27"
    29  
    30  func _() {
    31  	ctx, _ := context.WithCancel()  // ERROR "the cancel function returned by context.WithCancel should be called, not discarded, to avoid a context leak"
    32  	ctx, _ = context.WithTimeout()  // ERROR "the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak"
    33  	ctx, _ = context.WithDeadline() // ERROR "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak"
    34  }
    35  
    36  func _() {
    37  	ctx, cancel := context.WithCancel()
    38  	defer cancel() // ok
    39  }
    40  
    41  func _() {
    42  	ctx, cancel := context.WithCancel() // ERROR "not used on all paths"
    43  	if condition {
    44  		cancel()
    45  	}
    46  	return // ERROR "this return statement may be reached without using the cancel var"
    47  }
    48  
    49  func _() {
    50  	ctx, cancel := context.WithCancel()
    51  	if condition {
    52  		cancel()
    53  	} else {
    54  		// ok: infinite loop
    55  		for {
    56  			print(0)
    57  		}
    58  	}
    59  }
    60  
    61  func _() {
    62  	ctx, cancel := context.WithCancel() // ERROR "not used on all paths"
    63  	if condition {
    64  		cancel()
    65  	} else {
    66  		for i := 0; i < 10; i++ {
    67  			print(0)
    68  		}
    69  	}
    70  } // ERROR "this return statement may be reached without using the cancel var"
    71  
    72  func _() {
    73  	ctx, cancel := context.WithCancel()
    74  	// ok: used on all paths
    75  	switch someInt {
    76  	case 0:
    77  		new(testing.T).FailNow()
    78  	case 1:
    79  		log.Fatal()
    80  	case 2:
    81  		cancel()
    82  	case 3:
    83  		print("hi")
    84  		fallthrough
    85  	default:
    86  		os.Exit(1)
    87  	}
    88  }
    89  
    90  func _() {
    91  	ctx, cancel := context.WithCancel() // ERROR "not used on all paths"
    92  	switch someInt {
    93  	case 0:
    94  		new(testing.T).FailNow()
    95  	case 1:
    96  		log.Fatal()
    97  	case 2:
    98  		cancel()
    99  	case 3:
   100  		print("hi") // falls through to implicit return
   101  	default:
   102  		os.Exit(1)
   103  	}
   104  } // ERROR "this return statement may be reached without using the cancel var"
   105  
   106  func _(ch chan int) int {
   107  	ctx, cancel := context.WithCancel() // ERROR "not used on all paths"
   108  	select {
   109  	case <-ch:
   110  		new(testing.T).FailNow()
   111  	case y <- ch:
   112  		print("hi") // falls through to implicit return
   113  	case ch <- 1:
   114  		cancel()
   115  	default:
   116  		os.Exit(1)
   117  	}
   118  } // ERROR "this return statement may be reached without using the cancel var"
   119  
   120  func _(ch chan int) int {
   121  	ctx, cancel := context.WithCancel()
   122  	// A blocking select must execute one of its cases.
   123  	select {
   124  	case <-ch:
   125  		panic()
   126  	}
   127  }
   128  
   129  func _() {
   130  	go func() {
   131  		ctx, cancel := context.WithCancel() // ERROR "not used on all paths"
   132  		print(ctx)
   133  	}() // ERROR "may be reached without using the cancel var"
   134  }
   135  
   136  var condition bool
   137  var someInt int
   138  
   139  // Regression test for Go issue 16143.
   140  func _() {
   141  	var x struct{ f func() }
   142  	x.f()
   143  }
   144  
   145  // Regression test for Go issue 16230.
   146  func _() (ctx context.Context, cancel func()) {
   147  	ctx, cancel = context.WithCancel()
   148  	return // a naked return counts as a load of the named result values
   149  }
   150  
   151  // Same as above, but for literal function.
   152  var _ = func() (ctx context.Context, cancel func()) {
   153  	ctx, cancel = context.WithCancel()
   154  	return
   155  }