Skip to content

Commit

Permalink
Validate gocritic settings. Return error if settings includes a unsup…
Browse files Browse the repository at this point in the history
…ported gocritic checker (#1563)
  • Loading branch information
sebastien-rosset authored Jan 4, 2021
1 parent cfbbead commit 62710a8
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions pkg/config/config_gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,7 @@ func (s *GocriticSettings) Validate(log logutils.Log) error {
return errors.Wrap(err, "validate disabled checks")
}

for checkName := range s.SettingsPerCheck {
if !s.IsCheckEnabled(checkName) {
log.Warnf("Gocritic settings were provided for not enabled check %q", checkName)
}
}

if err := s.validateCheckerNames(); err != nil {
if err := s.validateCheckerNames(log); err != nil {
return errors.Wrap(err, "validation failed")
}

Expand All @@ -272,6 +266,7 @@ func sprintStrings(ss []string) string {
return fmt.Sprint(ss)
}

// getAllCheckerNames returns a map containing all checker names supported by gocritic.
func getAllCheckerNames() map[string]bool {
allCheckerNames := map[string]bool{}
for _, checker := range allGocriticCheckers {
Expand Down Expand Up @@ -311,7 +306,7 @@ func getDefaultDisabledGocriticCheckersNames() []string {
return disabled
}

func (s *GocriticSettings) validateCheckerNames() error {
func (s *GocriticSettings) validateCheckerNames(log logutils.Log) error {
allowedNames := getAllCheckerNames()

for _, name := range s.EnabledChecks {
Expand All @@ -328,6 +323,16 @@ func (s *GocriticSettings) validateCheckerNames() error {
}
}

for checkName := range s.SettingsPerCheck {
if _, ok := allowedNames[checkName]; !ok {
return fmt.Errorf("invalid setting, checker %s doesn't exist, all existing checkers: %s",
checkName, sprintAllowedCheckerNames(allowedNames))
}
if !s.IsCheckEnabled(checkName) {
log.Warnf("Gocritic settings were provided for not enabled check %q", checkName)
}
}

return nil
}

Expand Down

0 comments on commit 62710a8

Please sign in to comment.