Skip to content

Commit 14ae6f5

Browse files
committed
fix false positive when assigning an optional value to a field with a pointer type #11
1 parent 17962b7 commit 14ae6f5

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

processor.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ func (c *processor) process(n ast.Node) (*Result, error) {
3333
switch x := n.(type) {
3434
case *ast.AssignStmt:
3535
// Skip any assignment to the field.
36-
for _, s := range x.Lhs {
36+
for i, s := range x.Lhs {
3737
c.filter.AddPos(s.Pos())
3838

3939
if se, ok := s.(*ast.StarExpr); ok {
4040
c.filter.AddPos(se.X.Pos())
4141
}
42+
43+
if len(x.Rhs) > i {
44+
value := x.Rhs[i]
45+
if hasPointerKeyWithoutPointerGetter(c.info, s, value) {
46+
c.filter.AddPos(value.Pos())
47+
}
48+
}
4249
}
4350

4451
case *ast.IncDecStmt:
@@ -52,6 +59,11 @@ func (c *processor) process(n ast.Node) (*Result, error) {
5259
c.filter.AddPos(x.X.Pos())
5360
}
5461

62+
case *ast.KeyValueExpr:
63+
if hasPointerKeyWithoutPointerGetter(c.info, x.Key, x.Value) {
64+
c.filter.AddPos(x.Value.Pos())
65+
}
66+
5567
case *ast.CallExpr:
5668
if !c.cfg.ReplaceFirstArgInAppend && len(x.Args) > 0 {
5769
if v, ok := x.Fun.(*ast.Ident); ok && v.Name == "append" {
@@ -349,3 +361,22 @@ func getterResultHasPointer(info *types.Info, x ast.Expr, name string) (hasPoint
349361

350362
return false, false
351363
}
364+
365+
func hasPointerKeyWithoutPointerGetter(info *types.Info, key ast.Expr, value ast.Expr) bool {
366+
_, isPtr := info.TypeOf(key).(*types.Pointer)
367+
if !isPtr {
368+
return false
369+
}
370+
371+
se, ok := value.(*ast.SelectorExpr)
372+
if !ok {
373+
return false
374+
}
375+
376+
getterHasPointer, ok := getterResultHasPointer(info, se.X, se.Sel.Name)
377+
if !ok {
378+
return false
379+
}
380+
381+
return !getterHasPointer
382+
}

protogetter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func Run(pass *analysis.Pass, cfg *Config) error {
9696
(*ast.StarExpr)(nil),
9797
(*ast.IncDecStmt)(nil),
9898
(*ast.UnaryExpr)(nil),
99+
(*ast.KeyValueExpr)(nil),
99100
}
100101

101102
// Skip filtered files.

testdata/test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ func testValid(t *proto.Test) {
191191
// issue #12
192192
data := make([]byte, 4)
193193
_, _ = t.MyMarshal(data[4:])
194+
195+
// issue #11
196+
var optBoolVar *bool
197+
optBoolVar = t.OptBool
198+
_ = optBoolVar
199+
200+
type structWithPtrField struct {
201+
OptBool *bool
202+
}
203+
_ = structWithPtrField{
204+
OptBool: t.OptBool,
205+
}
194206
}
195207

196208
// stubs

testdata/test.go.golden

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,18 @@ func testValid(t *proto.Test) {
192192
// issue #12
193193
data := make([]byte, 4)
194194
_, _ = t.MyMarshal(data[4:])
195+
196+
// issue #11
197+
var optBoolVar *bool
198+
optBoolVar = t.OptBool
199+
_ = optBoolVar
200+
201+
type structWithPtrField struct {
202+
OptBool *bool
203+
}
204+
_ = structWithPtrField{
205+
OptBool: t.OptBool,
206+
}
195207
}
196208

197209
// stubs

0 commit comments

Comments
 (0)