Skip to content

Commit d5e1bf5

Browse files
committed
add more test cases for issue #11 and fix bugs
1 parent 14ae6f5 commit d5e1bf5

File tree

5 files changed

+162
-24
lines changed

5 files changed

+162
-24
lines changed

processor.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ func (c *processor) process(n ast.Node) (*Result, error) {
4242

4343
if len(x.Rhs) > i {
4444
value := x.Rhs[i]
45-
if hasPointerKeyWithoutPointerGetter(c.info, s, value) {
46-
c.filter.AddPos(value.Pos())
45+
if se, ok := value.(*ast.SelectorExpr); ok {
46+
if hasPointerKeyWithoutPointerGetter(c.info, s, se) {
47+
c.filter.AddPos(se.Sel.Pos())
48+
}
4749
}
4850
}
4951
}
@@ -60,8 +62,10 @@ func (c *processor) process(n ast.Node) (*Result, error) {
6062
}
6163

6264
case *ast.KeyValueExpr:
63-
if hasPointerKeyWithoutPointerGetter(c.info, x.Key, x.Value) {
64-
c.filter.AddPos(x.Value.Pos())
65+
if se, ok := x.Value.(*ast.SelectorExpr); ok {
66+
if hasPointerKeyWithoutPointerGetter(c.info, x.Key, se) {
67+
c.filter.AddPos(se.Sel.Pos())
68+
}
6569
}
6670

6771
case *ast.CallExpr:
@@ -188,8 +192,11 @@ func (c *processor) processInner(expr ast.Expr) {
188192
c.processInner(x.X)
189193
c.write(".")
190194

195+
// Skip if the field is filtered.
196+
isFiltered := c.filter.IsFiltered(x.Sel.Pos())
197+
191198
// If getter exists, use it.
192-
if methodIsExists(c.info, x.X, "Get"+x.Sel.Name) {
199+
if methodIsExists(c.info, x.X, "Get"+x.Sel.Name) && !isFiltered {
193200
c.writeFrom(x.Sel.Name)
194201
c.writeTo("Get" + x.Sel.Name + "()")
195202
return
@@ -362,18 +369,13 @@ func getterResultHasPointer(info *types.Info, x ast.Expr, name string) (hasPoint
362369
return false, false
363370
}
364371

365-
func hasPointerKeyWithoutPointerGetter(info *types.Info, key ast.Expr, value ast.Expr) bool {
372+
func hasPointerKeyWithoutPointerGetter(info *types.Info, key ast.Expr, value *ast.SelectorExpr) bool {
366373
_, isPtr := info.TypeOf(key).(*types.Pointer)
367374
if !isPtr {
368375
return false
369376
}
370377

371-
se, ok := value.(*ast.SelectorExpr)
372-
if !ok {
373-
return false
374-
}
375-
376-
getterHasPointer, ok := getterResultHasPointer(info, se.X, se.Sel.Name)
378+
getterHasPointer, ok := getterResultHasPointer(info, value.X, value.Sel.Name)
377379
if !ok {
378380
return false
379381
}

testdata/proto/test.pb.go

+115-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/proto/test.proto

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ message Test {
3131
message Embedded {
3232
string s = 1;
3333
Embedded embedded = 2;
34+
optional bool opt_bool = 3;
3435
}
3536

3637
service Testing {

testdata/test.go

+16
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ func testInvalid(t *proto.Test) {
107107
_ = many[slicesIndexFunc(many, func(m *proto.Test) bool {
108108
return strings.Contains(strings.ToLower(m.GetS()), "specific value")
109109
})].GetS()
110+
111+
// issue #11
112+
var optBoolVar *bool
113+
optBoolVar = t.Embedded.OptBool // want `avoid direct access to proto field t\.Embedded\.OptBool, use t\.GetEmbedded\(\)\.OptBool instead`
114+
_ = optBoolVar
115+
116+
type structWithPtrField struct {
117+
OptBool *bool
118+
}
119+
_ = structWithPtrField{
120+
OptBool: t.Embedded.OptBool, // want `avoid direct access to proto field t\.Embedded\.OptBool, use t\.GetEmbedded\(\)\.OptBool instead`
121+
}
110122
}
111123

112124
func testValid(t *proto.Test) {
@@ -195,6 +207,7 @@ func testValid(t *proto.Test) {
195207
// issue #11
196208
var optBoolVar *bool
197209
optBoolVar = t.OptBool
210+
optBoolVar = t.GetEmbedded().OptBool
198211
_ = optBoolVar
199212

200213
type structWithPtrField struct {
@@ -203,6 +216,9 @@ func testValid(t *proto.Test) {
203216
_ = structWithPtrField{
204217
OptBool: t.OptBool,
205218
}
219+
_ = structWithPtrField{
220+
OptBool: t.GetEmbedded().OptBool,
221+
}
206222
}
207223

208224
// stubs

testdata/test.go.golden

+16
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ func testInvalid(t *proto.Test) {
108108
_ = many[slicesIndexFunc(many, func(m *proto.Test) bool {
109109
return strings.Contains(strings.ToLower(m.GetS()), "specific value")
110110
})].GetS()
111+
112+
// issue #11
113+
var optBoolVar *bool
114+
optBoolVar = t.GetEmbedded().OptBool // want `avoid direct access to proto field t\.Embedded\.OptBool, use t\.GetEmbedded\(\)\.OptBool instead`
115+
_ = optBoolVar
116+
117+
type structWithPtrField struct {
118+
OptBool *bool
119+
}
120+
_ = structWithPtrField{
121+
OptBool: t.GetEmbedded().OptBool, // want `avoid direct access to proto field t\.Embedded\.OptBool, use t\.GetEmbedded\(\)\.OptBool instead`
122+
}
111123
}
112124

113125
func testValid(t *proto.Test) {
@@ -196,6 +208,7 @@ func testValid(t *proto.Test) {
196208
// issue #11
197209
var optBoolVar *bool
198210
optBoolVar = t.OptBool
211+
optBoolVar = t.GetEmbedded().OptBool
199212
_ = optBoolVar
200213

201214
type structWithPtrField struct {
@@ -204,6 +217,9 @@ func testValid(t *proto.Test) {
204217
_ = structWithPtrField{
205218
OptBool: t.OptBool,
206219
}
220+
_ = structWithPtrField{
221+
OptBool: t.GetEmbedded().OptBool,
222+
}
207223
}
208224

209225
// stubs

0 commit comments

Comments
 (0)