github.com/nozzle/golangci-lint@v1.49.0-nz3/test/testdata/errorlint_comparison.go (about) 1 //golangcitest:args -Eerrorlint 2 //golangcitest:config_path testdata/configs/errorlint_comparison.yml 3 package testdata 4 5 import ( 6 "errors" 7 "log" 8 ) 9 10 var errCompare = errors.New("foo") 11 12 func errorLintDoThing() error { 13 return errCompare 14 } 15 16 func errorLintComparison() { 17 err := errorLintDoThing() 18 if errors.Is(err, errCompare) { 19 log.Println("errCompare") 20 } 21 if err == nil { 22 log.Println("nil") 23 } 24 if err != nil { 25 log.Println("nil") 26 } 27 if nil == err { 28 log.Println("nil") 29 } 30 if nil != err { 31 log.Println("nil") 32 } 33 if err == errCompare { // want "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error" 34 log.Println("errCompare") 35 } 36 if err != errCompare { // want "comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error" 37 log.Println("not errCompare") 38 } 39 if errCompare == err { // want "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error" 40 log.Println("errCompare") 41 } 42 if errCompare != err { // want "comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error" 43 log.Println("not errCompare") 44 } 45 switch err { // want "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors" 46 case errCompare: 47 log.Println("errCompare") 48 } 49 switch errorLintDoThing() { // want "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors" 50 case errCompare: 51 log.Println("errCompare") 52 } 53 }