Skip to content

Commit aa0450c

Browse files
authored
Add exptostd linter (#5259)
1 parent 2a06dcb commit aa0450c

15 files changed

+671
-0
lines changed

.golangci.next.reference.yml

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ linters:
3535
- exhaustive
3636
- exhaustruct
3737
- exportloopref
38+
- exptostd
3839
- fatcontext
3940
- forbidigo
4041
- forcetypeassert
@@ -153,6 +154,7 @@ linters:
153154
- exhaustive
154155
- exhaustruct
155156
- exportloopref
157+
- exptostd
156158
- fatcontext
157159
- forbidigo
158160
- forcetypeassert

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ require (
6565
github.com/kunwardeep/paralleltest v1.0.10
6666
github.com/kyoh86/exportloopref v0.1.11
6767
github.com/lasiar/canonicalheader v1.1.2
68+
github.com/ldez/exptostd v0.3.0
6869
github.com/ldez/gomoddirectives v0.6.0
6970
github.com/ldez/grignotin v0.7.0
7071
github.com/ldez/tagliatelle v0.7.1

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

+1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@
350350
"exhaustive",
351351
"exhaustruct",
352352
"exportloopref",
353+
"exptostd",
353354
"fatcontext",
354355
"forbidigo",
355356
"forcetypeassert",

pkg/golinters/exptostd/exptostd.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package exptostd
2+
3+
import (
4+
"github.com/ldez/exptostd"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/goanalysis"
8+
)
9+
10+
func New() *goanalysis.Linter {
11+
a := exptostd.NewAnalyzer()
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package exptostd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
12+
13+
func TestFix(t *testing.T) {
14+
integration.RunFix(t)
15+
}
16+
17+
func TestFixPathPrefix(t *testing.T) {
18+
integration.RunFixPathPrefix(t)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//golangcitest:args -Eexptostd
2+
package testdata
3+
4+
import (
5+
"fmt"
6+
7+
"golang.org/x/exp/maps" // want `Import statement 'golang.org/x/exp/maps' can be replaced by 'maps'`
8+
"golang.org/x/exp/slices" // want `Import statement 'golang.org/x/exp/slices' can be replaced by 'slices'`
9+
)
10+
11+
func _(m, a map[string]string) {
12+
maps.Clone(m) // want `golang.org/x/exp/maps.Clone\(\) can be replaced by maps.Clone\(\)`
13+
14+
maps.Equal(m, a) // want `golang.org/x/exp/maps.Equal\(\) can be replaced by maps.Equal\(\)`
15+
16+
maps.EqualFunc(m, a, func(i, j string) bool { // want `golang.org/x/exp/maps.EqualFunc\(\) can be replaced by maps.EqualFunc\(\)`
17+
return true
18+
})
19+
20+
maps.Copy(m, a) // want `golang.org/x/exp/maps.Copy\(\) can be replaced by maps.Copy\(\)`
21+
22+
maps.DeleteFunc(m, func(_, _ string) bool { // want `golang.org/x/exp/maps.DeleteFunc\(\) can be replaced by maps.DeleteFunc\(\)`
23+
return true
24+
})
25+
26+
maps.Clear(m) // want `golang.org/x/exp/maps.Clear\(\) can be replaced by clear\(\)`
27+
28+
fmt.Println("Hello")
29+
}
30+
31+
func _(a, b []string) {
32+
slices.Equal(a, b)
33+
slices.EqualFunc(a, b, func(_ string, _ string) bool {
34+
return true
35+
})
36+
slices.Compare(a, b)
37+
slices.CompareFunc(a, b, func(_ string, _ string) int {
38+
return 0
39+
})
40+
slices.Index(a, "a")
41+
slices.IndexFunc(a, func(_ string) bool {
42+
return true
43+
})
44+
slices.Contains(a, "a")
45+
slices.ContainsFunc(a, func(_ string) bool {
46+
return true
47+
})
48+
slices.Insert(a, 0, "a", "b")
49+
slices.Delete(a, 0, 1)
50+
slices.DeleteFunc(a, func(_ string) bool {
51+
return true
52+
})
53+
slices.Replace(a, 0, 1, "a")
54+
slices.Clone(a)
55+
slices.Compact(a)
56+
slices.CompactFunc(a, func(_ string, _ string) bool {
57+
return true
58+
})
59+
slices.Grow(a, 2)
60+
slices.Clip(a)
61+
slices.Reverse(a)
62+
slices.Sort(a)
63+
slices.SortFunc(a, func(_, _ string) int {
64+
return 0
65+
})
66+
slices.SortStableFunc(a, func(_, _ string) int {
67+
return 0
68+
})
69+
slices.IsSorted(a)
70+
slices.IsSortedFunc(a, func(_, _ string) int {
71+
return 0
72+
})
73+
slices.Min(a)
74+
slices.MinFunc(a, func(_, _ string) int {
75+
return 0
76+
})
77+
slices.Max(a)
78+
slices.MaxFunc(a, func(_, _ string) int {
79+
return 0
80+
})
81+
slices.BinarySearch(a, "a")
82+
slices.BinarySearchFunc(a, b, func(_ string, _ []string) int {
83+
return 0
84+
})
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//golangcitest:args -Eexptostd
2+
package testdata
3+
4+
/*
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
void myprint(char* s) {
9+
printf("%d\n", s);
10+
}
11+
*/
12+
import "C"
13+
14+
import (
15+
"fmt"
16+
"unsafe"
17+
18+
"golang.org/x/exp/maps" // want `Import statement 'golang.org/x/exp/maps' can be replaced by 'maps'`
19+
"golang.org/x/exp/slices" // want `Import statement 'golang.org/x/exp/slices' can be replaced by 'slices'`
20+
)
21+
22+
func _() {
23+
cs := C.CString("Hello from stdio\n")
24+
C.myprint(cs)
25+
C.free(unsafe.Pointer(cs))
26+
}
27+
28+
func _(m, a map[string]string) {
29+
maps.Clone(m) // want `golang.org/x/exp/maps.Clone\(\) can be replaced by maps.Clone\(\)`
30+
31+
maps.Equal(m, a) // want `golang.org/x/exp/maps.Equal\(\) can be replaced by maps.Equal\(\)`
32+
33+
maps.EqualFunc(m, a, func(i, j string) bool { // want `golang.org/x/exp/maps.EqualFunc\(\) can be replaced by maps.EqualFunc\(\)`
34+
return true
35+
})
36+
37+
maps.Copy(m, a) // want `golang.org/x/exp/maps.Copy\(\) can be replaced by maps.Copy\(\)`
38+
39+
maps.DeleteFunc(m, func(_, _ string) bool { // want `golang.org/x/exp/maps.DeleteFunc\(\) can be replaced by maps.DeleteFunc\(\)`
40+
return true
41+
})
42+
43+
maps.Clear(m) // want `golang.org/x/exp/maps.Clear\(\) can be replaced by clear\(\)`
44+
45+
fmt.Println("Hello")
46+
}
47+
48+
func _(a, b []string) {
49+
slices.Equal(a, b)
50+
slices.EqualFunc(a, b, func(_ string, _ string) bool {
51+
return true
52+
})
53+
slices.Compare(a, b)
54+
slices.CompareFunc(a, b, func(_ string, _ string) int {
55+
return 0
56+
})
57+
slices.Index(a, "a")
58+
slices.IndexFunc(a, func(_ string) bool {
59+
return true
60+
})
61+
slices.Contains(a, "a")
62+
slices.ContainsFunc(a, func(_ string) bool {
63+
return true
64+
})
65+
slices.Insert(a, 0, "a", "b")
66+
slices.Delete(a, 0, 1)
67+
slices.DeleteFunc(a, func(_ string) bool {
68+
return true
69+
})
70+
slices.Replace(a, 0, 1, "a")
71+
slices.Clone(a)
72+
slices.Compact(a)
73+
slices.CompactFunc(a, func(_ string, _ string) bool {
74+
return true
75+
})
76+
slices.Grow(a, 2)
77+
slices.Clip(a)
78+
slices.Reverse(a)
79+
slices.Sort(a)
80+
slices.SortFunc(a, func(_, _ string) int {
81+
return 0
82+
})
83+
slices.SortStableFunc(a, func(_, _ string) int {
84+
return 0
85+
})
86+
slices.IsSorted(a)
87+
slices.IsSortedFunc(a, func(_, _ string) int {
88+
return 0
89+
})
90+
slices.Min(a)
91+
slices.MinFunc(a, func(_, _ string) int {
92+
return 0
93+
})
94+
slices.Max(a)
95+
slices.MaxFunc(a, func(_, _ string) int {
96+
return 0
97+
})
98+
slices.BinarySearch(a, "a")
99+
slices.BinarySearchFunc(a, b, func(_ string, _ []string) int {
100+
return 0
101+
})
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//go:build go1.23
2+
3+
//golangcitest:args -Eexptostd
4+
package testdata
5+
6+
import (
7+
"fmt"
8+
9+
"golang.org/x/exp/maps" // want `Import statement 'golang.org/x/exp/maps' can be replaced by 'maps'`
10+
"golang.org/x/exp/slices" // want `Import statement 'golang.org/x/exp/slices' can be replaced by 'slices'`
11+
)
12+
13+
func _(m, a map[string]string) {
14+
maps.Clone(m) // want `golang.org/x/exp/maps.Clone\(\) can be replaced by maps.Clone\(\)`
15+
16+
maps.Keys(m) // want `golang.org/x/exp/maps.Keys\(\) can be replaced by slices.Collect\(maps.Keys\(\)\)`
17+
18+
maps.Values(m) // want `golang.org/x/exp/maps.Values\(\) can be replaced by slices.Collect\(maps.Values\(\)\)`
19+
20+
maps.Equal(m, a) // want `golang.org/x/exp/maps.Equal\(\) can be replaced by maps.Equal\(\)`
21+
22+
maps.EqualFunc(m, a, func(i, j string) bool { // want `golang.org/x/exp/maps.EqualFunc\(\) can be replaced by maps.EqualFunc\(\)`
23+
return true
24+
})
25+
26+
maps.Copy(m, a) // want `golang.org/x/exp/maps.Copy\(\) can be replaced by maps.Copy\(\)`
27+
28+
maps.DeleteFunc(m, func(_, _ string) bool { // want `golang.org/x/exp/maps.DeleteFunc\(\) can be replaced by maps.DeleteFunc\(\)`
29+
return true
30+
})
31+
32+
maps.Clear(m) // want `golang.org/x/exp/maps.Clear\(\) can be replaced by clear\(\)`
33+
34+
fmt.Println("Hello")
35+
}
36+
37+
func _(a, b []string) {
38+
slices.Equal(a, b)
39+
slices.EqualFunc(a, b, func(_ string, _ string) bool {
40+
return true
41+
})
42+
slices.Compare(a, b)
43+
slices.CompareFunc(a, b, func(_ string, _ string) int {
44+
return 0
45+
})
46+
slices.Index(a, "a")
47+
slices.IndexFunc(a, func(_ string) bool {
48+
return true
49+
})
50+
slices.Contains(a, "a")
51+
slices.ContainsFunc(a, func(_ string) bool {
52+
return true
53+
})
54+
slices.Insert(a, 0, "a", "b")
55+
slices.Delete(a, 0, 1)
56+
slices.DeleteFunc(a, func(_ string) bool {
57+
return true
58+
})
59+
slices.Replace(a, 0, 1, "a")
60+
slices.Clone(a)
61+
slices.Compact(a)
62+
slices.CompactFunc(a, func(_ string, _ string) bool {
63+
return true
64+
})
65+
slices.Grow(a, 2)
66+
slices.Clip(a)
67+
slices.Reverse(a)
68+
slices.Sort(a)
69+
slices.SortFunc(a, func(_, _ string) int {
70+
return 0
71+
})
72+
slices.SortStableFunc(a, func(_, _ string) int {
73+
return 0
74+
})
75+
slices.IsSorted(a)
76+
slices.IsSortedFunc(a, func(_, _ string) int {
77+
return 0
78+
})
79+
slices.Min(a)
80+
slices.MinFunc(a, func(_, _ string) int {
81+
return 0
82+
})
83+
slices.Max(a)
84+
slices.MaxFunc(a, func(_, _ string) int {
85+
return 0
86+
})
87+
slices.BinarySearch(a, "a")
88+
slices.BinarySearchFunc(a, b, func(_ string, _ []string) int {
89+
return 0
90+
})
91+
}

0 commit comments

Comments
 (0)