github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/errcheck/testdata/src/CheckErrcheck/CheckErrcheck.go (about)

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