Skip to content

Add ptrequality linter #5870

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/daixiang0/gci v0.13.6
github.com/denis-tingaikin/go-header v0.5.0
github.com/fatih/color v1.18.0
github.com/fillmore-labs/ptrequality v0.0.1
github.com/firefart/nonamedreturns v1.0.6
github.com/fzipp/gocyclo v0.6.0
github.com/ghostiam/protogetter v0.3.15
Expand Down Expand Up @@ -143,6 +144,7 @@ require (

require (
codeberg.org/chavacava/garif v0.2.0 // indirect
fillmore-labs.com/cmplint v0.0.3 // indirect
github.com/Masterminds/semver/v3 v3.3.1 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum

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

15 changes: 15 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@
"predeclared",
"promlinter",
"protogetter",
"ptrequality",
"reassign",
"recvcheck",
"revive",
Expand Down Expand Up @@ -2881,6 +2882,17 @@
}
}
},
"ptrequalitySettings": {
"type": "object",
"additionalProperties": false,
"properties": {
"check-is": {
"description": "Suppress check for errors with \"Is\" method",
"type": "boolean",
"default": true
}
}
},
"reviveSettings": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -4530,6 +4542,9 @@
"protogetter": {
"$ref": "#/definitions/settings/definitions/protogetterSettings"
},
"ptrequality": {
"$ref": "#/definitions/settings/definitions/ptrequalitySettings"
},
"revive": {
"$ref": "#/definitions/settings/definitions/reviveSettings"
},
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ var defaultLintersSettings = LintersSettings{
Predeclared: PredeclaredSettings{
Qualified: false,
},
PtrEquality: PtrEqualitySettings{
CheckIs: true,
},
SlogLint: SlogLintSettings{
NoMixedArgs: true,
KVOnly: false,
Expand Down Expand Up @@ -263,6 +266,7 @@ type LintersSettings struct {
Predeclared PredeclaredSettings `mapstructure:"predeclared"`
Promlinter PromlinterSettings `mapstructure:"promlinter"`
ProtoGetter ProtoGetterSettings `mapstructure:"protogetter"`
PtrEquality PtrEqualitySettings `mapstructure:"ptrequality"`
Reassign ReassignSettings `mapstructure:"reassign"`
Recvcheck RecvcheckSettings `mapstructure:"recvcheck"`
Revive ReviveSettings `mapstructure:"revive"`
Expand Down Expand Up @@ -761,6 +765,10 @@ type ProtoGetterSettings struct {
ReplaceFirstArgInAppend bool `mapstructure:"replace-first-arg-in-append"`
}

type PtrEqualitySettings struct {
CheckIs bool `mapstructure:"check-is"`
}

type ReassignSettings struct {
Patterns []string `mapstructure:"patterns"`
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/golinters/ptrequality/ptrequality.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ptrequality

import (
"github.com/fillmore-labs/ptrequality"

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

func New(settings *config.PtrEqualitySettings) *goanalysis.Linter {
return goanalysis.NewLinterFromAnalyzer(ptrequality.Analyzer).
WithConfig(map[string]any{
"check-is": settings.CheckIs,
}).
WithLoadMode(goanalysis.LoadModeTypesInfo)
}
11 changes: 11 additions & 0 deletions pkg/golinters/ptrequality/ptrequality_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ptrequality

import (
"testing"

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

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
22 changes: 22 additions & 0 deletions pkg/golinters/ptrequality/testdata/ptrequality.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//golangcitest:args -Eptrequality
//golangcitest:config_path testdata/ptrequality.yml
package main

import (
"errors"
"log"
"net/url"
)

func main() {
_, err := url.Parse("://example.com")

if errors.Is(err, &url.Error{}) { // want "is always false"
log.Fatal("Cannot parse URL")
}

var urlErr *url.Error
if errors.As(err, &urlErr) {
log.Fatalf("Cannot parse URL: %v", urlErr)
}
}
6 changes: 6 additions & 0 deletions pkg/golinters/ptrequality/testdata/ptrequality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "2"

linters:
settings:
ptrequality:
check-is: false
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import (
"github.com/golangci/golangci-lint/v2/pkg/golinters/predeclared"
"github.com/golangci/golangci-lint/v2/pkg/golinters/promlinter"
"github.com/golangci/golangci-lint/v2/pkg/golinters/protogetter"
"github.com/golangci/golangci-lint/v2/pkg/golinters/ptrequality"
"github.com/golangci/golangci-lint/v2/pkg/golinters/reassign"
"github.com/golangci/golangci-lint/v2/pkg/golinters/recvcheck"
"github.com/golangci/golangci-lint/v2/pkg/golinters/revive"
Expand Down Expand Up @@ -555,6 +556,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithAutoFix().
WithURL("https://github.com/ghostiam/protogetter"),

linter.NewConfig(ptrequality.New(&cfg.Linters.Settings.PtrEquality)).
WithSince("v2.2.0").
WithLoadForGoAnalysis().
WithURL("https://github.com/fillmore-labs/ptrequality"),

linter.NewConfig(reassign.New(&cfg.Linters.Settings.Reassign)).
WithSince("v1.49.0").
WithLoadForGoAnalysis().
Expand Down