Add "encoding/json" to denylist (#18)

- "encoding/json" should be avoided as we got a json module for that which get the correct json library when specified.
- Allow to exemptions to the denylist (e.g. the json module need to import this package).

Reviewed-on: https://gitea.com/gitea/gitea-vet/pulls/18
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Gusted <williamzijl7@hotmail.com>
Co-committed-by: Gusted <williamzijl7@hotmail.com>
This commit is contained in:
Gusted 2022-01-22 23:17:48 +08:00 committed by Andrew Thornton
parent 791434a6a7
commit 48ebc90254
1 changed files with 13 additions and 2 deletions

View File

@ -11,7 +11,7 @@ import (
)
var (
deniedImports = []string{"io/ioutil"}
deniedImports = []string{"io/ioutil", "encoding/json"}
DenylistImports = &analysis.Analyzer{
Name: "denylist_imports",
Doc: "check for denied imports",
@ -27,7 +27,18 @@ func runDenylistImports(pass *analysis.Pass) (interface{}, error) {
val = strings.TrimSuffix(val, `"`)
for _, deniedImport := range deniedImports {
if deniedImport == val {
pass.Reportf(im.Path.Pos(), `"`+deniedImport+"\" is not allowed to be imported")
// Allow a exemption when there is a comment 'Allow "package_name" import'
allowed := false
for _, comment := range file.Comments {
if strings.Contains(comment.Text(), "Allow \""+val+"\" import") {
allowed = true
break
}
}
if !allowed {
pass.Reportf(im.Path.Pos(), `"`+deniedImport+"\" is not allowed to be imported")
}
}
}
}