github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/analysis/passes/ctrlflow/testdata/src/a/a.go (about)

     1  // Copyright 2021 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 a
     6  
     7  // This file tests facts produced by ctrlflow.
     8  
     9  import (
    10  	"log"
    11  	"os"
    12  	"runtime"
    13  	"syscall"
    14  	"testing"
    15  
    16  	"lib"
    17  )
    18  
    19  var cond bool
    20  
    21  func a() { // want a:"noReturn"
    22  	if cond {
    23  		b()
    24  	} else {
    25  		for {
    26  		}
    27  	}
    28  }
    29  
    30  func b() { // want b:"noReturn"
    31  	select {}
    32  }
    33  
    34  func f(x int) { // no fact here
    35  	switch x {
    36  	case 0:
    37  		os.Exit(0)
    38  	case 1:
    39  		panic(0)
    40  	}
    41  	// default case returns
    42  }
    43  
    44  type T int
    45  
    46  func (T) method1() { // want method1:"noReturn"
    47  	a()
    48  }
    49  
    50  func (T) method2() { // (may return)
    51  	if cond {
    52  		a()
    53  	}
    54  }
    55  
    56  // Checking for the noreturn fact associated with F ensures that
    57  // ctrlflow proved each of the listed functions was "noReturn".
    58  //
    59  func standardFunctions(x int) { // want standardFunctions:"noReturn"
    60  	t := new(testing.T)
    61  	switch x {
    62  	case 0:
    63  		t.FailNow()
    64  	case 1:
    65  		t.Fatal()
    66  	case 2:
    67  		t.Fatalf("")
    68  	case 3:
    69  		t.Skip()
    70  	case 4:
    71  		t.SkipNow()
    72  	case 5:
    73  		t.Skipf("")
    74  	case 6:
    75  		log.Fatal()
    76  	case 7:
    77  		log.Fatalf("")
    78  	case 8:
    79  		log.Fatalln()
    80  	case 9:
    81  		os.Exit(0)
    82  	case 10:
    83  		syscall.Exit(0)
    84  	case 11:
    85  		runtime.Goexit()
    86  	case 12:
    87  		log.Panic()
    88  	case 13:
    89  		log.Panicln()
    90  	case 14:
    91  		log.Panicf("")
    92  	default:
    93  		panic(0)
    94  	}
    95  }
    96  
    97  // False positives are possible.
    98  // This function is marked noReturn but in fact returns.
    99  //
   100  func spurious() { // want spurious:"noReturn"
   101  	defer func() { recover() }()
   102  	panic(nil)
   103  }
   104  
   105  func noBody()
   106  
   107  func g() {
   108  	lib.CanReturn()
   109  }
   110  
   111  func h() { // want h:"noReturn"
   112  	lib.NoReturn()
   113  }