|
| 1 | +//golangcitest:args -Enakedefer |
| 2 | +//golangcitest:config_path testdata/configs/nakedefer.yml |
| 3 | +package testdata |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "compress/zlib" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "os" |
| 14 | +) |
| 15 | + |
| 16 | +func funcNotReturnAnyType() { |
| 17 | +} |
| 18 | + |
| 19 | +func funcReturnErr() error { |
| 20 | + return errors.New("some error") |
| 21 | +} |
| 22 | + |
| 23 | +func funcReturnFuncAndErr() (func(), error) { |
| 24 | + return func() { |
| 25 | + }, nil |
| 26 | +} |
| 27 | + |
| 28 | +func ignoreFunc() error { |
| 29 | + return errors.New("some error") |
| 30 | +} |
| 31 | + |
| 32 | +func testCaseValid1() { |
| 33 | + defer funcNotReturnAnyType() // ignore |
| 34 | + |
| 35 | + defer func() { //ignore |
| 36 | + funcNotReturnAnyType() |
| 37 | + }() |
| 38 | + |
| 39 | + defer func() { //ignore |
| 40 | + _ = funcReturnErr() |
| 41 | + }() |
| 42 | +} |
| 43 | + |
| 44 | +func testCaseInvalid1() { |
| 45 | + defer funcReturnErr() // want "deferred call should not return anything" |
| 46 | + |
| 47 | + defer funcReturnFuncAndErr() // want "deferred call should not return anything" |
| 48 | + |
| 49 | + defer func() error { // want "deferred call should not return anything" |
| 50 | + return nil |
| 51 | + }() |
| 52 | + |
| 53 | + defer func() func() { // want "deferred call should not return anything" |
| 54 | + return func() {} |
| 55 | + }() |
| 56 | +} |
| 57 | + |
| 58 | +func testCase1() { |
| 59 | + defer fmt.Errorf("some text") // want "deferred call should not return anything" |
| 60 | + |
| 61 | + r := new(bytes.Buffer) |
| 62 | + defer io.LimitReader(r, 1) // want "deferred call should not return anything" |
| 63 | + |
| 64 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 65 | + w.WriteHeader(http.StatusOK) |
| 66 | + _, _ = w.Write([]byte("DONE")) |
| 67 | + })) |
| 68 | + defer srv.Close() //ignore |
| 69 | + defer srv.CloseClientConnections() //ignore |
| 70 | + defer srv.Certificate() // want "deferred call should not return anything" |
| 71 | +} |
| 72 | + |
| 73 | +func testCaseExclude1() { |
| 74 | + // exclude ignoreFunc |
| 75 | + defer ignoreFunc() // ignore |
| 76 | +} |
| 77 | + |
| 78 | +func testCaseExclude2() { |
| 79 | + // exclude os\.(Create|WriteFile|Chmod) |
| 80 | + defer os.Create("file_test1") // ignore |
| 81 | + defer os.WriteFile("file_test2", []byte("data"), os.ModeAppend) // ignore |
| 82 | + defer os.Chmod("file_test3", os.ModeAppend) // ignore |
| 83 | + defer os.FindProcess(100500) // want "deferred call should not return anything" |
| 84 | +} |
| 85 | + |
| 86 | +func testCaseExclude3() { |
| 87 | + // exclude fmt\.Print.* |
| 88 | + defer fmt.Println("e1") // ignore |
| 89 | + defer fmt.Print("e1") // ignore |
| 90 | + defer fmt.Printf("e1") // ignore |
| 91 | + defer fmt.Sprintf("some text") // want "deferred call should not return anything" |
| 92 | +} |
| 93 | + |
| 94 | +func testCaseExclude4() { |
| 95 | + // exclude io\.Close |
| 96 | + rc, _ := zlib.NewReader(bytes.NewReader([]byte("111"))) |
| 97 | + defer rc.Close() // ignore |
| 98 | +} |
0 commit comments