Skip to content

Add goptrcmp linter #4210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,7 @@ linters:
- gomoddirectives
- gomodguard
- goprintffuncname
- goptrcmp
- gosec
- gosimple
- gosmopolitan
Expand Down Expand Up @@ -2484,6 +2485,7 @@ linters:
- gomoddirectives
- gomodguard
- goprintffuncname
- goptrcmp
- gosec
- gosimple
- gosmopolitan
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ require (
github.com/ultraware/whitespace v0.1.0
github.com/uudashr/gocognit v1.1.2
github.com/valyala/quicktemplate v1.7.0
github.com/w1ck3dg0ph3r/goptrcmp v0.1.1
github.com/xen0n/gosmopolitan v1.2.2
github.com/yagipy/maintidx v1.0.0
github.com/yeya24/promlinter v0.2.0
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.

17 changes: 17 additions & 0 deletions pkg/golinters/goptrcmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package golinters

import (
"github.com/w1ck3dg0ph3r/goptrcmp"
"golang.org/x/tools/go/analysis"

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

func NewGoPtrCmp() *goanalysis.Linter {
return goanalysis.NewLinter(
"goptrcmp",
"Reports comparison between pointer values",
[]*analysis.Analyzer{goptrcmp.Analyzer()},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/jirfag/go-printf-func-name"),

linter.NewConfig(golinters.NewGoPtrCmp()).
WithLoadForGoAnalysis().
WithSince("v1.55.3").
WithPresets(linter.PresetBugs).
WithURL("https://github.com/w1ck3dg0ph3r/goptrcmp"),

linter.NewConfig(golinters.NewGosec(gosecCfg)).
WithSince("v1.0.0").
WithLoadForGoAnalysis().
Expand Down
48 changes: 48 additions & 0 deletions test/testdata/goptrcmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//golangcitest:args -Egoptrcmp
package testdata

import (
"fmt"
)

type Foo struct {
String string
Pointer *string
}

func CmpFoos(a, b *Foo) bool {
if a != b { // want "pointer comparison: a != b"
return false
}

if a == nil {
return false
}

if nil == b {
return false
}

if *a != *b {
return false
}

if a.String != b.String {
return false
}

if a.Pointer != b.Pointer { // want "pointer comparison: a.Pointer != b.Pointer"
return false
}

if *a.Pointer != *b.Pointer {
return false
}

fmt.Println(a == b) // want "pointer comparison: a == b"
fmt.Println(a.String == b.String)
fmt.Println(a.Pointer == b.Pointer) // want "pointer comparison: a.Pointer == b.Pointer"
fmt.Println(*a.Pointer == *b.Pointer)

return true
}