mirror of
https://github.com/mgechev/revive.git
synced 2025-04-21 11:56:55 +02:00
fixes issue 290 (#294)
This commit is contained in:
parent
757182a78d
commit
2bec93f05f
@ -1,32 +1,38 @@
|
|||||||
package fixtures
|
package fixtures
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"syscall"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func foo0() {
|
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() {
|
func init() {
|
||||||
log.Fatal("v ...interface{}")
|
log.Fatal("v ...interface{}")
|
||||||
}
|
}
|
||||||
|
|
||||||
func foo() {
|
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() {
|
func main() {
|
||||||
log.Fatalln("v ...interface{}")
|
log.Fatalln("v ...interface{}")
|
||||||
}
|
}
|
||||||
|
|
||||||
func bar() {
|
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() {
|
func bar2() {
|
||||||
bar()
|
bar()
|
||||||
syscall.Exit(1) // MATCH /calls to syscall.Exit only in main() or init() functions/
|
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
fixtures/deep-exit_test.go
Normal file
11
fixtures/deep-exit_test.go
Normal file
@ -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())
|
||||||
|
}
|
@ -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)
|
ast.Walk(w, file.AST)
|
||||||
return failures
|
return failures
|
||||||
}
|
}
|
||||||
@ -43,16 +43,15 @@ func (r *DeepExitRule) Name() string {
|
|||||||
type lintDeepExit struct {
|
type lintDeepExit struct {
|
||||||
onFailure func(lint.Failure)
|
onFailure func(lint.Failure)
|
||||||
exitFunctions map[string]map[string]bool
|
exitFunctions map[string]map[string]bool
|
||||||
ignore bool
|
isTestFile bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
|
func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
|
||||||
if stmt, ok := node.(*ast.FuncDecl); ok {
|
if fd, ok := node.(*ast.FuncDecl); ok {
|
||||||
w.updateIgnore(stmt)
|
if w.mustIgnore(fd) {
|
||||||
return w
|
return nil // skip analysis of this function
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.ignore {
|
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +87,8 @@ func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
|
|||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *lintDeepExit) updateIgnore(fd *ast.FuncDecl) {
|
func (w *lintDeepExit) mustIgnore(fd *ast.FuncDecl) bool {
|
||||||
fn := fd.Name.Name
|
fn := fd.Name.Name
|
||||||
w.ignore = (fn == "init" || fn == "main")
|
|
||||||
|
return fn == "init" || fn == "main" || (w.isTestFile && fn == "TestMain")
|
||||||
}
|
}
|
||||||
|
@ -8,4 +8,5 @@ import (
|
|||||||
|
|
||||||
func TestDeepExit(t *testing.T) {
|
func TestDeepExit(t *testing.T) {
|
||||||
testRule(t, "deep-exit", &rule.DeepExitRule{})
|
testRule(t, "deep-exit", &rule.DeepExitRule{})
|
||||||
|
testRule(t, "deep-exit_test", &rule.DeepExitRule{})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user