1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package nilness inspects the control-flow graph of an SSA function
// and reports errors such as nil pointer dereferences and degenerate
// nil pointer comparisons.
//
// # Analyzer nilness
//
// nilness: check for redundant or impossible nil comparisons
//
// The nilness checker inspects the control-flow graph of each function in
// a package and reports nil pointer dereferences, degenerate nil
// pointers, and panics with nil values. A degenerate comparison is of the form
// x==nil or x!=nil where x is statically known to be nil or non-nil. These are
// often a mistake, especially in control flow related to errors. Panics with nil
// values are checked because they are not detectable by
//
// if r := recover(); r != nil {
//
// This check reports conditions such as:
//
// if f == nil { // impossible condition (f is a function)
// }
//
// and:
//
// p := &v
// ...
// if p != nil { // tautological condition
// }
//
// and:
//
// if p == nil {
// print(*p) // nil dereference
// }
//
// and:
//
// if p == nil {
// panic(p)
// }
//
// Sometimes the control flow may be quite complex, making bugs hard
// to spot. In the example below, the err.Error expression is
// guaranteed to panic because, after the first return, err must be
// nil. The intervening loop is just a distraction.
//
// ...
// err := g.Wait()
// if err != nil {
// return err
// }
// partialSuccess := false
// for _, err := range errs {
// if err == nil {
// partialSuccess = true
// break
// }
// }
// if partialSuccess {
// reportStatus(StatusMessage{
// Code: code.ERROR,
// Detail: err.Error(), // "nil dereference in dynamic method call"
// })
// return nil
// }
//
// ...
package nilness
|