mirror of
https://github.com/securego/gosec.git
synced 2024-12-24 11:35:52 +00:00
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.
This commit is contained in:
parent
11c32522c5
commit
3a0ea5176b
1 changed files with 2 additions and 13 deletions
15
analyzer.go
15
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 {
|
||||
|
|
Loading…
Reference in a new issue