Commit 5e17ce22 authored by Alan Donovan's avatar Alan Donovan

cmd/vet: lostcancel: suppress the check in the main.main function

When main.main returns, the process exits, so there's no need to cancel contexts.

This change was initially reviewed as
https://go-review.googlesource.com/c/go/+/106915/4
but somehow I messed up and committed patchset 5, which was
effectively empty.

Change-Id: Ic4250eb6563af9bc734e429aafc7081ca7d0e012
Reviewed-on: https://go-review.googlesource.com/c/148758Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 7d6b5e34
...@@ -104,6 +104,11 @@ func checkLostCancel(f *File, node ast.Node) { ...@@ -104,6 +104,11 @@ func checkLostCancel(f *File, node ast.Node) {
var sig *types.Signature var sig *types.Signature
switch node := node.(type) { switch node := node.(type) {
case *ast.FuncDecl: case *ast.FuncDecl:
if node.Name.Name == "main" && node.Recv == nil && f.file.Name.Name == "main" {
// Returning from main.main terminates the process,
// so there's no need to cancel contexts.
return
}
obj := f.pkg.defs[node.Name] obj := f.pkg.defs[node.Name]
if obj == nil { if obj == nil {
return // type error (e.g. duplicate function declaration) return // type error (e.g. duplicate function declaration)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package testdata package main
import ( import (
"context" "context"
...@@ -33,6 +33,12 @@ func _() { ...@@ -33,6 +33,12 @@ func _() {
ctx, _ = context.WithDeadline() // ERROR "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak" ctx, _ = context.WithDeadline() // ERROR "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak"
} }
// Return from main is handled specially.
// Since the program exits, there's no need to call cancel.
func main() {
var ctx, cancel = context.WithCancel()
}
func _() { func _() {
ctx, cancel := context.WithCancel() ctx, cancel := context.WithCancel()
defer cancel() // ok defer cancel() // ok
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment