### Install nilnil and run on current directory Source: https://github.com/antonboom/nilnil/blob/master/README.md Install the nilnil linter using go install and run it on the current directory and all subdirectories. ```bash $ go install github.com/Antonboom/nilnil@latest $ nilnil ./... ``` -------------------------------- ### CLI configuration examples for nilnil Source: https://github.com/antonboom/nilnil/blob/master/README.md Shows various CLI invocations to configure nilnil's checked types, detect opposite, and only-two options. ```shell $ nilnil --checked-types chan,func,iface,map,ptr,uintptr,unsafeptr ./... $ nilnil --detect-opposite ./... $ nilnil --detect-opposite --checked-types ptr ./.. $ nilnil --only-two=false ./... $ nilnil --detect-opposite --only-two=false --checked-types iface,map ./.. ``` -------------------------------- ### Not implemented - before and after Source: https://github.com/antonboom/nilnil/blob/master/README.md Shows a before and after example of fixing a nil, nil return in a StartStream method. The after version returns an error indicating the feature is not implemented. ```go // BEFORE func (s *Service) StartStream(ctx context.Context) (*Stream, error) { return nil, nil } // AFTER func (s *Service) StartStream(ctx context.Context) (*Stream, error) { return nil, errors.New("not implemented") } ``` -------------------------------- ### Rewrite ambiguous example with sentinel error Source: https://github.com/antonboom/nilnil/blob/master/README.md Shows how to rewrite the ambiguous example using a sentinel error to avoid the nil check and make the code more idiomatic. ```go user, err := getUser() if errors.Is(err, errUserNotFound) { // Do something and return. } if err != nil { return err } // Use user. ``` -------------------------------- ### parsePublicKey from crypto/tls - before and after Source: https://github.com/antonboom/nilnil/blob/master/README.md Shows a before and after example of fixing a nil, nil return in parsePublicKey from crypto/tls. The after version returns an error for unknown algorithms. ```go // BEFORE func parsePublicKey(algo PublicKeyAlgorithm, keyData *publicKeyInfo) (interface{}, error) { der := cryptobyte.String(keyData.PublicKey.RightAlign()) switch algo { case RSA: // ... return pub, nil case ECDSA: // ... return pub, nil case Ed25519: // ... return ed25519.PublicKey(der), nil case DSA: // ... return pub, nil default: return nil, nil } } // AFTER var errUnknownPublicKeyAlgo = errors.New("unknown public key algo") func parsePublicKey(algo PublicKeyAlgorithm, keyData *publicKeyInfo) (interface{}, error) { der := cryptobyte.String(keyData.PublicKey.RightAlign()) switch algo { case RSA: // ... return pub, nil case ECDSA: // ... return pub, nil case Ed25519: // ... return ed25519.PublicKey(der), nil case DSA: // ... return pub, nil default: return nil, fmt.Errorf("%w: %v", errUnknownPublicKeyAlgo, algo) } } ``` -------------------------------- ### nil-safe type example Source: https://github.com/antonboom/nilnil/blob/master/README.md Shows an example of a nil-safe type where returning nil, nil is acceptable but discouraged. The RateLimiter type handles nil receivers. ```go package ratelimiter type RateLimiter struct { // ... } func New() (*RateLimiter, error) { // It's OK, RateLimiter is nil-safe. // But it's better not to do it anyway. return nil, nil } func (r *RateLimiter) Allow() bool { if r == nil { return true } return r.allow() } ``` -------------------------------- ### http2clientConnReadLoop from net/http - before and after Source: https://github.com/antonboom/nilnil/blob/master/README.md Shows a before and after example of fixing a nil, nil return in http2clientConnReadLoop from net/http. The after version uses a sentinel error to signal skipping a frame. ```go // BEFORE // As a special case, handleResponse may return (nil, nil) to skip the frame. func (rl *http2clientConnReadLoop) handleResponse(/* ... */) (*Response, error) { if statusCode >= 100 && statusCode <= 199 { return nil, nil } } // ... res, err := rl.handleResponse(cs, f) if err != nil { return err } if res == nil { // (nil, nil) special case. See handleResponse docs. return nil } // AFTER var errNeedSkipFrame = errors.New("need skip frame") // As a special case, handleResponse may return errNeedSkipFrame to skip the frame. func (rl *http2clientConnReadLoop) handleResponse(/* ... */) (*Response, error) { if statusCode >= 100 && statusCode <= 199 { return nil, errNeedSkipFrame } } // ... res, err := rl.handleResponse(cs, f) if errors.Is(err, errNeedSkipFrame) { return nil } if err != nil { return err } ``` -------------------------------- ### Rewritten Kubernetes code with error handling Source: https://github.com/antonboom/nilnil/blob/master/README.md Shows the rewritten version of the Kubernetes example that checks the error and returns nil on failure, avoiding the anti-pattern. ```go func (fh *fakeHistory) UpdateControllerRevision(revision *apps.ControllerRevision, newRevision int64) (*apps.ControllerRevision, error) { clone := revision.DeepCopy() clone.Revision = newRevision if err := fh.indexer.Update(clone); err != nil { return nil, fmt.Errorf("update index: %w", err) } return clone, nil } ``` -------------------------------- ### Comment explaining nil, nil return in Go standard library Source: https://github.com/antonboom/nilnil/blob/master/README.md Example from the Go standard library where a comment explains that a function returns nil, nil when there is no line table, illustrating the need for such comments. ```go // ... // If this compilation unit has no line table, it returns nil, nil. func (d *Data) LineReader(cu *Entry) (*LineReader, error) { ``` -------------------------------- ### Kubernetes code returning non-nil error and valid value Source: https://github.com/antonboom/nilnil/blob/master/README.md Example from Kubernetes where a function returns a non-nil error and a valid value simultaneously, which can lead to hard-to-find bugs. ```go func (fh *fakeHistory) UpdateControllerRevision(revision *apps.ControllerRevision, newRevision int64) (*apps.ControllerRevision, error) { clone := revision.DeepCopy() clone.Revision = newRevision return clone, fh.indexer.Update(clone) } ``` -------------------------------- ### Run nilnil on Go 1.22.2 source code Source: https://github.com/antonboom/nilnil/blob/master/README.md Run nilnil on the Go source code, filtering out test files. The output shows the linter findings with file locations and the message 'return both a `nil` error and an invalid value: use a sentinel error instead'. ```shell $ cd $GOROOT/src $ nilnil ./... 2>&1 | grep -v "_test.go" /usr/local/go/src/internal/bisect/bisect.go:196:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/fd_unix.go:71:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/fd_unix.go:79:4: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/fd_unix.go:156:4: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/iprawsock_posix.go:36:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/tcpsock_posix.go:38:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/udpsock_posix.go:37:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/unixsock_posix.go:92:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/crypto/tls/key_agreement.go:46:2: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/crypto/tls/ticket.go:355:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/crypto/tls/ticket.go:359:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/driver/types.go:157:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/driver/types.go:232:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/driver/types.go:263:4: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/convert.go:548:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:205:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:231:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:257:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:284:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:311:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:337:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:363:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:389:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/database/sql/sql.go:422:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/debug/dwarf/entry.go:884:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/debug/dwarf/line.go:146:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/debug/dwarf/line.go:153:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/debug/dwarf/typeunit.go:138:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/debug/pe/file.go:470:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/http/h2_bundle.go:9530:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/http/transfer.go:765:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/http/transfer.go:775:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/net/http/transfer.go:798:3: return both a `nil` error and an invalid value: use a sentinel error instead /usr/local/go/src/go/build/build.go:1442:3: return both a `nil` error and an invalid value: use a sentinel error instead ``` -------------------------------- ### Ambiguous nil check after nil error return Source: https://github.com/antonboom/nilnil/blob/master/README.md Shows the problematic pattern where a function returns nil error and a nil value, leading to an ambiguous nil check on the returned value. ```go user, err := getUser() if err != nil { return err } if user != nil { // Ambiguous! // Use user. } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.