Skip to content

feat: add exhaustruct linter #2667

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 7 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
review: tests
  • Loading branch information
ldez committed Apr 30, 2022
commit 47ec81a38e59318603b130c139360fa04dfbe4a7
2 changes: 1 addition & 1 deletion pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle, linter.PresetTest).
WithLoadForGoAnalysis().
WithURL("https://github.com/mbilski/exhaustivestruct").
Deprecated("Owner seems to abandon linter.", "v1.46.0", "exhaustruct"),
Deprecated("The owner seems to have abandoned the linter.", "v1.46.0", "exhaustruct"),

linter.NewConfig(golinters.NewExhaustruct(exhaustructCfg)).
WithSince("v1.46.0").
Expand Down
5 changes: 5 additions & 0 deletions test/testdata/configs/exhaustivestruct.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters-settings:
exhaustivestruct:
struct-patterns:
- '*.ExhaustiveStructCustom'
- '*.ExhaustiveStructCustom2'
4 changes: 4 additions & 0 deletions test/testdata/configs/exhaustruct.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
exhaustruct:
include: ".*\\.ExhaustructCustom$"
exclude: ".*\\.ExhaustructCustom2"
56 changes: 31 additions & 25 deletions test/testdata/exhaustivestruct.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
//args: -Eexhaustivestruct
// args: -Eexhaustivestruct --internal-cmd-test
package testdata

import "time"

type Test struct {
type ExhaustiveStruct struct {
A string
B int
c bool // private field inside the same package are not ignored
D float64
E time.Time
}

var pass = Test{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}
func exhaustiveStruct() {
// pass
_ = ExhaustiveStruct{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}

var failPrivate = Test{ // ERROR "c is missing in Test"
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}
// failPrivate
_ = ExhaustiveStruct{ // ERROR "c is missing in ExhaustiveStruct"
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}

var fail = Test{ // ERROR "B is missing in Test"
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}
// fail
_ = ExhaustiveStruct{ // ERROR "B is missing in ExhaustiveStruct"
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}

var failMultiple = Test{ // ERROR "B, D are missing in Test"
A: "a",
c: false,
E: time.Now(),
// failMultiple
_ = ExhaustiveStruct{ // ERROR "B, D are missing in ExhaustiveStruct"
A: "a",
c: false,
E: time.Now(),
}
}
178 changes: 97 additions & 81 deletions test/testdata/exhaustivestruct_custom.go
Original file line number Diff line number Diff line change
@@ -1,113 +1,129 @@
//args: -Eexhaustivestruct
//config: linters-settings.exhaustivestruct.struct-patterns=*.Test1,*.Test3
// args: -Eexhaustivestruct --internal-cmd-test
// config_path: testdata/configs/exhaustivestruct.yml
package testdata

import "time"

type Test1 struct {
type ExhaustiveStructCustom struct {
A string
B int
c bool // private field inside the same package are not ignored
D float64
E time.Time
}

var passTest1 = Test1{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}

var failTest1 = Test1{ // ERROR "B is missing in Test"
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}

var failMultipleTest1 = Test1{ // ERROR "B, D are missing in Test"
A: "a",
c: false,
E: time.Now(),
}
func exhaustiveStructCustom() {
// pass
_ = ExhaustiveStructCustom{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}

// fail
_ = ExhaustiveStructCustom{ // ERROR "B is missing in ExhaustiveStructCustom"
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}

// failMultiple
_ = ExhaustiveStructCustom{ // ERROR "B, D are missing in ExhaustiveStructCustom"
A: "a",
c: false,
E: time.Now(),
}

// failPrivate
_ = ExhaustiveStructCustom{ // ERROR "c is missing in ExhaustiveStructCustom"
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}

var failPrivateTest1 = Test1{ // ERROR "c is missing in Test"
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}

type Test2 struct {
type ExhaustiveStructCustom1 struct {
A string
B int
c bool // private field inside the same package are not ignored
D float64
E time.Time
}

var passTest2 = Test1{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}

var failTest2 = Test2{
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}

var failMultipleTest2 = Test2{
A: "a",
c: false,
E: time.Now(),
}

var failPrivateTest2 = Test2{
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
func exhaustiveStructCustom1() {
_ = ExhaustiveStructCustom1{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}

_ = ExhaustiveStructCustom1{
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}

_ = ExhaustiveStructCustom1{
A: "a",
c: false,
E: time.Now(),
}

_ = ExhaustiveStructCustom1{
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}
}

type Test3 struct {
type ExhaustiveStructCustom2 struct {
A string
B int
c bool // private field inside the same package are not ignored
D float64
E time.Time
}

var passTest3 = Test3{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}

var failTest3 = Test3{ // ERROR "B is missing in Test"
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}

var failMultipleTest3 = Test3{ // ERROR "B, D are missing in Test"
A: "a",
c: false,
E: time.Now(),
}
func exhaustiveStructCustom2() {
// pass
_ = ExhaustiveStructCustom2{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}

// fail
_ = ExhaustiveStructCustom2{ // ERROR "B is missing in ExhaustiveStructCustom2"
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}

// failMultiple
_ = ExhaustiveStructCustom2{ // ERROR "B, D are missing in ExhaustiveStructCustom2"
A: "a",
c: false,
E: time.Now(),
}

// failPrivate
_ = ExhaustiveStructCustom2{ // ERROR "c is missing in ExhaustiveStructCustom2"
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}

var failPrivateTest3 = Test3{ // ERROR "c is missing in Test"
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}
57 changes: 32 additions & 25 deletions test/testdata/exhaustruct.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
//args: -Eexhaustruct
// args: -Eexhaustruct
package testdata

import "time"

type Test struct {
type Exhaustruct struct {
A string
B int
c bool // private field inside the same package are not ignored
D float64
E time.Time
}

var pass = Test{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}
func exhaustruct() {
// pass
_ = Exhaustruct{
A: "a",
B: 0,
c: false,
D: 1.0,
E: time.Now(),
}

var failPrivate = Test{ // ERROR "c is missing in Test"
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}
// failPrivate
_ = Exhaustruct{ // ERROR "c is missing in Exhaustruct"
A: "a",
B: 0,
D: 1.0,
E: time.Now(),
}

var fail = Test{ // ERROR "B is missing in Test"
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}
// fail
_ = Exhaustruct{ // ERROR "B is missing in Exhaustruct"
A: "a",
c: false,
D: 1.0,
E: time.Now(),
}

// failMultiple
_ = Exhaustruct{ // ERROR "B, D are missing in Exhaustruct"
A: "a",
c: false,
E: time.Now(),
}

var failMultiple = Test{ // ERROR "B, D are missing in Test"
A: "a",
c: false,
E: time.Now(),
}
Loading