mirror of
https://github.com/mgechev/revive.git
synced 2024-11-21 17:16:40 +02:00
fix: nil panic on old go.mod (issue #1100)
This commit is contained in:
parent
a4d0c434e1
commit
c0d4d07ab6
@ -173,6 +173,10 @@ func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error)
|
||||
return "", nil, fmt.Errorf("failed to parse %q, got %v", modFileName, err)
|
||||
}
|
||||
|
||||
if modAst.Go == nil {
|
||||
return "", nil, fmt.Errorf("%q does not specify a Go version", modFileName)
|
||||
}
|
||||
|
||||
ver, err = goversion.NewVersion(modAst.Go.Version)
|
||||
return filepath.Dir(modFileName), ver, err
|
||||
}
|
||||
|
22
test/issue1100_test.go
Normal file
22
test/issue1100_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
"github.com/mgechev/revive/rule"
|
||||
)
|
||||
|
||||
func TestIssue1100(t *testing.T) {
|
||||
args := []any{map[string]any{
|
||||
"maxLitCount": "2",
|
||||
"allowStrs": "\"\"",
|
||||
"allowInts": "0,1,2",
|
||||
"allowFloats": "0.0,1.0",
|
||||
"ignoreFuncs": "os\\.(CreateFile|WriteFile|Chmod|FindProcess),\\.Println,ignoredFunc,\\.Info",
|
||||
}}
|
||||
|
||||
testRule(t, "goUnknown/issue1100", &rule.AddConstantRule{}, &lint.RuleConfig{
|
||||
Arguments: args,
|
||||
})
|
||||
}
|
2
testdata/goUnknown/go.mod
vendored
Normal file
2
testdata/goUnknown/go.mod
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
module github.com/mgechev/revive/testdata
|
||||
|
104
testdata/goUnknown/issue1100.go
vendored
Normal file
104
testdata/goUnknown/issue1100.go
vendored
Normal file
@ -0,0 +1,104 @@
|
||||
package fixtures
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type testLogger struct{}
|
||||
|
||||
func (l *testLogger) Info(ctx context.Context, msg string) {}
|
||||
|
||||
func getLogger() *testLogger {
|
||||
return &testLogger{}
|
||||
}
|
||||
|
||||
func test1007() {
|
||||
getLogger().Info(context.Background(), "test1007")
|
||||
getLogger().Info(context.Background(), "test1007")
|
||||
getLogger().Info(context.Background(), "test1007")
|
||||
}
|
||||
|
||||
func foo(a float32, b string, c any, d int) {
|
||||
a = 1.0 // ignore
|
||||
b = "ignore"
|
||||
c = 2 // ignore
|
||||
println("lit", 12) // MATCH /avoid magic numbers like '12', create a named constant for it/
|
||||
if a == 12.50 { // MATCH /avoid magic numbers like '12.50', create a named constant for it/
|
||||
if b == "lit" {
|
||||
c = "lit" // MATCH /string literal "lit" appears, at least, 3 times, create a named constant for it/
|
||||
}
|
||||
for i := 0; i < 1; i++ {
|
||||
println("lit")
|
||||
}
|
||||
}
|
||||
|
||||
println(0666) // MATCH /avoid magic numbers like '0666', create a named constant for it/
|
||||
os.Chmod("test", 0666) // ignore
|
||||
os.FindProcess(102100) // ignore
|
||||
fmt.Println("test", 12) // ignore
|
||||
fmt.Printf("%d", 100) // MATCH /avoid magic numbers like '100', create a named constant for it/
|
||||
myPrintln("%d", 100) // MATCH /avoid magic numbers like '100', create a named constant for it/
|
||||
ignoredFunc(1000) // ignore
|
||||
ignoredFunc1(1000) // ignore - match regexp too
|
||||
|
||||
println("The result of calling myFunc is: ", ignoredFunc(100)) // ignore
|
||||
println("result is: ", ignoredFunc(notIgnoredFunc(ignoredFunc(100)))) // ignore
|
||||
println("result of calling myFunc is: ", notIgnoredFunc(ignoredFunc(100))) // ignore
|
||||
|
||||
println("result myFunc is: ", notIgnoredFunc(100)) // MATCH /avoid magic numbers like '100', create a named constant for it/
|
||||
println("The result is: ", ignoredFunc(notIgnoredFunc(100))) // MATCH /avoid magic numbers like '100', create a named constant for it/
|
||||
}
|
||||
|
||||
func myPrintln(s string, num int) {
|
||||
|
||||
}
|
||||
|
||||
func ignoredFunc1(num int) int {
|
||||
return num
|
||||
}
|
||||
|
||||
func ignoredFunc(num int) int {
|
||||
return num
|
||||
}
|
||||
|
||||
func notIgnoredFunc(num int) int {
|
||||
return num
|
||||
}
|
||||
|
||||
func tagsInStructLiteralsShouldBeOK() {
|
||||
a := struct {
|
||||
X int `json:"x"`
|
||||
}{}
|
||||
|
||||
b := struct {
|
||||
X int `json:"x"`
|
||||
}{}
|
||||
|
||||
c := struct {
|
||||
X int `json:"x"`
|
||||
}{}
|
||||
|
||||
d := struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
}{}
|
||||
|
||||
e := struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
}{}
|
||||
|
||||
var f struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
}
|
||||
|
||||
var g struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
}
|
||||
|
||||
_, _, _, _, _, _, _ = a, b, c, d, e, f, g
|
||||
}
|
Loading…
Reference in New Issue
Block a user