Skip to content

Commit

Permalink
Add exptostd linter (#5259)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Dec 30, 2024
1 parent 2a06dcb commit aa0450c
Show file tree
Hide file tree
Showing 15 changed files with 671 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ linters:
- exhaustive
- exhaustruct
- exportloopref
- exptostd
- fatcontext
- forbidigo
- forcetypeassert
Expand Down Expand Up @@ -153,6 +154,7 @@ linters:
- exhaustive
- exhaustruct
- exportloopref
- exptostd
- fatcontext
- forbidigo
- forcetypeassert
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/kunwardeep/paralleltest v1.0.10
github.com/kyoh86/exportloopref v0.1.11
github.com/lasiar/canonicalheader v1.1.2
github.com/ldez/exptostd v0.3.0
github.com/ldez/gomoddirectives v0.6.0
github.com/ldez/grignotin v0.7.0
github.com/ldez/tagliatelle v0.7.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
"exhaustive",
"exhaustruct",
"exportloopref",
"exptostd",
"fatcontext",
"forbidigo",
"forcetypeassert",
Expand Down
19 changes: 19 additions & 0 deletions pkg/golinters/exptostd/exptostd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package exptostd

import (
"github.com/ldez/exptostd"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func New() *goanalysis.Linter {
a := exptostd.NewAnalyzer()

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
19 changes: 19 additions & 0 deletions pkg/golinters/exptostd/exptostd_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package exptostd

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}

func TestFix(t *testing.T) {
integration.RunFix(t)
}

func TestFixPathPrefix(t *testing.T) {
integration.RunFixPathPrefix(t)
}
85 changes: 85 additions & 0 deletions pkg/golinters/exptostd/testdata/exptostd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//golangcitest:args -Eexptostd
package testdata

import (
"fmt"

"golang.org/x/exp/maps" // want `Import statement 'golang.org/x/exp/maps' can be replaced by 'maps'`
"golang.org/x/exp/slices" // want `Import statement 'golang.org/x/exp/slices' can be replaced by 'slices'`
)

func _(m, a map[string]string) {
maps.Clone(m) // want `golang.org/x/exp/maps.Clone\(\) can be replaced by maps.Clone\(\)`

maps.Equal(m, a) // want `golang.org/x/exp/maps.Equal\(\) can be replaced by maps.Equal\(\)`

maps.EqualFunc(m, a, func(i, j string) bool { // want `golang.org/x/exp/maps.EqualFunc\(\) can be replaced by maps.EqualFunc\(\)`
return true
})

maps.Copy(m, a) // want `golang.org/x/exp/maps.Copy\(\) can be replaced by maps.Copy\(\)`

maps.DeleteFunc(m, func(_, _ string) bool { // want `golang.org/x/exp/maps.DeleteFunc\(\) can be replaced by maps.DeleteFunc\(\)`
return true
})

maps.Clear(m) // want `golang.org/x/exp/maps.Clear\(\) can be replaced by clear\(\)`

fmt.Println("Hello")
}

func _(a, b []string) {
slices.Equal(a, b)
slices.EqualFunc(a, b, func(_ string, _ string) bool {
return true
})
slices.Compare(a, b)
slices.CompareFunc(a, b, func(_ string, _ string) int {
return 0
})
slices.Index(a, "a")
slices.IndexFunc(a, func(_ string) bool {
return true
})
slices.Contains(a, "a")
slices.ContainsFunc(a, func(_ string) bool {
return true
})
slices.Insert(a, 0, "a", "b")
slices.Delete(a, 0, 1)
slices.DeleteFunc(a, func(_ string) bool {
return true
})
slices.Replace(a, 0, 1, "a")
slices.Clone(a)
slices.Compact(a)
slices.CompactFunc(a, func(_ string, _ string) bool {
return true
})
slices.Grow(a, 2)
slices.Clip(a)
slices.Reverse(a)
slices.Sort(a)
slices.SortFunc(a, func(_, _ string) int {
return 0
})
slices.SortStableFunc(a, func(_, _ string) int {
return 0
})
slices.IsSorted(a)
slices.IsSortedFunc(a, func(_, _ string) int {
return 0
})
slices.Min(a)
slices.MinFunc(a, func(_, _ string) int {
return 0
})
slices.Max(a)
slices.MaxFunc(a, func(_, _ string) int {
return 0
})
slices.BinarySearch(a, "a")
slices.BinarySearchFunc(a, b, func(_ string, _ []string) int {
return 0
})
}
102 changes: 102 additions & 0 deletions pkg/golinters/exptostd/testdata/exptostd_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//golangcitest:args -Eexptostd
package testdata

/*
#include <stdio.h>
#include <stdlib.h>
void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

import (
"fmt"
"unsafe"

"golang.org/x/exp/maps" // want `Import statement 'golang.org/x/exp/maps' can be replaced by 'maps'`
"golang.org/x/exp/slices" // want `Import statement 'golang.org/x/exp/slices' can be replaced by 'slices'`
)

func _() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
}

func _(m, a map[string]string) {
maps.Clone(m) // want `golang.org/x/exp/maps.Clone\(\) can be replaced by maps.Clone\(\)`

maps.Equal(m, a) // want `golang.org/x/exp/maps.Equal\(\) can be replaced by maps.Equal\(\)`

maps.EqualFunc(m, a, func(i, j string) bool { // want `golang.org/x/exp/maps.EqualFunc\(\) can be replaced by maps.EqualFunc\(\)`
return true
})

maps.Copy(m, a) // want `golang.org/x/exp/maps.Copy\(\) can be replaced by maps.Copy\(\)`

maps.DeleteFunc(m, func(_, _ string) bool { // want `golang.org/x/exp/maps.DeleteFunc\(\) can be replaced by maps.DeleteFunc\(\)`
return true
})

maps.Clear(m) // want `golang.org/x/exp/maps.Clear\(\) can be replaced by clear\(\)`

fmt.Println("Hello")
}

func _(a, b []string) {
slices.Equal(a, b)
slices.EqualFunc(a, b, func(_ string, _ string) bool {
return true
})
slices.Compare(a, b)
slices.CompareFunc(a, b, func(_ string, _ string) int {
return 0
})
slices.Index(a, "a")
slices.IndexFunc(a, func(_ string) bool {
return true
})
slices.Contains(a, "a")
slices.ContainsFunc(a, func(_ string) bool {
return true
})
slices.Insert(a, 0, "a", "b")
slices.Delete(a, 0, 1)
slices.DeleteFunc(a, func(_ string) bool {
return true
})
slices.Replace(a, 0, 1, "a")
slices.Clone(a)
slices.Compact(a)
slices.CompactFunc(a, func(_ string, _ string) bool {
return true
})
slices.Grow(a, 2)
slices.Clip(a)
slices.Reverse(a)
slices.Sort(a)
slices.SortFunc(a, func(_, _ string) int {
return 0
})
slices.SortStableFunc(a, func(_, _ string) int {
return 0
})
slices.IsSorted(a)
slices.IsSortedFunc(a, func(_, _ string) int {
return 0
})
slices.Min(a)
slices.MinFunc(a, func(_, _ string) int {
return 0
})
slices.Max(a)
slices.MaxFunc(a, func(_, _ string) int {
return 0
})
slices.BinarySearch(a, "a")
slices.BinarySearchFunc(a, b, func(_ string, _ []string) int {
return 0
})
}
91 changes: 91 additions & 0 deletions pkg/golinters/exptostd/testdata/exptostd_go123.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//go:build go1.23

//golangcitest:args -Eexptostd
package testdata

import (
"fmt"

"golang.org/x/exp/maps" // want `Import statement 'golang.org/x/exp/maps' can be replaced by 'maps'`
"golang.org/x/exp/slices" // want `Import statement 'golang.org/x/exp/slices' can be replaced by 'slices'`
)

func _(m, a map[string]string) {
maps.Clone(m) // want `golang.org/x/exp/maps.Clone\(\) can be replaced by maps.Clone\(\)`

maps.Keys(m) // want `golang.org/x/exp/maps.Keys\(\) can be replaced by slices.Collect\(maps.Keys\(\)\)`

maps.Values(m) // want `golang.org/x/exp/maps.Values\(\) can be replaced by slices.Collect\(maps.Values\(\)\)`

maps.Equal(m, a) // want `golang.org/x/exp/maps.Equal\(\) can be replaced by maps.Equal\(\)`

maps.EqualFunc(m, a, func(i, j string) bool { // want `golang.org/x/exp/maps.EqualFunc\(\) can be replaced by maps.EqualFunc\(\)`
return true
})

maps.Copy(m, a) // want `golang.org/x/exp/maps.Copy\(\) can be replaced by maps.Copy\(\)`

maps.DeleteFunc(m, func(_, _ string) bool { // want `golang.org/x/exp/maps.DeleteFunc\(\) can be replaced by maps.DeleteFunc\(\)`
return true
})

maps.Clear(m) // want `golang.org/x/exp/maps.Clear\(\) can be replaced by clear\(\)`

fmt.Println("Hello")
}

func _(a, b []string) {
slices.Equal(a, b)
slices.EqualFunc(a, b, func(_ string, _ string) bool {
return true
})
slices.Compare(a, b)
slices.CompareFunc(a, b, func(_ string, _ string) int {
return 0
})
slices.Index(a, "a")
slices.IndexFunc(a, func(_ string) bool {
return true
})
slices.Contains(a, "a")
slices.ContainsFunc(a, func(_ string) bool {
return true
})
slices.Insert(a, 0, "a", "b")
slices.Delete(a, 0, 1)
slices.DeleteFunc(a, func(_ string) bool {
return true
})
slices.Replace(a, 0, 1, "a")
slices.Clone(a)
slices.Compact(a)
slices.CompactFunc(a, func(_ string, _ string) bool {
return true
})
slices.Grow(a, 2)
slices.Clip(a)
slices.Reverse(a)
slices.Sort(a)
slices.SortFunc(a, func(_, _ string) int {
return 0
})
slices.SortStableFunc(a, func(_, _ string) int {
return 0
})
slices.IsSorted(a)
slices.IsSortedFunc(a, func(_, _ string) int {
return 0
})
slices.Min(a)
slices.MinFunc(a, func(_, _ string) int {
return 0
})
slices.Max(a)
slices.MaxFunc(a, func(_, _ string) int {
return 0
})
slices.BinarySearch(a, "a")
slices.BinarySearchFunc(a, b, func(_ string, _ []string) int {
return 0
})
}
Loading

0 comments on commit aa0450c

Please sign in to comment.