Skip to content

Commit 11b4b5e

Browse files
adonovangopherbot
authored andcommitted
go/analysis/passes/nilness: add longer example to doc
Fixes Google Issue 331478795 Change-Id: I0258bdd6a50000effc66c4e1fe03f99e2720c0cc Reviewed-on: https://go-review.googlesource.com/c/tools/+/575056 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 9ed98fa commit 11b4b5e

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

go/analysis/passes/nilness/doc.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,31 @@
4242
// if p == nil {
4343
// panic(p)
4444
// }
45+
//
46+
// Sometimes the control flow may be quite complex, making bugs hard
47+
// to spot. In the example below, the err.Error expression is
48+
// guaranteed to panic because, after the first return, err must be
49+
// nil. The intervening loop is just a distraction.
50+
//
51+
// ...
52+
// err := g.Wait()
53+
// if err != nil {
54+
// return err
55+
// }
56+
// partialSuccess := false
57+
// for _, err := range errs {
58+
// if err == nil {
59+
// partialSuccess = true
60+
// break
61+
// }
62+
// }
63+
// if partialSuccess {
64+
// reportStatus(StatusMessage{
65+
// Code: code.ERROR,
66+
// Detail: err.Error(), // "nil dereference in dynamic method call"
67+
// })
68+
// return nil
69+
// }
70+
//
71+
// ...
4572
package nilness

gopls/doc/analyzers.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,33 @@ and:
479479
panic(p)
480480
}
481481

482+
Sometimes the control flow may be quite complex, making bugs hard
483+
to spot. In the example below, the err.Error expression is
484+
guaranteed to panic because, after the first return, err must be
485+
nil. The intervening loop is just a distraction.
486+
487+
...
488+
err := g.Wait()
489+
if err != nil {
490+
return err
491+
}
492+
partialSuccess := false
493+
for _, err := range errs {
494+
if err == nil {
495+
partialSuccess = true
496+
break
497+
}
498+
}
499+
if partialSuccess {
500+
reportStatus(StatusMessage{
501+
Code: code.ERROR,
502+
Detail: err.Error(), // "nil dereference in dynamic method call"
503+
})
504+
return nil
505+
}
506+
507+
...
508+
482509
[Full documentation](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/nilness)
483510

484511
**Enabled by default.**

gopls/internal/settings/api_json.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)