Skip to content

Commit c0d4d07

Browse files
centralabchavacava
centralab
authored andcommitted
fix: nil panic on old go.mod (issue #1100)
1 parent a4d0c43 commit c0d4d07

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

lint/linter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error)
173173
return "", nil, fmt.Errorf("failed to parse %q, got %v", modFileName, err)
174174
}
175175

176+
if modAst.Go == nil {
177+
return "", nil, fmt.Errorf("%q does not specify a Go version", modFileName)
178+
}
179+
176180
ver, err = goversion.NewVersion(modAst.Go.Version)
177181
return filepath.Dir(modFileName), ver, err
178182
}

test/issue1100_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mgechev/revive/lint"
7+
"github.com/mgechev/revive/rule"
8+
)
9+
10+
func TestIssue1100(t *testing.T) {
11+
args := []any{map[string]any{
12+
"maxLitCount": "2",
13+
"allowStrs": "\"\"",
14+
"allowInts": "0,1,2",
15+
"allowFloats": "0.0,1.0",
16+
"ignoreFuncs": "os\\.(CreateFile|WriteFile|Chmod|FindProcess),\\.Println,ignoredFunc,\\.Info",
17+
}}
18+
19+
testRule(t, "goUnknown/issue1100", &rule.AddConstantRule{}, &lint.RuleConfig{
20+
Arguments: args,
21+
})
22+
}

testdata/goUnknown/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module github.com/mgechev/revive/testdata
2+

testdata/goUnknown/issue1100.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package fixtures
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
)
8+
9+
type testLogger struct{}
10+
11+
func (l *testLogger) Info(ctx context.Context, msg string) {}
12+
13+
func getLogger() *testLogger {
14+
return &testLogger{}
15+
}
16+
17+
func test1007() {
18+
getLogger().Info(context.Background(), "test1007")
19+
getLogger().Info(context.Background(), "test1007")
20+
getLogger().Info(context.Background(), "test1007")
21+
}
22+
23+
func foo(a float32, b string, c any, d int) {
24+
a = 1.0 // ignore
25+
b = "ignore"
26+
c = 2 // ignore
27+
println("lit", 12) // MATCH /avoid magic numbers like '12', create a named constant for it/
28+
if a == 12.50 { // MATCH /avoid magic numbers like '12.50', create a named constant for it/
29+
if b == "lit" {
30+
c = "lit" // MATCH /string literal "lit" appears, at least, 3 times, create a named constant for it/
31+
}
32+
for i := 0; i < 1; i++ {
33+
println("lit")
34+
}
35+
}
36+
37+
println(0666) // MATCH /avoid magic numbers like '0666', create a named constant for it/
38+
os.Chmod("test", 0666) // ignore
39+
os.FindProcess(102100) // ignore
40+
fmt.Println("test", 12) // ignore
41+
fmt.Printf("%d", 100) // MATCH /avoid magic numbers like '100', create a named constant for it/
42+
myPrintln("%d", 100) // MATCH /avoid magic numbers like '100', create a named constant for it/
43+
ignoredFunc(1000) // ignore
44+
ignoredFunc1(1000) // ignore - match regexp too
45+
46+
println("The result of calling myFunc is: ", ignoredFunc(100)) // ignore
47+
println("result is: ", ignoredFunc(notIgnoredFunc(ignoredFunc(100)))) // ignore
48+
println("result of calling myFunc is: ", notIgnoredFunc(ignoredFunc(100))) // ignore
49+
50+
println("result myFunc is: ", notIgnoredFunc(100)) // MATCH /avoid magic numbers like '100', create a named constant for it/
51+
println("The result is: ", ignoredFunc(notIgnoredFunc(100))) // MATCH /avoid magic numbers like '100', create a named constant for it/
52+
}
53+
54+
func myPrintln(s string, num int) {
55+
56+
}
57+
58+
func ignoredFunc1(num int) int {
59+
return num
60+
}
61+
62+
func ignoredFunc(num int) int {
63+
return num
64+
}
65+
66+
func notIgnoredFunc(num int) int {
67+
return num
68+
}
69+
70+
func tagsInStructLiteralsShouldBeOK() {
71+
a := struct {
72+
X int `json:"x"`
73+
}{}
74+
75+
b := struct {
76+
X int `json:"x"`
77+
}{}
78+
79+
c := struct {
80+
X int `json:"x"`
81+
}{}
82+
83+
d := struct {
84+
X int `json:"x"`
85+
Y int `json:"y"`
86+
}{}
87+
88+
e := struct {
89+
X int `json:"x"`
90+
Y int `json:"y"`
91+
}{}
92+
93+
var f struct {
94+
X int `json:"x"`
95+
Y int `json:"y"`
96+
}
97+
98+
var g struct {
99+
X int `json:"x"`
100+
Y int `json:"y"`
101+
}
102+
103+
_, _, _, _, _, _, _ = a, b, c, d, e, f, g
104+
}

0 commit comments

Comments
 (0)