Skip to content

Commit d6446b9

Browse files
committed
Add *ast.KeyValueExpr as a source for RHS varaibles
Key-value expressions can use variables both as keys and values so we must parse them to check for assigned and used variables. Fixes #106
1 parent bbddf87 commit d6446b9

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

wsl.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,7 @@ func (p *Processor) findRHS(node ast.Node) []string {
845845
case *ast.BasicLit, *ast.SelectStmt, *ast.ChanType,
846846
*ast.LabeledStmt, *ast.DeclStmt, *ast.BranchStmt,
847847
*ast.TypeSpec, *ast.ArrayType, *ast.CaseClause,
848-
*ast.CommClause, *ast.KeyValueExpr, *ast.MapType,
849-
*ast.FuncLit:
848+
*ast.CommClause, *ast.MapType, *ast.FuncLit:
850849
// Nothing to add to RHS
851850
case *ast.Ident:
852851
return []string{t.Name}
@@ -905,6 +904,9 @@ func (p *Processor) findRHS(node ast.Node) []string {
905904
rhs = append(rhs, p.findRHS(t.X)...)
906905
rhs = append(rhs, p.findRHS(t.Low)...)
907906
rhs = append(rhs, p.findRHS(t.High)...)
907+
case *ast.KeyValueExpr:
908+
rhs = p.findRHS(t.Key)
909+
rhs = append(rhs, p.findRHS(t.Value)...)
908910
default:
909911
if x, ok := maybeX(t); ok {
910912
return p.findRHS(x)

wsl_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,19 @@ func TestWithConfig(t *testing.T) {
19211921
reasonShortDeclNotExclusive,
19221922
},
19231923
},
1924+
{
1925+
description: "key value pairs can use variables",
1926+
code: []byte(`package main
1927+
1928+
func main() {
1929+
someData := GetSomeData()
1930+
log.WithFields(log.Fields{
1931+
"data1": someData.One,
1932+
"data2": someData.Two,
1933+
"data3": someData.Three,
1934+
}).Debug("Got some data")
1935+
}`),
1936+
},
19241937
}
19251938

19261939
for _, tc := range cases {

0 commit comments

Comments
 (0)