@@ -29,7 +29,7 @@ func (r *ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fai
29
29
}
30
30
31
31
receiverName := receiver .Names [0 ].Name
32
- assignmentsToReceiver := r .getAssignmentsToReceiver (receiverName , funcDecl .Body )
32
+ assignmentsToReceiver := r .getReceiverModifications (receiverName , funcDecl .Body )
33
33
if len (assignmentsToReceiver ) == 0 {
34
34
continue // receiver is not modified
35
35
}
@@ -140,38 +140,44 @@ func (r *ModifiesValRecRule) mustSkip(receiver *ast.Field, pkg *lint.Package) bo
140
140
return false
141
141
}
142
142
143
- func (r * ModifiesValRecRule ) getAssignmentsToReceiver (receiverName string , funcBody * ast.BlockStmt ) []ast.Node {
144
- receiverAssignmentFinder := func (n ast.Node ) bool {
145
- // look for assignments with the receiver in the right hand
146
- assignment , ok := n .(* ast.AssignStmt )
147
- if ! ok {
148
- return false
149
- }
143
+ func (r * ModifiesValRecRule ) getReceiverModifications (receiverName string , funcBody * ast.BlockStmt ) []ast.Node {
144
+ receiverModificationFinder := func (n ast.Node ) bool {
145
+ switch node := n .(type ) {
146
+ case * ast.IncDecStmt :
147
+ se , ok := node .X .(* ast.SelectorExpr )
148
+ if ! ok {
149
+ return false
150
+ }
150
151
151
- for _ , exp := range assignment .Lhs {
152
- switch e := exp .(type ) {
153
- case * ast.IndexExpr : // receiver...[] = ...
154
- continue
155
- case * ast.StarExpr : // *receiver = ...
156
- continue
157
- case * ast.SelectorExpr : // receiver.field = ...
158
- name := r .getNameFromExpr (e .X )
159
- if name == "" || name != receiverName {
152
+ name := r .getNameFromExpr (se .X )
153
+ return name == receiverName
154
+ case * ast.AssignStmt :
155
+ // look for assignments with the receiver in the right hand
156
+ for _ , exp := range node .Lhs {
157
+ switch e := exp .(type ) {
158
+ case * ast.IndexExpr : // receiver...[] = ...
160
159
continue
161
- }
162
- case * ast.Ident : // receiver := ...
163
- if e .Name != receiverName {
160
+ case * ast.StarExpr : // *receiver = ...
161
+ continue
162
+ case * ast.SelectorExpr : // receiver.field = ...
163
+ name := r .getNameFromExpr (e .X )
164
+ if name == "" || name != receiverName {
165
+ continue
166
+ }
167
+ case * ast.Ident : // receiver := ...
168
+ if e .Name != receiverName {
169
+ continue
170
+ }
171
+ default :
164
172
continue
165
173
}
166
- default :
167
- continue
168
- }
169
174
170
- return true
175
+ return true
176
+ }
171
177
}
172
178
173
179
return false
174
180
}
175
181
176
- return pick (funcBody , receiverAssignmentFinder )
182
+ return pick (funcBody , receiverModificationFinder )
177
183
}
0 commit comments