Skip to content

fixes issue 290 #294

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

Merged
merged 1 commit into from
Jan 7, 2020
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
22 changes: 14 additions & 8 deletions fixtures/deep-exit.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
package fixtures

import (
"syscall"
"log"
"os"
"syscall"
"testing"
)

func foo0() {
os.Exit(1) // MATCH /calls to os.Exit only in main() or init() functions/
os.Exit(1) // MATCH /calls to os.Exit only in main() or init() functions/
}

func init() {
log.Fatal("v ...interface{}")
log.Fatal("v ...interface{}")
}

func foo() {
log.Fatalf(1) // MATCH /calls to log.Fatalf only in main() or init() functions/
log.Fatalf(1) // MATCH /calls to log.Fatalf only in main() or init() functions/
}

func main() {
log.Fatalln("v ...interface{}")
log.Fatalln("v ...interface{}")
}

func bar() {
log.Fatal(1) // MATCH /calls to log.Fatal only in main() or init() functions/
log.Fatal(1) // MATCH /calls to log.Fatal only in main() or init() functions/
}

func bar2() {
bar()
syscall.Exit(1) // MATCH /calls to syscall.Exit only in main() or init() functions/
bar()
syscall.Exit(1) // MATCH /calls to syscall.Exit only in main() or init() functions/
}

func TestMain(m *testing.M) {
// must match because this is not a test file
os.Exit(m.Run()) // MATCH /calls to os.Exit only in main() or init() functions/
}
11 changes: 11 additions & 0 deletions fixtures/deep-exit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fixtures

import (
"os"
"testing"
)

func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
os.Exit(m.Run())
}
18 changes: 9 additions & 9 deletions rule/deep-exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *DeepExitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
},
}

w := lintDeepExit{onFailure, exitFunctions, false}
w := lintDeepExit{onFailure, exitFunctions, file.IsTest()}
ast.Walk(w, file.AST)
return failures
}
Expand All @@ -43,16 +43,15 @@ func (r *DeepExitRule) Name() string {
type lintDeepExit struct {
onFailure func(lint.Failure)
exitFunctions map[string]map[string]bool
ignore bool
isTestFile bool
}

func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
if stmt, ok := node.(*ast.FuncDecl); ok {
w.updateIgnore(stmt)
return w
}
if fd, ok := node.(*ast.FuncDecl); ok {
if w.mustIgnore(fd) {
return nil // skip analysis of this function
}

if w.ignore {
return w
}

Expand Down Expand Up @@ -88,7 +87,8 @@ func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
return w
}

func (w *lintDeepExit) updateIgnore(fd *ast.FuncDecl) {
func (w *lintDeepExit) mustIgnore(fd *ast.FuncDecl) bool {
fn := fd.Name.Name
w.ignore = (fn == "init" || fn == "main")

return fn == "init" || fn == "main" || (w.isTestFile && fn == "TestMain")
}
1 change: 1 addition & 0 deletions test/deep-exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import (

func TestDeepExit(t *testing.T) {
testRule(t, "deep-exit", &rule.DeepExitRule{})
testRule(t, "deep-exit_test", &rule.DeepExitRule{})
}