Skip to content

Commit 3ac2bca

Browse files
committed
oneoflint: tool checking an exhaustive match for proto code
1 parent 87f87bd commit 3ac2bca

File tree

6 files changed

+126
-1
lines changed

6 files changed

+126
-1
lines changed

cmd/oneoflint/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"golang.org/x/tools/go/analysis/unitchecker"
5+
6+
"github.com/gomoni/sumlint/internal/lint"
7+
)
8+
9+
func main() {
10+
unitchecker.Main(lint.Oneof)
11+
}

internal/lint/lint.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ var Sum = &analysis.Analyzer{
3131
},
3232
}
3333

34+
var Oneof = &analysis.Analyzer{
35+
Name: "oneoflint",
36+
Doc: "checks exhaustive type switches over oneof fields in proto files",
37+
Run: analyzer{prefix: "is"}.run,
38+
FactTypes: []analysis.Fact{
39+
new(InterfaceImplementorsFact),
40+
},
41+
}
42+
3443
type analyzer struct {
3544
prefix string
3645
}

internal/lint/lint_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ func TestAnalyzer(t *testing.T) {
2323
pkgs: []string{
2424
"exhaustive",
2525
"nonexhaustive",
26-
"dflt"},
26+
"dflt",
27+
},
28+
},
29+
{
30+
scenario: "oneoflint",
31+
analyzer: lint.Oneof,
32+
pkgs: []string{
33+
"one_of",
34+
},
2735
},
2836
}
2937
for _, tc := range testCases {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: v1
2+
plugins:
3+
- plugin: go
4+
out: .
5+
opt: paths=source_relative
6+
- plugin: go-grpc
7+
out: .
8+
opt: paths=source_relative

internal/lint/testdata/src/one_of/oneof.pb.go

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
syntax = "proto3";
2+
option go_package="one.of";
3+
package oneof;
4+
5+
message A {
6+
int32 id = 1;
7+
}
8+
message B {
9+
string name = 1;
10+
}
11+
12+
message Msg {
13+
oneof payload {
14+
A a = 1;
15+
B b = 2;
16+
}
17+
}

0 commit comments

Comments
 (0)