github.com/inturn/pre-commit-gobuild@v1.0.12/internal/errchecker/testdata/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/sha256"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"math/rand"
     9  	mrand "math/rand"
    10  )
    11  
    12  func a() error {
    13  	fmt.Println("this function returns an error") // EXCLUDED
    14  	return nil
    15  }
    16  
    17  func b() (int, error) {
    18  	fmt.Println("this function returns an int and an error") // EXCLUDED
    19  	return 0, nil
    20  }
    21  
    22  func c() int {
    23  	fmt.Println("this function returns an int") // EXCLUDED
    24  	return 7
    25  }
    26  
    27  func rec() {
    28  	defer func() {
    29  		recover()     // UNCHECKED
    30  		_ = recover() // BLANK
    31  	}()
    32  	defer recover() // UNCHECKED
    33  }
    34  
    35  type MyError string
    36  
    37  func (e MyError) Error() string {
    38  	return string(e)
    39  }
    40  
    41  func customError() error {
    42  	return MyError("an error occurred")
    43  }
    44  
    45  func customConcreteError() MyError {
    46  	return MyError("an error occurred")
    47  }
    48  
    49  func customConcreteErrorTuple() (int, MyError) {
    50  	return 0, MyError("an error occurred")
    51  }
    52  
    53  type MyPointerError string
    54  
    55  func (e *MyPointerError) Error() string {
    56  	return string(*e)
    57  }
    58  
    59  func customPointerError() *MyPointerError {
    60  	e := MyPointerError("an error occurred")
    61  	return &e
    62  }
    63  
    64  func customPointerErrorTuple() (int, *MyPointerError) {
    65  	e := MyPointerError("an error occurred")
    66  	return 0, &e
    67  }
    68  
    69  // Test custom excludes
    70  type ErrorMakerInterface interface {
    71  	MakeNilError() error
    72  }
    73  type ErrorMakerInterfaceWrapper interface {
    74  	ErrorMakerInterface
    75  }
    76  
    77  func main() {
    78  	// Single error return
    79  	_ = a() // BLANK
    80  	a()     // UNCHECKED
    81  
    82  	// Return another value and an error
    83  	_, _ = b() // BLANK
    84  	b()        // UNCHECKED
    85  
    86  	// Return a custom error type
    87  	_ = customError() // BLANK
    88  	customError()     // UNCHECKED
    89  
    90  	// Return a custom concrete error type
    91  	_ = customConcreteError()         // BLANK
    92  	customConcreteError()             // UNCHECKED
    93  	_, _ = customConcreteErrorTuple() // BLANK
    94  	customConcreteErrorTuple()        // UNCHECKED
    95  
    96  	// Return a custom pointer error type
    97  	_ = customPointerError()         // BLANK
    98  	customPointerError()             // UNCHECKED
    99  	_, _ = customPointerErrorTuple() // BLANK
   100  	customPointerErrorTuple()        // UNCHECKED
   101  
   102  	// Method with a single error return
   103  	x := t{}
   104  	_ = x.a() // BLANK
   105  	x.a()     // UNCHECKED
   106  
   107  	// Method call on a struct member
   108  	y := u{x}
   109  	_ = y.t.a() // BLANK
   110  	y.t.a()     // UNCHECKED
   111  
   112  	m1 := map[string]func() error{"a": a}
   113  	_ = m1["a"]() // BLANK
   114  	m1["a"]()     // UNCHECKED
   115  
   116  	// Additional cases for assigning errors to blank identifier
   117  	z, _ := b()    // BLANK
   118  	_, w := a(), 5 // BLANK
   119  
   120  	// Assign non error to blank identifier
   121  	_ = c()
   122  
   123  	_ = z + w // Avoid complaints about unused variables
   124  
   125  	// Type assertions
   126  	var i interface{}
   127  	s1 := i.(string)    // ASSERT
   128  	s1 = i.(string)     // ASSERT
   129  	s2, _ := i.(string) // ASSERT
   130  	s2, _ = i.(string)  // ASSERT
   131  	s3, ok := i.(string)
   132  	s3, ok = i.(string)
   133  	switch s4 := i.(type) {
   134  	case string:
   135  		_ = s4
   136  	}
   137  	_, _, _, _ = s1, s2, s3, ok
   138  
   139  	// Goroutine
   140  	go a()    // UNCHECKED
   141  	defer a() // UNCHECKED
   142  
   143  	b1 := bytes.Buffer{}
   144  	b2 := &bytes.Buffer{}
   145  	b1.Write(nil)
   146  	b2.Write(nil)
   147  	rand.Read(nil)
   148  	mrand.Read(nil)
   149  	sha256.New().Write([]byte{})
   150  
   151  	ioutil.ReadFile("main.go") // UNCHECKED
   152  
   153  	var emiw ErrorMakerInterfaceWrapper
   154  	emiw.MakeNilError()
   155  }