-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exhaustivestruct: add missing settings (#1746)
- Loading branch information
Showing
5 changed files
with
171 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
package golinters | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mbilski/exhaustivestruct/pkg/analyzer" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewExhaustiveStruct() *goanalysis.Linter { | ||
func NewExhaustiveStruct(settings *config.ExhaustiveStructSettings) *goanalysis.Linter { | ||
a := analyzer.Analyzer | ||
|
||
var cfg map[string]map[string]interface{} | ||
if settings != nil { | ||
cfg = map[string]map[string]interface{}{ | ||
a.Name: { | ||
"struct_patterns": strings.Join(settings.StructPatterns, ","), | ||
}, | ||
} | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
"exhaustivestruct", | ||
"Checks if all struct's fields are initialized", | ||
[]*analysis.Analyzer{analyzer.Analyzer}, | ||
nil, | ||
a.Name, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
cfg, | ||
).WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
//args: -Eexhaustivestruct | ||
//config: linters-settings.exhaustivestruct.struct-patterns=*.Test1,*.Test3 | ||
package testdata | ||
|
||
import "time" | ||
|
||
type Test1 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(), | ||
} | ||
|
||
var failPrivateTest1 = Test1{ // ERROR "c is missing in Test" | ||
A: "a", | ||
B: 0, | ||
D: 1.0, | ||
E: time.Now(), | ||
} | ||
|
||
type Test2 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(), | ||
} | ||
|
||
type Test3 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(), | ||
} | ||
|
||
var failPrivateTest3 = Test3{ // ERROR "c is missing in Test" | ||
A: "a", | ||
B: 0, | ||
D: 1.0, | ||
E: time.Now(), | ||
} |