Skip to content
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

Add constructorcheck linter #4937

Open
wants to merge 5 commits into
base: master
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 .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,7 @@ linters:
- bidichk
- bodyclose
- canonicalheader
- constructorcheck
- containedctx
- contextcheck
- copyloopvar
Expand Down Expand Up @@ -2722,6 +2723,7 @@ linters:
- bidichk
- bodyclose
- canonicalheader
- constructorcheck
- containedctx
- contextcheck
- copyloopvar
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2
github.com/polyfloyd/go-errorlint v1.6.0
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/reflechant/constructor-check v1.0.1
github.com/ryancurrah/gomodguard v1.3.3
github.com/ryanrolds/sqlclosecheck v0.5.1
github.com/sanposhiho/wastedassign/v2 v2.0.7
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.

1 change: 1 addition & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
"bidichk",
"bodyclose",
"canonicalheader",
"constructorcheck",
"containedctx",
"contextcheck",
"copyloopvar",
Expand Down
18 changes: 18 additions & 0 deletions pkg/golinters/constructorcheck/constructorcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package constructorcheck

import (
"github.com/golangci/golangci-lint/pkg/goanalysis"
constructorcheck "github.com/reflechant/constructor-check/analyzer"
"golang.org/x/tools/go/analysis"
)

func New() *goanalysis.Linter {
a := constructorcheck.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
11 changes: 11 additions & 0 deletions pkg/golinters/constructorcheck/constructorcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package constructorcheck

import (
"testing"

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

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

import (
"bytes"
"fmt"
)

var buf = bytes.Buffer{} // standard library is excluded from analysis

// T is a type whose zero values are supposedly invalid
// so a constructor NewT was created.
type T struct {
x int
s string
m map[int]int
}

var (
tNil *T // want `nil value of type command-line-arguments.T may be unsafe, use constructor NewT instead`
tZero = T{} // want `zero value of type command-line-arguments.T may be unsafe, use constructor NewT instead`
tZeroPtr = &T{} // want `zero value of type command-line-arguments.T may be unsafe, use constructor NewT instead`
tNew = new(T) // want `zero value of type command-line-arguments.T may be unsafe, use constructor NewT instead`
tComposite = T{ // want `use constructor NewT for type command-line-arguments.T instead of a composite literal`
x: 1,
s: "abc",
}
tCompositePtr = &T{ // want `use constructor NewT for type command-line-arguments.T instead of a composite literal`
x: 1,
s: "abc",
}
tColl = []T{T{x: 1}} // want `use constructor NewT for type command-line-arguments.T instead of a composite literal`
tPtrColl = []*T{&T{x: 1}} // want `use constructor NewT for type command-line-arguments.T instead of a composite literal`

)

// NewT is a valid constructor for type T. Here we check if it's called
// instead of constructing values of type T manually
func NewT() *T {
return &T{
m: make(map[int]int),
}
}

type structWithTField struct {
i int
t T
}

var structWithT = structWithTField{
i: 1,
t: T{x: 1}, // want `use constructor NewT for type command-line-arguments.T instead of a composite literal`
}

type structWithTPtrField struct {
i int
t *T
}

var structWithTPtr = structWithTPtrField{
i: 1,
t: &T{x: 1}, // want `use constructor NewT for type command-line-arguments.T instead of a composite literal`
}

func fnWithT() {
x := T{} // want `zero value of type command-line-arguments.T may be unsafe, use constructor NewT instead`
x2 := &T{} // want `zero value of type command-line-arguments.T may be unsafe, use constructor NewT instead`
x3 := new(T) // want `zero value of type command-line-arguments.T may be unsafe, use constructor NewT instead`
fmt.Println(x, x2, x3)
}

func retT() T {
return T{ // want `use constructor NewT for type command-line-arguments.T instead of a composite literal`
x: 1,
}
}

func retTPtr() *T {
return &T{ // want `use constructor NewT for type command-line-arguments.T instead of a composite literal`
x: 1,
}
}

func retTNilPtr() *T {
var t *T // want `nil value of type command-line-arguments.T may be unsafe, use constructor NewT instead`
return t
}

type T2 struct {
x int
}

func NewT2() *T2 {
// new(T) inside T's constructor is permitted
return new(T2)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/golangci/golangci-lint/pkg/golinters/bidichk"
"github.com/golangci/golangci-lint/pkg/golinters/bodyclose"
"github.com/golangci/golangci-lint/pkg/golinters/canonicalheader"
"github.com/golangci/golangci-lint/pkg/golinters/constructorcheck"
"github.com/golangci/golangci-lint/pkg/golinters/containedctx"
"github.com/golangci/golangci-lint/pkg/golinters/contextcheck"
"github.com/golangci/golangci-lint/pkg/golinters/copyloopvar"
Expand Down Expand Up @@ -161,6 +162,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/lasiar/canonicalHeader"),

linter.NewConfig(constructorcheck.New()).
WithSince("v1.61.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/reflechant/constructor-check"),

linter.NewConfig(containedctx.New()).
WithSince("1.44.0").
WithLoadForGoAnalysis().
Expand Down