mattermost/tools/mattermost-govet/emptyInterface/emptyInterface.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

55 lines
1.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package emptyInterface
import (
"go/ast"
"strings"
"golang.org/x/tools/go/analysis"
)
var Analyzer = &analysis.Analyzer{
Name: "emptyInterface",
Doc: "check for usage of interface{} instead of any",
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
checkList := []string{
"Regenerate this file using `make plugin-mocks`.",
"Regenerate this file using `make store-mocks`.",
"Regenerate this file using `make sharedchannel-mocks`.",
}
for _, file := range pass.Files {
comments := ""
for _, commentGroup := range file.Comments {
for _, comment := range commentGroup.List {
comments = comments + comment.Text
}
}
skip := false
for _, check := range checkList {
if strings.Contains(comments, check) {
skip = true
}
}
if skip {
continue
}
ast.Inspect(file, func(node ast.Node) bool {
switch x := node.(type) {
case *ast.InterfaceType:
if len(x.Methods.List) == 0 {
pass.Reportf(x.Pos(), "using 'interface{}', please replace it with 'any'.")
}
return true
}
return true
})
}
return nil, nil
}