From 3a0ea5176b62d7c91f99c098fefef93e4fd46ee7 Mon Sep 17 00:00:00 2001 From: nobishino Date: Thu, 7 Mar 2024 22:27:03 +0900 Subject: [PATCH] Refactor: use standard function to check if a file is generated As of Go1.21, we can use https://pkg.go.dev/go/ast#IsGenerated to check if a file is generated. Probably we want to use this instead of own implementation. --- analyzer.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/analyzer.go b/analyzer.go index 0b1225b..17f2009 100644 --- a/analyzer.go +++ b/analyzer.go @@ -376,7 +376,7 @@ func (gosec *Analyzer) CheckRules(pkg *packages.Package) { if filepath.Ext(checkedFile) != ".go" { continue } - if gosec.excludeGenerated && isGeneratedFile(file) { + if gosec.excludeGenerated && ast.IsGenerated(file) { gosec.logger.Println("Ignoring generated file:", checkedFile) continue } @@ -459,7 +459,7 @@ func (gosec *Analyzer) CheckAnalyzers(pkg *packages.Package) { func (gosec *Analyzer) generatedFiles(pkg *packages.Package) map[string]bool { generatedFiles := map[string]bool{} for _, file := range pkg.Syntax { - if isGeneratedFile(file) { + if ast.IsGenerated(file) { fp := pkg.Fset.File(file.Pos()) if fp == nil { // skip files which cannot be located @@ -500,17 +500,6 @@ func (gosec *Analyzer) buildSSA(pkg *packages.Package) (interface{}, error) { return ssaPass.Analyzer.Run(ssaPass) } -func isGeneratedFile(file *ast.File) bool { - for _, comment := range file.Comments { - for _, row := range comment.List { - if generatedCodePattern.MatchString(row.Text) { - return true - } - } - } - return false -} - // ParseErrors parses the errors from given package func (gosec *Analyzer) ParseErrors(pkg *packages.Package) error { if len(pkg.Errors) == 0 {