Skip to content

Commit d6be453

Browse files
committed
Add ptrequality linter
ptrequality is a Go linter (static analysis tool) that detects comparisons against the address of newly created values, such as ptr == &MyStruct{} or ptr == new(MyStruct). https://pkg.go.dev/fillmore-labs.com/cmplint Signed-off-by: Oliver Eikemeier <[email protected]>
1 parent 51f1a17 commit d6be453

File tree

9 files changed

+95
-0
lines changed

9 files changed

+95
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.23.0
55
require (
66
4d63.com/gocheckcompilerdirectives v1.3.0
77
4d63.com/gochecknoglobals v0.2.2
8+
fillmore-labs.com/cmplint v0.0.3
89
github.com/4meepo/tagalign v1.4.2
910
github.com/Abirdcfly/dupword v0.1.6
1011
github.com/AlwxSin/noinlineerr v1.0.3

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@
804804
"predeclared",
805805
"promlinter",
806806
"protogetter",
807+
"ptrequality",
807808
"reassign",
808809
"recvcheck",
809810
"revive",
@@ -2881,6 +2882,17 @@
28812882
}
28822883
}
28832884
},
2885+
"ptrequalitySettings": {
2886+
"type": "object",
2887+
"additionalProperties": false,
2888+
"properties": {
2889+
"check-is": {
2890+
"description": "Suppress check for errors with \"Is\" method",
2891+
"type": "boolean",
2892+
"default": true
2893+
}
2894+
}
2895+
},
28842896
"reviveSettings": {
28852897
"type": "object",
28862898
"additionalProperties": false,
@@ -4530,6 +4542,9 @@
45304542
"protogetter": {
45314543
"$ref": "#/definitions/settings/definitions/protogetterSettings"
45324544
},
4545+
"ptrequality": {
4546+
"$ref": "#/definitions/settings/definitions/ptrequalitySettings"
4547+
},
45334548
"revive": {
45344549
"$ref": "#/definitions/settings/definitions/reviveSettings"
45354550
},

pkg/config/linters_settings.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ var defaultLintersSettings = LintersSettings{
137137
Predeclared: PredeclaredSettings{
138138
Qualified: false,
139139
},
140+
PtrEquality: PtrEqualitySettings{
141+
CheckIs: true,
142+
},
140143
SlogLint: SlogLintSettings{
141144
NoMixedArgs: true,
142145
KVOnly: false,
@@ -263,6 +266,7 @@ type LintersSettings struct {
263266
Predeclared PredeclaredSettings `mapstructure:"predeclared"`
264267
Promlinter PromlinterSettings `mapstructure:"promlinter"`
265268
ProtoGetter ProtoGetterSettings `mapstructure:"protogetter"`
269+
PtrEquality PtrEqualitySettings `mapstructure:"ptrequality"`
266270
Reassign ReassignSettings `mapstructure:"reassign"`
267271
Recvcheck RecvcheckSettings `mapstructure:"recvcheck"`
268272
Revive ReviveSettings `mapstructure:"revive"`
@@ -761,6 +765,10 @@ type ProtoGetterSettings struct {
761765
ReplaceFirstArgInAppend bool `mapstructure:"replace-first-arg-in-append"`
762766
}
763767

768+
type PtrEqualitySettings struct {
769+
CheckIs bool `mapstructure:"check-is"`
770+
}
771+
764772
type ReassignSettings struct {
765773
Patterns []string `mapstructure:"patterns"`
766774
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ptrequality
2+
3+
import (
4+
"fillmore-labs.com/cmplint/analyzer"
5+
6+
"github.com/golangci/golangci-lint/v2/pkg/config"
7+
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
8+
)
9+
10+
const (
11+
Name = "ptrequality"
12+
Doc = "Detect new pointers used within equality comparisons, making the result always false or undefined"
13+
)
14+
15+
func New(settings *config.PtrEqualitySettings) *goanalysis.Linter {
16+
a := analyzer.New(
17+
analyzer.WithName(Name),
18+
analyzer.WithDoc(Doc),
19+
analyzer.WithCheckIs(settings.CheckIs),
20+
)
21+
22+
return goanalysis.NewLinterFromAnalyzer(a).
23+
WithLoadMode(goanalysis.LoadModeTypesInfo)
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ptrequality
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/v2/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//golangcitest:args -Eptrequality
2+
//golangcitest:config_path testdata/ptrequality.yml
3+
package main
4+
5+
import (
6+
"errors"
7+
"log"
8+
"net/url"
9+
)
10+
11+
func main() {
12+
_, err := url.Parse("://example.com")
13+
14+
if errors.Is(err, &url.Error{}) { // want "is always false"
15+
log.Fatal("Cannot parse URL")
16+
}
17+
18+
var urlErr *url.Error
19+
if errors.As(err, &urlErr) {
20+
log.Fatalf("Cannot parse URL: %v", urlErr)
21+
}
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
ptrequality:
6+
check-is: false

pkg/lint/lintersdb/builder_linter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import (
8989
"github.com/golangci/golangci-lint/v2/pkg/golinters/predeclared"
9090
"github.com/golangci/golangci-lint/v2/pkg/golinters/promlinter"
9191
"github.com/golangci/golangci-lint/v2/pkg/golinters/protogetter"
92+
"github.com/golangci/golangci-lint/v2/pkg/golinters/ptrequality"
9293
"github.com/golangci/golangci-lint/v2/pkg/golinters/reassign"
9394
"github.com/golangci/golangci-lint/v2/pkg/golinters/recvcheck"
9495
"github.com/golangci/golangci-lint/v2/pkg/golinters/revive"
@@ -555,6 +556,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
555556
WithAutoFix().
556557
WithURL("https://github.com/ghostiam/protogetter"),
557558

559+
linter.NewConfig(ptrequality.New(&cfg.Linters.Settings.PtrEquality)).
560+
WithSince("v2.2.0").
561+
WithLoadForGoAnalysis().
562+
WithURL("https://github.com/fillmore-labs/cmplint"),
563+
558564
linter.NewConfig(reassign.New(&cfg.Linters.Settings.Reassign)).
559565
WithSince("v1.49.0").
560566
WithLoadForGoAnalysis().

0 commit comments

Comments
 (0)