mattermost/tools/mattermost-govet/pointerToSlice/pointerToSlice.go
Jesse Hallam 4d20645a5b
Inline mattermost-govet into the monorepo (#35869)
* inline mattermost-govet

* fix style issues

* simplify the openApiSync spec test

* README.md tweaks

* fix missing licenses

* simplify README.md

* trigger server-ci on tools/mattermost-govet/**

* Apply 470cf78253

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2026-04-01 13:24:22 +00:00

89 lines
1.9 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package pointerToSlice
import (
"go/ast"
"strings"
"golang.org/x/tools/go/analysis"
)
var (
Analyzer = &analysis.Analyzer{
Name: "pointerToSlice",
Doc: "check for usage of pointer to slice in function definitions",
Run: run,
}
ignoreFilesPattern string
)
func init() {
Analyzer.Flags.StringVar(&ignoreFilesPattern, "ignore", "", "Comma separated list of files to ignore")
}
func run(pass *analysis.Pass) (interface{}, error) {
var ignoreFiles []string
if ignoreFilesPattern != "" {
ignoreFiles = strings.Split(ignoreFilesPattern, ",")
}
checkNode := func(expr ast.Expr) {
sexpr, ok := expr.(*ast.StarExpr)
if !ok {
return
}
if _, ok := sexpr.X.(*ast.ArrayType); ok {
pass.Reportf(sexpr.Pos(), "use of pointer to slice in function definition")
return
}
if tv, ok := pass.TypesInfo.Types[sexpr.X]; ok {
if strings.HasPrefix(tv.Type.String(), "[]") || strings.HasPrefix(tv.Type.Underlying().String(), "[]") {
pass.Reportf(sexpr.Pos(), "use of pointer to slice in function definition")
return
}
}
}
for _, file := range pass.Files {
ast.Inspect(file, func(node ast.Node) bool {
if node != nil {
f := pass.Fset.File(node.Pos())
if isIgnore(f.Name(), ignoreFiles) {
return false
}
}
f, ok := node.(*ast.FuncDecl)
if !ok || f.Type == nil {
return true
}
if params := f.Type.Params; params != nil {
for _, p := range params.List {
checkNode(p.Type)
}
}
if results := f.Type.Results; results != nil {
for _, r := range results.List {
checkNode(r.Type)
}
}
return true
})
}
return nil, nil
}
func isIgnore(file string, ignoreFiles []string) bool {
for _, f := range ignoreFiles {
if strings.HasSuffix(file, f) {
return true
}
}
return false
}