From d0450b63015359a704073c6d85559082e6f54d92 Mon Sep 17 00:00:00 2001 From: Alexander Chuprov Date: Thu, 17 Aug 2023 15:28:14 +0300 Subject: [PATCH] [#10] linters: Add support for the nolint comment Signed-off-by: Alexander Chuprov --- internal/analyzers/noliteral/linter.go | 2 +- internal/analyzers/use-strconv/linter.go | 2 +- pkg/ast-utils/ast.go | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/internal/analyzers/noliteral/linter.go b/internal/analyzers/noliteral/linter.go index aa453b8..107613d 100644 --- a/internal/analyzers/noliteral/linter.go +++ b/internal/analyzers/noliteral/linter.go @@ -57,7 +57,7 @@ func run(pass *analysis.Pass) (interface{}, error) { } isLog, _ := astutils.IsTargetMethod(expr.Fun, Config.TargetMethods) - if !isLog || len(expr.Args) == 0 { + if !isLog || len(expr.Args) == 0 || astutils.HasNoLintComment(pass, expr.Pos()) { return true } diff --git a/internal/analyzers/use-strconv/linter.go b/internal/analyzers/use-strconv/linter.go index fe6ca1f..304d822 100644 --- a/internal/analyzers/use-strconv/linter.go +++ b/internal/analyzers/use-strconv/linter.go @@ -71,7 +71,7 @@ func run(pass *analysis.Pass) (interface{}, error) { stringLiteral, ok := expr.Args[0].(*ast.BasicLit) - if !ok { + if !ok || astutils.HasNoLintComment(pass, expr.Pos()) { return true } diff --git a/pkg/ast-utils/ast.go b/pkg/ast-utils/ast.go index fcb379e..df22a59 100644 --- a/pkg/ast-utils/ast.go +++ b/pkg/ast-utils/ast.go @@ -5,6 +5,8 @@ import ( "go/token" "strings" "sync" + + "golang.org/x/tools/go/analysis" ) type aliasCacheKey struct { @@ -94,3 +96,16 @@ func GetPackageName(expr ast.Expr) string { } return "" } + +func HasNoLintComment(pass *analysis.Pass, pos token.Pos) bool { + for _, commentGroup := range pass.Files[0].Comments { + if commentGroup.End() < pos { + for _, comment := range commentGroup.List { + if strings.Contains(comment.Text, "nolint") { + return true + } + } + } + } + return false +}