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

Commit 113a74d

Browse files
committed
Allow type assertions
1 parent f4c7e24 commit 113a74d

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ifshort
22
[![Go Report Card](https://goreportcard.com/badge/github.com/esimonov/ifshort)](https://goreportcard.com/report/github.com/esimonov/ifshort)
3-
<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>
3+
<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>
44

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

pkg/analyzer/analyzer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ func (nom namedOccurrenceMap) checkExpression(candidate ast.Expr, ifPos token.Po
229229
nom.checkExpression(v.High, ifPos)
230230
nom.checkExpression(v.Low, ifPos)
231231
nom.checkExpression(v.X, ifPos)
232+
case *ast.TypeAssertExpr:
233+
nom.checkExpression(v.X, ifPos)
232234
case *ast.UnaryExpr:
233235
nom.checkExpression(v.X, ifPos)
234236
}

pkg/analyzer/occurrences.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (nom namedOccurrenceMap) addFromAssignment(pass *analysis.Pass, assignment
125125
continue
126126
}
127127

128-
if ident.Name == "_" || ident.Obj == nil || isAssignmentToPointer(ident.Obj.Decl) {
128+
if ident.Name == "_" || ident.Obj == nil || isUnshortenableAssignment(ident.Obj.Decl) {
129129
continue
130130
}
131131

@@ -144,7 +144,7 @@ func (nom namedOccurrenceMap) addFromAssignment(pass *analysis.Pass, assignment
144144
}
145145
}
146146

147-
func isAssignmentToPointer(decl interface{}) bool {
147+
func isUnshortenableAssignment(decl interface{}) bool {
148148
assign, ok := decl.(*ast.AssignStmt)
149149
if !ok {
150150
return false

testdata/testdata.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,15 @@ func notUsed_AssignmentToPointer_OK() {
442442
return
443443
}
444444
}
445+
446+
func notUsed_TypeAssertion_OK() {
447+
v := getValue()
448+
if v == nil {
449+
noOp1(v)
450+
}
451+
452+
w, ok := v.(*dummyType)
453+
if !ok {
454+
noOp2(w)
455+
}
456+
}

0 commit comments

Comments
 (0)