diff --git a/config.toml b/config.toml index 6b4e27e..0afb8e6 100644 --- a/config.toml +++ b/config.toml @@ -1,7 +1,10 @@ severity = "warning" confidence = 0.8 -[rules.else] -[rules.names] -[rules.argument-limit] - arguments = ['2'] +[rule.else] +[rule.names] +[rule.argument-limit] + arguments = [2] +[rule.cyclomatic] + arguments = [3] + severity = "error" \ No newline at end of file diff --git a/lint/rule.go b/lint/rule.go index f41381c..eb42b42 100644 --- a/lint/rule.go +++ b/lint/rule.go @@ -47,7 +47,7 @@ type DisabledInterval struct { } // Arguments is type used for the arguments of a rule. -type Arguments = []string +type Arguments = []interface{} // RuleConfig is type used for the rule configuration. type RuleConfig struct { @@ -62,7 +62,7 @@ type RulesConfig = map[string]RuleConfig type Config struct { Confidence float64 Severity Severity - Rules RulesConfig + Rules RulesConfig `toml:"rule"` } // Rule defines an abstract rule interaface diff --git a/main.go b/main.go index 294774a..03ff34c 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,7 @@ var allRules = []lint.Rule{ &rule.TimeNamesRule{}, &rule.ContextKeyTypeRule{}, &rule.ContextArgumentsRule{}, + &rule.CyclomaticRule{}, } func getLintingRules(config *lint.Config) []lint.Rule { @@ -82,7 +83,7 @@ func main() { package p func Test() { - if true { + if true || bar && baz { return 42; } else { return 23; diff --git a/rule/argument-limit.go b/rule/argument-limit.go index 45672dd..01b1383 100644 --- a/rule/argument-limit.go +++ b/rule/argument-limit.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "go/ast" - "strconv" "github.com/mgechev/revive/lint" ) @@ -16,15 +15,16 @@ func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) [] if len(arguments) != 1 { panic(`invalid configuration for "argument-limit"`) } - total, err := strconv.ParseInt(arguments[0], 10, 32) - if err != nil { - panic(`invalid configuration for "argument-limit"`) + + total, ok := arguments[0].(int64) // Alt. non panicking version + if !ok { + panic(`invalid value passed as argument number to the "argument-list" rule`) } var failures []lint.Failure walker := lintArgsNum{ - total: total, + total: int(total), onFailure: func(failure lint.Failure) { failures = append(failures, failure) }, @@ -41,14 +41,14 @@ func (r *ArgumentsLimitRule) Name() string { } type lintArgsNum struct { - total int64 + total int onFailure func(lint.Failure) } func (w lintArgsNum) Visit(n ast.Node) ast.Visitor { node, ok := n.(*ast.FuncDecl) if ok { - num := int64(len(node.Type.Params.List)) + num := len(node.Type.Params.List) if num > w.total { w.onFailure(lint.Failure{ Confidence: 1, diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 6e34a88..b7e9c0f 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "go/token" - "strconv" "github.com/mgechev/revive/lint" ) @@ -18,15 +17,15 @@ type CyclomaticRule struct{} func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var failures []lint.Failure - complexity, err := strconv.Atoi(arguments[0]) - if err != nil { + complexity, ok := arguments[0].(int64) // Alt. non panicking version + if !ok { panic("invalid argument for cyclomatic complexity") } fileAst := file.AST walker := lintCyclomatic{ file: file, - complexity: complexity, + complexity: int(complexity), onFailure: func(failure lint.Failure) { failures = append(failures, failure) }, @@ -39,7 +38,7 @@ func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint // Name returns the rule name. func (r *CyclomaticRule) Name() string { - return "errorf" + return "cyclomatic" } type lintCyclomatic struct {