Skip to content

Commit

Permalink
Merge pull request #4 from breml/add-flag-for-report-no-exported
Browse files Browse the repository at this point in the history
Add flag report-no-exported
  • Loading branch information
breml committed Nov 8, 2021
2 parents f5f3668 + 4464b1c commit 817869a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 11 additions & 9 deletions errchkjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

type errchkjson struct {
omitSafe bool // -omit-safe flag
omitSafe bool // -omit-safe flag
reportNoExported bool // -report-no-exported flag
}

// NewAnalyzer returns a new errchkjson analyzer.
Expand All @@ -30,6 +31,7 @@ func NewAnalyzer() *analysis.Analyzer {

a.Flags.Init("errchkjson", flag.ExitOnError)
a.Flags.BoolVar(&errchkjson.omitSafe, "omit-safe", false, "if omit-safe is true, checking of safe returns is omitted")
a.Flags.BoolVar(&errchkjson.reportNoExported, "report-no-exported", false, "if report-no-exported is true, encoding a struct without exported fields is reported as issue")

return a
}
Expand Down Expand Up @@ -112,7 +114,7 @@ func (e *errchkjson) handleJSONMarshal(pass *analysis.Pass, ce *ast.CallExpr, fn
t = t.(*types.Pointer).Elem()
}

err := jsonSafe(t, 0)
err := e.jsonSafe(t, 0)
if err != nil {
if _, ok := err.(unsupported); ok {
pass.Reportf(ce.Pos(), "`%s` for %v", fnName, err)
Expand Down Expand Up @@ -141,7 +143,7 @@ const (
unsupportedBasicTypes = types.IsComplex
)

func jsonSafe(t types.Type, level int) error {
func (e *errchkjson) jsonSafe(t types.Type, level int) error {
if types.Implements(t, textMarshalerInterface()) {
return fmt.Errorf("unsafe type `%s` found", t.String())
}
Expand All @@ -168,14 +170,14 @@ func jsonSafe(t types.Type, level int) error {
}

case *types.Array:
err := jsonSafe(ut.Elem(), level+1)
err := e.jsonSafe(ut.Elem(), level+1)
if err != nil {
return err
}
return nil

case *types.Slice:
err := jsonSafe(ut.Elem(), level+1)
err := e.jsonSafe(ut.Elem(), level+1)
if err != nil {
return err
}
Expand All @@ -194,19 +196,19 @@ func jsonSafe(t types.Type, level int) error {
continue
}
}
err := jsonSafe(ut.Field(i).Type(), level+1)
err := e.jsonSafe(ut.Field(i).Type(), level+1)
if err != nil {
return err
}
exported++
}
if level == 0 && exported == 0 {
if e.reportNoExported && level == 0 && exported == 0 {
return newNoexportedError(fmt.Errorf("struct does not export any field"))
}
return nil

case *types.Pointer:
err := jsonSafe(ut.Elem(), level+1)
err := e.jsonSafe(ut.Elem(), level+1)
if err != nil {
return err
}
Expand All @@ -217,7 +219,7 @@ func jsonSafe(t types.Type, level int) error {
if err != nil {
return err
}
err = jsonSafe(ut.Elem(), level+1)
err = e.jsonSafe(ut.Elem(), level+1)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions errchkjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func TestNoExportedField(t *testing.T) {
if err != nil {
t.Fatalf("error setting 'omit-safe' command line flag: %v", err)
}
err = errchkjson.Flags.Set("report-no-exported", "true")
if err != nil {
t.Fatalf("error setting 'report-no-exported' command line flag: %v", err)
}
testdata := analysistest.TestData()
analysistest.Run(t, testdata, errchkjson, "noexport")
}

0 comments on commit 817869a

Please sign in to comment.