fzipp / gocyclo

Calculate cyclomatic complexities of functions in Go source code.
BSD 3-Clause "New" or "Revised" License
1.33k stars 81 forks source link

Skip individual functions #14

Closed powerman closed 3 years ago

powerman commented 7 years ago

Background: https://github.com/alecthomas/gometalinter/issues/238

In short, when gocyclo is executed automatically (for ex. by gometalinter in CI) and it output is used to decide to pass or to fail automatic build we need more control than just single -over N value applied to all packages in current project.

Please add a feature to either completely skip gocyclo on per-function basis, or overwrite value of -over N argument on per-function basis. For example, check how another tool use annotations: https://github.com/GoASTScanner/gas#annotating-code

alecthomas commented 7 years ago

Note that the ability to suppress linter warnings has been implemented in gometalinter.

MrJoy commented 5 years ago

Some of us aren't using gometalinter, and still have need of this ability.

func (config *GCPCredentials) IsValid() bool {
    if config == nil {
        return false
    }

    missingRequiredOptions := config.Type == "" ||
        config.ProjectID == "" ||
        config.PrivateKeyID == "" ||
        config.PrivateKey == "" ||
        config.ClientEmail == "" ||
        config.ClientID == "" ||
        config.AuthURI == "" ||
        config.TokenURI == "" ||
        config.AuthProviderX509CertURL == "" ||
        config.ClientX509CertURL == ""
    return !missingRequiredOptions
}

This has a cyclomatic complexity of 11. I have my lint task configured for a limit of 10, because in general that's a limit that works for my team. In this specific case, the cyclomatic complexity number is a bit misleading, and any attempt to break the function up to put it under the limit is just gonna add needless complexity and indirection.

the-maldridge commented 4 years ago

I just got bit by this on a high complexity test driver which was quite surprising. I really didn't expect a cyclo check to even look at test code, and then to not be able to silence the warning is a bit annoying.