Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
Fix issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
esimonov committed Jan 26, 2021
1 parent 64fdf4b commit f4c7e24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func (nom namedOccurrenceMap) checkStatement(stmt ast.Stmt, ifPos token.Pos) {

case *ast.RangeStmt:
nom.checkExpression(v.X, token.NoPos)
if v.Body != nil {
for _, e := range v.Body.List {
nom.checkStatement(e, ifPos)
}
}
case *ast.ReturnStmt:
for _, r := range v.Results {
nom.checkExpression(r, token.NoPos)
Expand Down
23 changes: 22 additions & 1 deletion pkg/analyzer/occurrences.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (nom namedOccurrenceMap) addFromAssignment(pass *analysis.Pass, assignment
continue
}

if ident.Name == "_" || ident.Obj == nil {
if ident.Name == "_" || ident.Obj == nil || isAssignmentToPointer(ident.Obj.Decl) {
continue
}

Expand All @@ -144,6 +144,27 @@ func (nom namedOccurrenceMap) addFromAssignment(pass *analysis.Pass, assignment
}
}

func isAssignmentToPointer(decl interface{}) bool {
assign, ok := decl.(*ast.AssignStmt)
if !ok {
return false
}

for _, el := range assign.Rhs {
u, ok := el.(*ast.UnaryExpr)
if !ok {
continue
}

if u.Op == token.AND {
if _, ok := u.X.(*ast.CompositeLit); ok {
return true
}
}
}
return false
}

func areFlagSettingsSatisfied(pass *analysis.Pass, assignment *ast.AssignStmt, i int) bool {
lh := assignment.Lhs[i]
rh := assignment.Rhs[len(assignment.Rhs)-1]
Expand Down
8 changes: 8 additions & 0 deletions testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,11 @@ func notUsed_FuncLitReturn_OK() {
return
}
}

func notUsed_AssignmentToPointer_OK() {
v := &dummyType{}

if v != nil { // cannot be `if v := &dummyType{}; v != nil
return
}
}

0 comments on commit f4c7e24

Please sign in to comment.