Skip to content

Commit b596aae

Browse files
committed
Add URL; use TypeOf
Signed-off-by: Oliver Eikemeier <[email protected]>
1 parent 3a6ba38 commit b596aae

File tree

12 files changed

+34
-47
lines changed

12 files changed

+34
-47
lines changed

.github/workflows/test.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ubuntu-24.04
1919
strategy:
2020
matrix:
21-
go: ["1.23", "1.22"]
21+
go: ["1.23.3", "1.22.9"]
2222
env:
2323
GOTOOLCHAIN: local
2424
steps:
@@ -28,11 +28,10 @@ jobs:
2828
uses: actions/setup-go@v5
2929
with:
3030
go-version: ${{ matrix.go }}
31-
check-latest: true
3231
- name: 🧸 golangci-lint
3332
uses: golangci/golangci-lint-action@v6
3433
with:
35-
version: v1.61.0
34+
version: v1.62.0
3635
- name: 🔨 Test
3736
run: |
3837
go get -C ./pkg/analyzer/testdata golang.org/x/exp/errors

.golangci.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ linters:
55
enable-all: true
66
disable:
77
# deprecated
8-
- execinquery
98
- exportloopref
10-
- gomnd
119
# disabled
1210
- depguard
1311
- dupl
@@ -52,3 +50,6 @@ issues:
5250
linters:
5351
- gocheckcompilerdirectives
5452
text: "go:debug"
53+
- linters:
54+
- govet
55+
text: '^shadow: declaration of "(ctx|err|ok)" shadows declaration at line \d+$'

go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module fillmore-labs.com/zerolint
22

3-
go 1.22.7
3+
go 1.22.9
44

5-
toolchain go1.23.1
5+
toolchain go1.23.3
66

7-
require golang.org/x/tools v0.25.0
7+
require golang.org/x/tools v0.27.0
88

99
require (
10-
golang.org/x/mod v0.21.0 // indirect
11-
golang.org/x/sync v0.8.0 // indirect
10+
golang.org/x/mod v0.22.0 // indirect
11+
golang.org/x/sync v0.9.0 // indirect
1212
)

go.sum

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
22
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3-
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
4-
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
5-
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
6-
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
7-
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
8-
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
3+
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
4+
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
5+
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
6+
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
7+
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
8+
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=

