Skip to content

Commit 80432a2

Browse files
authored
Add a 'field.id.zero' check (#49)
This check reports an error if a field's ID is zero, which is generally unsupported by the Apache Thrift compiler. This is distinct from the `field.id.negative` check given the existence of the `--allow-neg-keys` Apache Thrift compiler option.
1 parent e950ef2 commit 80432a2

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ implicit/auto-assigning syntax).
9696

9797
This check reports an error if a field's ID is negative.
9898

99+
### `field.id.zero`
100+
101+
This check reports an error if a field's ID is zero, which is generally
102+
unsupported by the Apache Thrift compiler. This is distinct from the
103+
`field.id.negative` check given the existence of the `--allow-neg-keys`
104+
Apache Thrift compiler option.
105+
99106
### `field.optional`
100107

101108
This check warns if a field isn't declared as "optional", which is considered

checks/fields.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ func CheckFieldIDNegative() *thriftcheck.Check {
3737
})
3838
}
3939

40+
// CheckFieldIDZero reports an error if a field's ID is zero.
41+
func CheckFieldIDZero() *thriftcheck.Check {
42+
return thriftcheck.NewCheck("field.id.zero", func(c *thriftcheck.C, f *ast.Field) {
43+
if f.ID == 0 {
44+
c.Errorf(f, "field ID for %q is zero", f.Name)
45+
}
46+
})
47+
}
48+
4049
// CheckFieldOptional warns if a field isn't declared as "optional".
4150
func CheckFieldOptional() *thriftcheck.Check {
4251
return thriftcheck.NewCheck("field.optional", func(c *thriftcheck.C, f *ast.Field) {

checks/fields_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ func TestCheckFieldIDNegative(t *testing.T) {
6161
RunTests(t, check, tests)
6262
}
6363

64+
func TestCheckFieldIDZero(t *testing.T) {
65+
tests := []Test{
66+
{
67+
node: &ast.Field{ID: 1, Name: "Field"},
68+
want: []string{},
69+
},
70+
{
71+
node: &ast.Field{ID: 0, Name: "Field"},
72+
want: []string{
73+
`t.thrift:0:1: error: field ID for "Field" is zero (field.id.zero)`,
74+
},
75+
},
76+
{
77+
node: &ast.Field{ID: -1, Name: "Field"},
78+
want: []string{},
79+
},
80+
}
81+
82+
check := checks.CheckFieldIDZero()
83+
RunTests(t, check, tests)
84+
}
85+
6486
func TestCheckFieldOptional(t *testing.T) {
6587
tests := []Test{
6688
{

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func main() {
155155
checks.CheckEnumSize(cfg.Checks.Enum.Size.Warning, cfg.Checks.Enum.Size.Error),
156156
checks.CheckFieldIDMissing(),
157157
checks.CheckFieldIDNegative(),
158+
checks.CheckFieldIDZero(),
158159
checks.CheckFieldOptional(),
159160
checks.CheckFieldRequiredness(),
160161
checks.CheckIncludePath(),

0 commit comments

Comments
 (0)