Skip to content

perf: Optimize code with minimal fields #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,29 @@ func run(pass *analysis.Pass) (any, error) {
return
}

var st *structType
st, ok = structs[recv.Name]
st, ok := structs[recv.Name]
if !ok {
structs[recv.Name] = &structType{recv: recv.Name}
structs[recv.Name] = &structType{}
st = structs[recv.Name]
}

if isStar {
st.numStarMethod++
st.starUsed = true
} else {
st.numTypeMethod++
st.typeUsed = true
}
})

for _, st := range structs {
if st.numStarMethod > 0 && st.numTypeMethod > 0 {
pass.Reportf(pass.Pkg.Scope().Lookup(st.recv).Pos(), "the methods of %q use pointer receiver and non-pointer receiver.", st.recv)
for recv, st := range structs {
if st.starUsed && st.typeUsed {
pass.Reportf(pass.Pkg.Scope().Lookup(recv).Pos(), "the methods of %q use pointer receiver and non-pointer receiver.", recv)
}
}

return nil, nil
}

type structType struct {
recv string
numStarMethod int
numTypeMethod int
starUsed bool
typeUsed bool
}