pkg/analyzer/analyzer.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ can often be avoided.`
3333
var Analyzer = &analysis.Analyzer{ //nolint:gochecknoglobals
3434
Name: Name,
3535
Doc: Doc,
36+
URL: "https://pkg.go.dev/fillmore-labs.com/zerolint/pkg/analyzer",
3637
Run: run,
3738
Requires: []*analysis.Analyzer{inspect.Analyzer},
3839
}

pkg/analyzer/testdata/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module go.test
22

33
go 1.22
44

5-
require golang.org/x/exp/errors v0.0.0-20240823005443-9b4947da3948
5+
require golang.org/x/exp/errors v0.0.0-20241108190413-2d47ceb2692f

pkg/analyzer/testdata/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
golang.org/x/exp/errors v0.0.0-20240823005443-9b4947da3948 h1:IE9ZGXGK0A3EhE/vajvqwjYT5JsrZfxtVwBflW/O2vU=
2-
golang.org/x/exp/errors v0.0.0-20240823005443-9b4947da3948/go.mod h1:YgqsNsAu4fTvlab/7uiYK9LJrCIzKg/NiZUIH1/ayqo=
1+
golang.org/x/exp/errors v0.0.0-20241108190413-2d47ceb2692f h1:c+fBisNL7gIMeDtpC2uEtpqRl/MBKfCtkvXIH+N1kBk=
2+
golang.org/x/exp/errors v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:3MJlzkbfWNENOrJtD7NDv3LJffbUe3TmEUCaeYRe+is=

pkg/visitor/call.go

+6-20
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
// visitCallValue checks for encoding/json#Decoder.Decode, json.Unmarshal, errors.Is and errors.As.
3030
func (v Visitor) visitCallBasic(x *ast.CallExpr) bool { //nolint:cyclop
31-
fun, ok := unwrap(x.Fun).(*ast.SelectorExpr)
31+
fun, ok := ast.Unparen(x.Fun).(*ast.SelectorExpr)
3232
if !ok {
3333
return true
3434
}
@@ -38,8 +38,8 @@ func (v Visitor) visitCallBasic(x *ast.CallExpr) bool { //nolint:cyclop
3838
return false // Do not report pointers in json.Decoder#Decode
3939
}
4040

41-
switch path, ok2 := v.pathOf(fun.X); {
42-
case !ok2:
41+
switch path, ok := v.pathOf(fun.X); {
42+
case !ok:
4343
return true
4444

4545
case path == "encoding/json" && fun.Sel.Name == "Unmarshal":
@@ -105,7 +105,7 @@ func (v Visitor) visitCast(t types.Type, x *ast.CallExpr) bool {
105105

106106
message := fmt.Sprintf("cast of nil to pointer to zero-size variable of type %q", elem)
107107
var fixes []analysis.SuggestedFix
108-
if s, ok2 := unwrap(x.Fun).(*ast.StarExpr); ok2 {
108+
if s, ok := ast.Unparen(x.Fun).(*ast.StarExpr); ok {
109109
fixes = v.makePure(x, s.X)
110110
}
111111

@@ -119,13 +119,13 @@ func (v Visitor) visitNew(x *ast.CallExpr) bool {
119119
if len(x.Args) != 1 {
120120
return true
121121
}
122-
fun, ok := unwrap(x.Fun).(*ast.Ident)
122+
fun, ok := ast.Unparen(x.Fun).(*ast.Ident)
123123
if !ok || fun.Name != "new" {
124124
return true
125125
}
126126

127127
arg := x.Args[0] // new(arg).
128-
argType := v.TypesInfo.Types[arg].Type
128+
argType := v.TypesInfo.TypeOf(arg)
129129
if !v.zeroSizedType(argType) {
130130
return true
131131
}
@@ -137,20 +137,6 @@ func (v Visitor) visitNew(x *ast.CallExpr) bool {
137137
return false
138138
}
139139

140-
// unwrap removes parentheses from an expression (x).
141-
func unwrap(e ast.Expr) ast.Expr {
142-
x := e
143-
for {
144-
p, ok := x.(*ast.ParenExpr)
145-
if !ok {
146-
break
147-
}
148-
x = p.X
149-
}
150-
151-
return x
152-
}
153-
154140
// makePure adds a suggested fix from (*T)(nil) or new(T) to T{}.
155141
func (v Visitor) makePure(n ast.Node, x ast.Expr) []analysis.SuggestedFix {
156142
var buf bytes.Buffer

pkg/visitor/func.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func (v Visitor) visitFunc(x *ast.FuncDecl) bool {
3131
}
3232

3333
recv := x.Recv.List[0]
34-
recvType := v.TypesInfo.Types[recv.Type].Type
34+
recvType := v.TypesInfo.TypeOf(recv.Type)
3535
elem, ok := v.zeroSizedTypePointer(recvType)
3636
if !ok { // Not a pointer receiver or no pointer to a zero-sized type.
3737
return true
3838
}
3939

4040
var fixes []analysis.SuggestedFix
41-
if s, ok2 := recv.Type.(*ast.StarExpr); ok2 {
41+
if s, ok := recv.Type.(*ast.StarExpr); ok {
4242
fixes = v.removeOp(s, s.X)
4343
}
4444

pkg/visitor/star.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// visitUnary checks expressions in form *x.
2626
func (v Visitor) visitStar(x *ast.StarExpr) bool {
2727
// *...
28-
t := v.TypesInfo.Types[x.X].Type
28+
t := v.TypesInfo.TypeOf(x.X)
2929
var message string
3030
if p, ok := t.Underlying().(*types.Pointer); ok {
3131
if !v.zeroSizedType(p.Elem()) {

pkg/visitor/unary.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (v Visitor) visitUnary(x *ast.UnaryExpr) bool {
2929
}
3030

3131
// &...
32-
t := v.TypesInfo.Types[x.X].Type
32+
t := v.TypesInfo.TypeOf(x.X)
3333
if !v.zeroSizedType(t) {
3434
return true
3535
}

pkg/visitor/visitor.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ func (v Run) visitFunc() ([]ast.Node, func(ast.Node, bool) bool) {
8181
}
8282

8383
// visitBasic is the main functions called by inspector.Nodes for basic analysis.
84-
func (v Visitor) visitBasic(n ast.Node, push bool) bool {
84+
func (v Visitor) visitBasic(x ast.Node, push bool) bool {
8585
if !push {
8686
return true
8787
}
8888

89-
switch x := n.(type) {
89+
switch x := x.(type) {
9090
case *ast.BinaryExpr:
9191
return v.visitBinary(x)
9292

@@ -105,12 +105,12 @@ func (v Visitor) visitBasic(n ast.Node, push bool) bool {
105105
}
106106

107107
// visit is the main functions called by inspector.Nodes for full analysis.
108-
func (v Visitor) visit(n ast.Node, push bool) bool {
108+
func (v Visitor) visit(x ast.Node, push bool) bool {
109109
if !push {
110110
return true
111111
}
112112

113-
switch x := n.(type) {
113+
switch x := x.(type) {
114114
case *ast.StarExpr:
115115
return v.visitStar(x)
116116

0 commit comments

Comments
 (0)