[#16] noliteral: Fix nested functions

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2024-02-12 17:13:12 +03:00
parent 2c4e1d8b53
commit 7b767571bc
3 changed files with 47 additions and 5 deletions

View file

@ -2,6 +2,7 @@ package noliteral
import (
"go/ast"
"go/token"
astutils "git.frostfs.info/TrueCloudLab/linters/pkg/ast-utils"
"github.com/mitchellh/mapstructure"
@ -51,7 +52,17 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
switch n.(type) {
switch v := n.(type) {
// a := zap.Error()
case *ast.AssignStmt:
if _, ok := v.Rhs[0].(*ast.CallExpr); ok {
return false
}
// a := &log.Error()
case *ast.UnaryExpr:
if _, ok := v.X.(*ast.CallExpr); ok && v.Op == token.AND {
return false
}
// log.Error()
case *ast.CallExpr:
if expr, ok := n.(*ast.CallExpr); ok {
@ -69,18 +80,18 @@ func analyzeCallExpr(pass *analysis.Pass, expr *ast.CallExpr, file *ast.File) bo
isLog, _ := astutils.IsTargetMethod(expr.Fun, Config.TargetMethods)
if !isLog || len(expr.Args) == 0 || astutils.HasNoLintComment(pass, expr.Pos()) {
return true
return false
}
if !astutils.IsStringValue(expr.Args[0]) {
alias, _ := astutils.GetAliasByPkgName(file, Config.ConstantsPackage)
if Config.ConstantsPackage == "" || astutils.GetPackageName(expr.Args[0]) == alias || astutils.GetPackageName(expr.Args[0]) == "" {
return true
return false
}
for _, pkgName := range Config.DisablePackages {
if pkgName == astutils.GetPackageName(expr.Args[0]) {
return true
return false
}
}
@ -91,7 +102,7 @@ func analyzeCallExpr(pass *analysis.Pass, expr *ast.CallExpr, file *ast.File) bo
Message: "Wrong package for constants",
SuggestedFixes: nil,
})
return true
return false
}
pass.Report(analysis.Diagnostic{

View file

@ -23,6 +23,27 @@ func (c *cfg) custom_ok_const() {
c.log.Abyr(tochno_ne_dly_const_log.MSG)
}
// issue: https://git.frostfs.info/TrueCloudLab/linters/issues/16.
// Check that the check does not trigger a false positive for "zap.Error(invalid_pkg.Error)" passed as an argument.
func (c *cfg) debug1_ok() {
c.log.Debug(tochno_ne_dly_const_log.MSG, zap.Stringer("cid", bkt.CID), zap.String("oid", obj.VersionID), zap.Error(invalid_pkg.Error))
}
func (c *cfg) error_ok() {
field:=zap.Error(invalid_pkg.Error)
}
func (c *cfg) error_ok() {
callBack:=&zap.Error(invalid_pkg.Error)
}
func (c *cfg) error_ok() {
field=zap.Error(invalid_pkg.Error)
}
func (c *cfg) error_ok() {
callBack=&zap.Error(invalid_pkg.Error)
}
type Logger interface {
Debug(msg string)

View file

@ -15,6 +15,16 @@ func (c *cfg) debug_ok() {
c.log.Debug(logs.MSG) //acceptable
}
// issue: https://git.frostfs.info/TrueCloudLab/linters/issues/16.
// Check that the check does not trigger a false positive for "zap.Error("literal")" passed as an argument.
func (c *cfg) debug1_ok() {
c.log.Debug(logs.MSG, zap.Stringer("cid", bkt.CID), zap.String("oid", obj.VersionID), zap.Error("literal"))
}
func (c *cfg) debug1_ok() {
f(zap.Error("logs.MSG"))
}
func (c *cfg) error_ok() {
c.log.Error(logs.MSG) //acceptable
}