Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tone down close closer #36

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions closecloser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func run(pass *analysis.Pass) (any, error) {
c := newCloseCollector(pass, ipr)

ipr.Nodes([]ast.Node{
&ast.FuncDecl{},
&ast.File{},
&ast.AssignStmt{},
&ast.ValueSpec{},
},
Expand All @@ -32,14 +32,8 @@ func run(pass *analysis.Pass) (any, error) {
}
var ids []*ast.Ident
switch n := n.(type) {
case *ast.FuncDecl:
// NOTE:
// As of now, the analyzer does not report test functions.
// It is because:
// - resources can be implicitly closed by t.Cleanup()
// - people tend to write tests that do not close resources
// I think it is not desirable but this is practically helpful anyway.
if isTestOrTestHelper(n, pass) {
case *ast.File:
if utils.IsTestRelatedFile(n) {
return false
}
case *ast.AssignStmt:
Expand Down
50 changes: 11 additions & 39 deletions closecloser/ignored.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func shouldAllowUnclosed(expr ast.Expr, pass *analysis.Pass) bool {
}

func isAllowedCall(call *ast.CallExpr, pass *analysis.Pass) bool {
if isAllowedMethodCall(call, pass) {
if isMethodCall(call, pass) {
// closer returned by method call is allowed.
// its lifecycle might be managed by the receiver.
return true
}
if isAllowedFuncCall(call, pass) {
Expand All @@ -29,40 +31,22 @@ func isAllowedCall(call *ast.CallExpr, pass *analysis.Pass) bool {
return false
}

func isAllowedMethodCall(call *ast.CallExpr, pass *analysis.Pass) bool {
func isMethodCall(call *ast.CallExpr, pass *analysis.Pass) bool {
fun, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return false
}
ty := pass.TypesInfo.TypeOf(fun.X)
if ty == nil {
return false
}

if implementsCloser(ty) {
return true
}

pt, ok := ty.(*types.Pointer)
if ok {
ty = pt.Elem()
}

nt, ok := ty.(*types.Named)
ident, ok := fun.X.(*ast.Ident)
if !ok {
return false
return true
}
obj := nt.Obj()
if obj.Pkg() == nil {
ty := pass.TypesInfo.ObjectOf(ident)
if ty == nil {
return false
}
m := method{
pkg: obj.Pkg().Path(),
typ: nt.Obj().Name(),
name: fun.Sel.Name,
}
_, ok = allowedMethods[m]
return ok

_, ok = ty.(*types.PkgName)
return !ok
}

func isAllowedFuncCall(call *ast.CallExpr, pass *analysis.Pass) bool {
Expand All @@ -86,18 +70,6 @@ func isAllowedFuncCall(call *ast.CallExpr, pass *analysis.Pass) bool {
return ok
}

var allowedMethods = map[method]struct{}{
{"os/exec", "Cmd", "StdinPipe"}: {},
{"os/exec", "Cmd", "StdoutPipe"}: {},
{"os/exec", "Cmd", "StderrPipe"}: {},
}

type method struct {
pkg string
typ string
name string
}

var allowedFuns = map[fun]struct{}{}

type fun struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testdata
package testdata_test

import (
"os"
Expand Down
46 changes: 0 additions & 46 deletions closecloser/testing.go

This file was deleted.

Loading