Skip to content

Commit

Permalink
Refactor: use standard function to check if a file is generated
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nobishino authored and ccojocar committed Mar 8, 2024
1 parent 11c3252 commit 3a0ea51
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3a0ea51

Please sign in to comment.