Skip to content

feat: add embeddedstructfieldcheck linter #5761

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

Merged
merged 14 commits into from
May 13, 2025
Merged
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 @@ -33,6 +33,7 @@ linters:
- dupl
- dupword
- durationcheck
- embeddedstructfieldcheck
- err113
- errcheck
- errchkjson
Expand Down Expand Up @@ -142,6 +143,7 @@ linters:
- dupl
- dupword
- durationcheck
- embeddedstructfieldcheck
- err113
- errcheck
- errchkjson
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ require (
github.com/ldez/usetesting v0.4.3
github.com/leonklingele/grouper v1.1.2
github.com/macabu/inamedparam v0.2.0
github.com/manuelarte/embeddedstructfieldcheck v0.2.1
github.com/manuelarte/funcorder v0.5.0
github.com/maratori/testableexamples v1.0.0
github.com/maratori/testpackage v1.1.1
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 @@ -734,6 +734,7 @@
"dupl",
"dupword",
"durationcheck",
"embeddedstructfieldcheck",
"errcheck",
"errchkjson",
"errname",
Expand Down
19 changes: 19 additions & 0 deletions pkg/golinters/embeddedstructfieldcheck/embeddedstructfieldcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package embeddedstructfieldcheck

import (
"github.com/manuelarte/embeddedstructfieldcheck/analyzer"
"golang.org/x/tools/go/analysis"

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

func New() *goanalysis.Linter {
a := analyzer.NewAnalyzer()

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package embeddedstructfieldcheck

import (
"testing"

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

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}

func TestFix(t *testing.T) {
integration.RunFix(t)
}

func TestFixPathPrefix(t *testing.T) {
integration.RunFixPathPrefix(t)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//golangcitest:args -Eembeddedstructfieldcheck
package simple

import "time"

type ValidStructWithSingleLineComments struct {
// time.Time Single line comment
time.Time

// version Single line comment
version int
}

type StructWithSingleLineComments struct {
// time.Time Single line comment
time.Time // want `there must be an empty line separating embedded fields from regular fields`
// version Single line comment
version int
}

type StructWithMultiLineComments struct {
// time.Time Single line comment
time.Time // want `there must be an empty line separating embedded fields from regular fields`
// version Single line comment
// very long comment
version int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//golangcitest:args -Eembeddedstructfieldcheck
package simple

import (
"context"
"time"
)

type ValidStruct struct {
time.Time

version int
}

type NoSpaceStruct struct {
time.Time // want `there must be an empty line separating embedded fields from regular fields`
version int
}

type NotSortedStruct struct {
version int

time.Time // want `embedded fields should be listed before regular fields`
}

type MixedEmbeddedAndNotEmbedded struct {
context.Context

name string

time.Time // want `embedded fields should be listed before regular fields`

age int
}

type EmbeddedWithPointers struct {
*time.Time // want `there must be an empty line separating embedded fields from regular fields`
version int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//golangcitest:args -Eembeddedstructfieldcheck
package simple

import "time"

func myFunction() {
type myType struct {
version int
time.Time // want `embedded fields should be listed before regular fields`
}
}
39 changes: 39 additions & 0 deletions pkg/golinters/embeddedstructfieldcheck/testdata/fix/in/comments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//golangcitest:args -Eembeddedstructfieldcheck
//golangcitest:expected_exitcode 0
package testdata

import "time"

type ValidStructWithSingleLineComments struct {
// time.Time Single line comment
time.Time

// version Single line comment
version int
}

type StructWithSingleLineComments struct {
// time.Time Single line comment
time.Time // want `there must be an empty line separating embedded fields from regular fields`

// version Single line comment
version int
}

type StructWithMultiLineComments struct {
// time.Time Single line comment
time.Time // want `there must be an empty line separating embedded fields from regular fields`

// version Single line comment
// very long comment
version int
}

type A struct {
// comment
ValidStructWithSingleLineComments
// C is foo
StructWithSingleLineComments // want `there must be an empty line separating embedded fields from regular fields`

D string
}
23 changes: 23 additions & 0 deletions pkg/golinters/embeddedstructfieldcheck/testdata/fix/in/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//golangcitest:args -Eembeddedstructfieldcheck
//golangcitest:expected_exitcode 0
package testdata

import (
"time"
)

type ValidStruct struct {
time.Time

version int
}

type NoSpaceStruct struct {
time.Time // want `there must be an empty line separating embedded fields from regular fields`
version int
}

type EmbeddedWithPointers struct {
*time.Time // want `there must be an empty line separating embedded fields from regular fields`
version int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//golangcitest:args -Eembeddedstructfieldcheck
//golangcitest:expected_exitcode 0
package testdata

import "time"

type ValidStructWithSingleLineComments struct {
// time.Time Single line comment
time.Time

// version Single line comment
version int
}

type StructWithSingleLineComments struct {
// time.Time Single line comment
time.Time // want `there must be an empty line separating embedded fields from regular fields`

// version Single line comment
version int
}

type StructWithMultiLineComments struct {
// time.Time Single line comment
time.Time // want `there must be an empty line separating embedded fields from regular fields`

// version Single line comment
// very long comment
version int
}

type A struct {
// comment
ValidStructWithSingleLineComments
// C is foo
StructWithSingleLineComments // want `there must be an empty line separating embedded fields from regular fields`

D string
}
25 changes: 25 additions & 0 deletions pkg/golinters/embeddedstructfieldcheck/testdata/fix/out/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//golangcitest:args -Eembeddedstructfieldcheck
//golangcitest:expected_exitcode 0
package testdata

import (
"time"
)

type ValidStruct struct {
time.Time

version int
}

type NoSpaceStruct struct {
time.Time // want `there must be an empty line separating embedded fields from regular fields`

version int
}

type EmbeddedWithPointers struct {
*time.Time // want `there must be an empty line separating embedded fields from regular fields`

version int
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/golangci/golangci-lint/v2/pkg/golinters/dupl"
"github.com/golangci/golangci-lint/v2/pkg/golinters/dupword"
"github.com/golangci/golangci-lint/v2/pkg/golinters/durationcheck"
"github.com/golangci/golangci-lint/v2/pkg/golinters/embeddedstructfieldcheck"
"github.com/golangci/golangci-lint/v2/pkg/golinters/err113"
"github.com/golangci/golangci-lint/v2/pkg/golinters/errcheck"
"github.com/golangci/golangci-lint/v2/pkg/golinters/errchkjson"
Expand Down Expand Up @@ -212,6 +213,10 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/charithe/durationcheck"),

linter.NewConfig(embeddedstructfieldcheck.New()).
WithSince("v2.2.0").
WithURL("https://github.com/manuelarte/embeddedstructfieldcheck"),

linter.NewConfig(errcheck.New(&cfg.Linters.Settings.Errcheck)).
WithGroups(config.GroupStandard).
WithSince("v1.0.0").
Expand Down
Loading