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

Commit

Permalink
Allow type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
esimonov committed Jan 27, 2021
1 parent f4c7e24 commit 113a74d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ifshort
[![Go Report Card](https://goreportcard.com/badge/github.com/esimonov/ifshort)](https://goreportcard.com/report/github.com/esimonov/ifshort)
<a href='https://github.com/jpoles1/gopherbadger' target='_blank'>![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-96%25-brightgreen.svg?longCache=true&style=flat)</a>
<a href='https://github.com/jpoles1/gopherbadger' target='_blank'>![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-97%25-brightgreen.svg?longCache=true&style=flat)</a>

Go linter that checks if your code uses short syntax for `if`-statements whenever possible.

Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ func (nom namedOccurrenceMap) checkExpression(candidate ast.Expr, ifPos token.Po
nom.checkExpression(v.High, ifPos)
nom.checkExpression(v.Low, ifPos)
nom.checkExpression(v.X, ifPos)
case *ast.TypeAssertExpr:
nom.checkExpression(v.X, ifPos)
case *ast.UnaryExpr:
nom.checkExpression(v.X, ifPos)
}
Expand Down
4 changes: 2 additions & 2 deletions 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 || isAssignmentToPointer(ident.Obj.Decl) {
if ident.Name == "_" || ident.Obj == nil || isUnshortenableAssignment(ident.Obj.Decl) {
continue
}

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

func isAssignmentToPointer(decl interface{}) bool {
func isUnshortenableAssignment(decl interface{}) bool {
assign, ok := decl.(*ast.AssignStmt)
if !ok {
return false
Expand Down
12 changes: 12 additions & 0 deletions testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,15 @@ func notUsed_AssignmentToPointer_OK() {
return
}
}

func notUsed_TypeAssertion_OK() {
v := getValue()
if v == nil {
noOp1(v)
}

w, ok := v.(*dummyType)
if !ok {
noOp2(w)
}
}

0 comments on commit 113a74d

Please sign in to comment.