Skip to content

Commit

Permalink
1. Added checking of casting initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
maranqz committed Nov 26, 2023
1 parent 73ee41a commit 1d949c7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 24 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ Linter doesn't catch some cases.
2. Local initialization, [example](testdata/src/factory/unimplemented/local/).
3. Named return. If you want to block that case, you can use [nonamedreturns](https://github.com/firefart/nonamedreturns) linter, [example](testdata/src/factory/unimplemented/named_return.go).
4. var declaration, `var initilized nested.Struct` gives structure without factory, [example](testdata/src/factory/unimplemented/var.go).
5. Casting to nested struct, [example](testdata/src/factory/unimplemented/casting/).

## TODO

Expand Down
16 changes: 16 additions & 0 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ func (v *visitor) walk(n ast.Node) {
}

func (v *visitor) Visit(node ast.Node) ast.Visitor {
// casting pkg.Struct(struct{}{})
callExpr, ok := node.(*ast.CallExpr)
if ok {
ident := v.getIdent(callExpr.Fun)
identObj := v.pass.TypesInfo.ObjectOf(ident)

_, isTypeName := identObj.(*types.TypeName)
if isTypeName {
if v.blockedStrategy.IsBlocked(v.pass.Pkg, identObj) {
v.report(ident, identObj)
}
}

return v
}

compLit, ok := node.(*ast.CompositeLit)
if !ok {
return v
Expand Down
1 change: 1 addition & 0 deletions lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestLinterSuite(t *testing.T) {
prepare func(t *testing.T, a *analysis.Analyzer)
}{
"simple": {pkgs: []string{"simple/..."}},
"casting": {pkgs: []string{"casting/..."}},
"generic": {pkgs: []string{"generic/..."}},
"packageGlobs": {
pkgs: []string{"packageGlobs/..."},
Expand Down
27 changes: 27 additions & 0 deletions testdata/src/factory/casting/cast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package casting

import (
"factory/casting/nested"
)

func ToNestedMyInt() {
_ = nested.MyInt(1) // want `Use factory for nested.MyInt`
}

type Struct struct {
Field int
}

func ToNestedStrut() {
l := Struct{
Field: 1,
}

_ = nested.Struct(l) // want `Use factory for nested.Struct`
}

func ToLocal() {
n := nested.NewStruct()

_ = Struct(n)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nested

type MyInt int

type Struct struct {
Field int
}
Expand Down
4 changes: 4 additions & 0 deletions testdata/src/factory/simple/nested/nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package nested

type Struct struct{}

func NewStruct() Struct {
return Struct{}
}

type Mp map[bool]bool

type Slice []bool
Expand Down
2 changes: 2 additions & 0 deletions testdata/src/factory/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func Local() {
func Nested() {
_ = nested.Struct{} // want `Use factory for nested.Struct`
_ = &nested.Struct{} // want `Use factory for nested.Struct`

_ = nested.NewStruct()
}

func CallMp() {
Expand Down
23 changes: 0 additions & 23 deletions testdata/src/factory/unimplemented/casting/cast.go

This file was deleted.

0 comments on commit 1d949c7

Please sign in to comment.