Skip to content

Validate gocritic settings. Return error if settings includes a unsupported gocritic checker #1563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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