forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
exhaustruct
linter (golangci#2667)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
- Loading branch information
Showing
13 changed files
with
380 additions
and
107 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 @@ | ||
package golinters | ||
|
||
import "github.com/golangci/golangci-lint/pkg/logutils" | ||
|
||
// linterLogger must be use only when the context logger is not available. | ||
var linterLogger = logutils.NewStderrLog("linter") |
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,26 @@ | ||
package golinters | ||
|
||
import ( | ||
"github.com/GaijinEntertainment/go-exhaustruct/v2/pkg/analyzer" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewExhaustruct(settings *config.ExhaustructSettings) *goanalysis.Linter { | ||
var include, exclude []string | ||
|
||
if settings != nil { | ||
include = settings.Include | ||
exclude = settings.Exclude | ||
} | ||
|
||
a, err := analyzer.NewAnalyzer(include, exclude) | ||
if err != nil { | ||
linterLogger.Fatalf("exhaustruct configuration: %v", err) | ||
} | ||
|
||
return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, nil). | ||
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,5 @@ | ||
linters-settings: | ||
exhaustivestruct: | ||
struct-patterns: | ||
- '*.ExhaustiveStructCustom' | ||
- '*.ExhaustiveStructCustom2' |
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,6 @@ | ||
linters-settings: | ||
exhaustruct: | ||
include: | ||
- .*\.ExhaustructCustom | ||
exclude: | ||
- .*\.ExhaustructCustom[\d]{1,2} |
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,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(), | ||
} | ||
} |
Oops, something went wrong.