Skip to content

Commit 18eb871

Browse files
authored
Only apply 'field.id.{negative,zero}' to explicit field IDs (#50)
Auto-assigned field IDs start at zero and then go negative. If auto- assigned fields are intentionally used (i.e. no `field.id.missing` check), then it's not helpful to report additional errors about the range of the auto-assigned values. These checks now only report errors for explicitly assigned field IDs.
1 parent 80432a2 commit 18eb871

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ implicit/auto-assigning syntax).
9494

9595
### `field.id.negative`
9696

97-
This check reports an error if a field's ID is negative.
97+
This check reports an error if a field's ID is explicitly negative.
9898

9999
### `field.id.zero`
100100

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`
101+
This check reports an error if a field's ID is explicitly zero, which is
102+
generally unsupported by the Apache Thrift compiler. This is distinct from
103+
the `field.id.negative` check given the existence of the `--allow-neg-keys`
104104
Apache Thrift compiler option.
105105

106106
### `field.optional`

checks/fields.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ func CheckFieldIDMissing() *thriftcheck.Check {
2828
})
2929
}
3030

31-
// CheckFieldIDNegative reports an error if a field's ID is negative.
31+
// CheckFieldIDNegative reports an error if a field's ID is explicitly negative.
3232
func CheckFieldIDNegative() *thriftcheck.Check {
3333
return thriftcheck.NewCheck("field.id.negative", func(c *thriftcheck.C, f *ast.Field) {
34-
if f.ID < 0 {
34+
if !f.IDUnset && f.ID < 0 {
3535
c.Errorf(f, "field ID for %q (%d) is negative", f.Name, f.ID)
3636
}
3737
})
3838
}
3939

40-
// CheckFieldIDZero reports an error if a field's ID is zero.
40+
// CheckFieldIDZero reports an error if a field's ID is explicitly zero.
4141
func CheckFieldIDZero() *thriftcheck.Check {
4242
return thriftcheck.NewCheck("field.id.zero", func(c *thriftcheck.C, f *ast.Field) {
43-
if f.ID == 0 {
43+
if !f.IDUnset && f.ID == 0 {
4444
c.Errorf(f, "field ID for %q is zero", f.Name)
4545
}
4646
})

checks/fields_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func TestCheckFieldIDNegative(t *testing.T) {
5555
`t.thrift:0:1: error: field ID for "Field" (-1) is negative (field.id.negative)`,
5656
},
5757
},
58+
{
59+
node: &ast.Field{IDUnset: true},
60+
want: []string{},
61+
},
5862
}
5963

6064
check := checks.CheckFieldIDNegative()
@@ -77,6 +81,10 @@ func TestCheckFieldIDZero(t *testing.T) {
7781
node: &ast.Field{ID: -1, Name: "Field"},
7882
want: []string{},
7983
},
84+
{
85+
node: &ast.Field{IDUnset: true},
86+
want: []string{},
87+
},
8088
}
8189

8290
check := checks.CheckFieldIDZero()

0 commit comments

Comments
 (0)