Skip to content

Commit 2998e9a

Browse files
adonovangopherbot
authored andcommitted
go/analysis/passes/lostcancel: add WithCancelCause et al
The *Cause variants behave in a similar manner. Updates golang/go#70185 Change-Id: Ia7eea7a5be8878930505b63fa8222060fef47079 Reviewed-on: https://go-review.googlesource.com/c/tools/+/625115 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent f0379e0 commit 2998e9a

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

go/analysis/passes/lostcancel/doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// lostcancel: check cancel func returned by context.WithCancel is called
1111
//
1212
// The cancellation function returned by context.WithCancel, WithTimeout,
13-
// and WithDeadline must be called or the new context will remain live
14-
// until its parent context is cancelled.
13+
// WithDeadline and variants such as WithCancelCause must be called,
14+
// or the new context will remain live until its parent context is cancelled.
1515
// (The background context is never cancelled.)
1616
package lostcancel

go/analysis/passes/lostcancel/lostcancel.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ func isContextWithCancel(info *types.Info, n ast.Node) bool {
198198
return false
199199
}
200200
switch sel.Sel.Name {
201-
case "WithCancel", "WithTimeout", "WithDeadline":
201+
case "WithCancel", "WithCancelCause",
202+
"WithTimeout", "WithTimeoutCause",
203+
"WithDeadline", "WithDeadlineCause":
202204
default:
203205
return false
204206
}

go/analysis/passes/lostcancel/testdata/src/typeparams/typeparams.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ package typeparams
88

99
import (
1010
"context"
11+
"io"
1112
"time"
1213
)
1314

15+
//
16+
// These comment lines are ballast to ensure
17+
// that this is L17. Add/remove as needed.
18+
1419
var bg = context.Background()
1520

1621
func _[T any]() {
1722
var _, cancel = context.WithCancel(bg) // want `the cancel function is not used on all paths \(possible context leak\)`
1823
if false {
1924
_ = cancel
2025
}
21-
} // want "this return statement may be reached without using the cancel var defined on line 17"
26+
} // want "this return statement may be reached without using the cancel var defined on line 22"
2227

2328
func _[T any]() {
2429
_, cancel := context.WithCancel(bg)
@@ -55,3 +60,16 @@ func _() {
5560
var x C[int]
5661
x.f()
5762
}
63+
64+
func withCancelCause(maybe bool) {
65+
{
66+
_, cancel := context.WithCancelCause(bg)
67+
defer cancel(io.EOF) // ok
68+
}
69+
{
70+
_, cancel := context.WithCancelCause(bg) // want "the cancel function is not used on all paths \\(possible context leak\\)"
71+
if maybe {
72+
cancel(io.EOF)
73+
}
74+
}
75+
} // want "this return statement may be reached without using the cancel var defined on line 70"

gopls/doc/analyzers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ Package documentation: [loopclosure](https://pkg.go.dev/golang.org/x/tools/go/an
428428

429429

430430
The cancellation function returned by context.WithCancel, WithTimeout,
431-
and WithDeadline must be called or the new context will remain live
432-
until its parent context is cancelled.
431+
WithDeadline and variants such as WithCancelCause must be called,
432+
or the new context will remain live until its parent context is cancelled.
433433
(The background context is never cancelled.)
434434

435435
Default: on.

gopls/internal/doc/api.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@
466466
},
467467
{
468468
"Name": "\"lostcancel\"",
469-
"Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nand WithDeadline must be called or the new context will remain live\nuntil its parent context is cancelled.\n(The background context is never cancelled.)",
469+
"Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nWithDeadline and variants such as WithCancelCause must be called,\nor the new context will remain live until its parent context is cancelled.\n(The background context is never cancelled.)",
470470
"Default": "true"
471471
},
472472
{
@@ -1114,7 +1114,7 @@
11141114
},
11151115
{
11161116
"Name": "lostcancel",
1117-
"Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nand WithDeadline must be called or the new context will remain live\nuntil its parent context is cancelled.\n(The background context is never cancelled.)",
1117+
"Doc": "check cancel func returned by context.WithCancel is called\n\nThe cancellation function returned by context.WithCancel, WithTimeout,\nWithDeadline and variants such as WithCancelCause must be called,\nor the new context will remain live until its parent context is cancelled.\n(The background context is never cancelled.)",
11181118
"URL": "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/lostcancel",
11191119
"Default": true
11201120
},

0 commit comments

Comments
 (0)