Skip to content

Commit 5d732c5

Browse files
committed
Add cmplint linter
cmplint 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 cc3f6de commit 5d732c5

File tree

9 files changed

+87
-0
lines changed

9 files changed

+87
-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.2
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
@@ -728,6 +728,7 @@
728728
"bidichk",
729729
"bodyclose",
730730
"canonicalheader",
731+
"cmplint",
731732
"containedctx",
732733
"contextcheck",
733734
"copyloopvar",
@@ -945,6 +946,17 @@
945946
}
946947
}
947948
},
949+
"cmplintSettings": {
950+
"type": "object",
951+
"additionalProperties": false,
952+
"properties": {
953+
"check-is": {
954+
"description": "Suppress check for errors with \"Is\" method",
955+
"type": "boolean",
956+
"default": true
957+
}
958+
}
959+
},
948960
"cyclopSettings": {
949961
"type": "object",
950962
"additionalProperties": false,
@@ -4362,6 +4374,9 @@
43624374
"bidichk": {
43634375
"$ref": "#/definitions/settings/definitions/bidichkSettings"
43644376
},
4377+
"cmplint": {
4378+
"$ref": "#/definitions/settings/definitions/cmplintSettings"
4379+
},
43654380
"cyclop": {
43664381
"$ref": "#/definitions/settings/definitions/cyclopSettings"
43674382
},

pkg/config/linters_settings.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ var defaultLintersSettings = LintersSettings{
1212
Asasalint: AsasalintSettings{
1313
UseBuiltinExclusions: true,
1414
},
15+
Cmplint: CmplintSettings{
16+
CheckIs: true,
17+
},
1518
Decorder: DecorderSettings{
1619
DecOrder: []string{"type", "const", "var", "func"},
1720
DisableDecNumCheck: true,
@@ -209,6 +212,7 @@ type LintersSettings struct {
209212
BiDiChk BiDiChkSettings `mapstructure:"bidichk"`
210213
CopyLoopVar CopyLoopVarSettings `mapstructure:"copyloopvar"`
211214
Cyclop CyclopSettings `mapstructure:"cyclop"`
215+
Cmplint CmplintSettings `mapstructure:"cmplint"`
212216
Decorder DecorderSettings `mapstructure:"decorder"`
213217
Depguard DepGuardSettings `mapstructure:"depguard"`
214218
Dogsled DogsledSettings `mapstructure:"dogsled"`
@@ -319,6 +323,10 @@ type BiDiChkSettings struct {
319323
PopDirectionalIsolate bool `mapstructure:"pop-directional-isolate"`
320324
}
321325

326+
type CmplintSettings struct {
327+
CheckIs bool `mapstructure:"check-is"`
328+
}
329+
322330
type CopyLoopVarSettings struct {
323331
CheckAlias bool `mapstructure:"check-alias"`
324332
}

pkg/golinters/cmplint/cmplint.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cmplint
2+
3+
import (
4+
cmplint "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+
func New(settings *config.CmplintSettings) *goanalysis.Linter {
11+
cmplint.CheckIs = settings.CheckIs
12+
13+
return goanalysis.
14+
NewLinterFromAnalyzer(cmplint.Analyzer).
15+
WithLoadMode(goanalysis.LoadModeTypesInfo)
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cmplint
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 -Ecmplint
2+
//golangcitest:config_path testdata/cmplint.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+
cmplint:
6+
check-is: false

pkg/lint/lintersdb/builder_linter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/golangci/golangci-lint/v2/pkg/golinters/bidichk"
1010
"github.com/golangci/golangci-lint/v2/pkg/golinters/bodyclose"
1111
"github.com/golangci/golangci-lint/v2/pkg/golinters/canonicalheader"
12+
"github.com/golangci/golangci-lint/v2/pkg/golinters/cmplint"
1213
"github.com/golangci/golangci-lint/v2/pkg/golinters/containedctx"
1314
"github.com/golangci/golangci-lint/v2/pkg/golinters/contextcheck"
1415
"github.com/golangci/golangci-lint/v2/pkg/golinters/copyloopvar"
@@ -168,6 +169,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
168169
WithAutoFix().
169170
WithURL("https://github.com/lasiar/canonicalheader"),
170171

172+
linter.NewConfig(cmplint.New(&cfg.Linters.Settings.Cmplint)).
173+
WithSince("v2.2.0").
174+
WithLoadForGoAnalysis().
175+
WithURL("https://github.com/fillmore-labs/cmplint"),
176+
171177
linter.NewConfig(containedctx.New()).
172178
WithSince("v1.44.0").
173179
WithLoadForGoAnalysis().

0 commit comments

Comments
 (0)