diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index ee880f9c1..850c70c43 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -16,3 +16,5 @@ jobs: - name: Run Revive Action uses: morphy2k/revive-action@v2 + with: + config: revive.toml diff --git a/lint/package.go b/lint/package.go index 441e0f473..2ab035f16 100644 --- a/lint/package.go +++ b/lint/package.go @@ -166,17 +166,17 @@ func (p *Package) scanSortable() { // bitfield for which methods exist on each type. const ( - Len = 1 << iota - Less - Swap + bfLen = 1 << iota + bfLess + bfSwap ) - nmap := map[string]int{"Len": Len, "Less": Less, "Swap": Swap} + nmap := map[string]int{"Len": bfLen, "Less": bfLess, "Swap": bfSwap} has := make(map[string]int) for _, f := range p.files { ast.Walk(&walker{nmap, has}, f.AST) } for typ, ms := range has { - if ms == Len|Less|Swap { + if ms == bfLen|bfLess|bfSwap { p.sortable[typ] = true } } diff --git a/revive.toml b/revive.toml new file mode 100644 index 000000000..3d47522b7 --- /dev/null +++ b/revive.toml @@ -0,0 +1,38 @@ +# This configuration for the revive linter used for linting the revive's codebase itself. +# See .github/workflows/lint.yaml. + +ignoreGeneratedHeader = false +severity = "warning" +confidence = 0.8 +errorCode = 1 +warningCode = 1 + +[rule.bare-return] +[rule.blank-imports] +[rule.context-as-argument] +[rule.context-keys-type] +[rule.dot-imports] +[rule.empty-block] +[rule.empty-lines] +[rule.error-naming] +[rule.error-return] +[rule.error-strings] +[rule.errorf] +[rule.exported] +[rule.increment-decrement] +[rule.indent-error-flow] +[rule.line-length-limit] + arguments = [200] +[rule.package-comments] +[rule.range] +[rule.receiver-naming] +[rule.redefines-builtin-id] +[rule.superfluous-else] +[rule.time-naming] +[rule.unexported-naming] +[rule.unexported-return] +[rule.unreachable-code] +[rule.unused-parameter] +[rule.useless-break] +[rule.var-declaration] +[rule.var-naming] diff --git a/rule/add-constant.go b/rule/add-constant.go index f7a2447be..233f1d848 100644 --- a/rule/add-constant.go +++ b/rule/add-constant.go @@ -160,14 +160,14 @@ func (w *lintAddConstantRule) isIgnoredFunc(fName string) bool { } func (w *lintAddConstantRule) checkStrLit(n *ast.BasicLit) { - const IgnoreMarker = -1 + const ignoreMarker = -1 if w.allowList[kindSTRING][n.Value] { return } count := w.strLits[n.Value] - mustCheck := count > IgnoreMarker + mustCheck := count > ignoreMarker if mustCheck { w.strLits[n.Value] = count + 1 if w.strLits[n.Value] > w.strLitLimit { diff --git a/rule/comments-density.go b/rule/comments-density.go index 5956fea23..c5298ea07 100644 --- a/rule/comments-density.go +++ b/rule/comments-density.go @@ -53,7 +53,8 @@ func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) [ { Node: file.AST, Confidence: 1, - Failure: fmt.Sprintf("the file has a comment density of %2.f%% (%d comment lines for %d code lines) but expected a minimum of %d%%", density, commentsLines, statementsCount, r.minimumCommentsDensity), + Failure: fmt.Sprintf("the file has a comment density of %2.f%% (%d comment lines for %d code lines) but expected a minimum of %d%%", + density, commentsLines, statementsCount, r.minimumCommentsDensity), }, } } diff --git a/rule/early-return.go b/rule/early-return.go index 9c04a1dbe..62d491f27 100644 --- a/rule/early-return.go +++ b/rule/early-return.go @@ -21,27 +21,27 @@ func (*EarlyReturnRule) Name() string { return "early-return" } -// CheckIfElse evaluates the rule against an ifelse.Chain. -func (*EarlyReturnRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) (failMsg string) { +// CheckIfElse evaluates the rule against an ifelse.Chain and returns a failure message if applicable. +func (*EarlyReturnRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) string { if !chain.Else.Deviates() { // this rule only applies if the else-block deviates control flow - return + return "" } if chain.HasPriorNonDeviating && !chain.If.IsEmpty() { // if we de-indent this block then a previous branch // might flow into it, affecting program behaviour - return + return "" } if chain.If.Deviates() { // avoid overlapping with superfluous-else - return + return "" } if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.If.HasDecls) { // avoid increasing variable scope - return + return "" } if chain.If.IsEmpty() { diff --git a/rule/filename-format.go b/rule/filename-format.go index 54e6a5bf0..49fdf9c3e 100644 --- a/rule/filename-format.go +++ b/rule/filename-format.go @@ -25,7 +25,7 @@ func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) [] return nil } - failureMsg := fmt.Sprintf("Filename %s is not of the format %s.%s", filename, r.format.String(), r.getMsgForNonAsciiChars(filename)) + failureMsg := fmt.Sprintf("Filename %s is not of the format %s.%s", filename, r.format.String(), r.getMsgForNonASCIIChars(filename)) return []lint.Failure{{ Confidence: 1, Failure: failureMsg, @@ -34,7 +34,7 @@ func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) [] }} } -func (r *FilenameFormatRule) getMsgForNonAsciiChars(str string) string { +func (r *FilenameFormatRule) getMsgForNonASCIIChars(str string) string { result := "" for _, c := range str { if c <= unicode.MaxASCII { diff --git a/rule/indent-error-flow.go b/rule/indent-error-flow.go index 294ceef84..ebc1e793a 100644 --- a/rule/indent-error-flow.go +++ b/rule/indent-error-flow.go @@ -18,27 +18,27 @@ func (*IndentErrorFlowRule) Name() string { return "indent-error-flow" } -// CheckIfElse evaluates the rule against an ifelse.Chain. -func (*IndentErrorFlowRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) (failMsg string) { +// CheckIfElse evaluates the rule against an ifelse.Chain and returns a failure message if applicable. +func (*IndentErrorFlowRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) string { if !chain.If.Deviates() { // this rule only applies if the if-block deviates control flow - return + return "" } if chain.HasPriorNonDeviating { // if we de-indent the "else" block then a previous branch // might flow into it, affecting program behaviour - return + return "" } if !chain.If.Returns() { // avoid overlapping with superfluous-else - return + return "" } if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.Else.HasDecls) { // avoid increasing variable scope - return + return "" } return "if block ends with a return statement, so drop this else and outdent its block" diff --git a/rule/superfluous-else.go b/rule/superfluous-else.go index 2aa1b6b2c..18e8f3bdd 100644 --- a/rule/superfluous-else.go +++ b/rule/superfluous-else.go @@ -2,6 +2,7 @@ package rule import ( "fmt" + "github.com/mgechev/revive/internal/ifelse" "github.com/mgechev/revive/lint" ) @@ -19,27 +20,27 @@ func (*SuperfluousElseRule) Name() string { return "superfluous-else" } -// CheckIfElse evaluates the rule against an ifelse.Chain. -func (*SuperfluousElseRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) (failMsg string) { +// CheckIfElse evaluates the rule against an ifelse.Chain and returns a failure message if applicable. +func (*SuperfluousElseRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) string { if !chain.If.Deviates() { // this rule only applies if the if-block deviates control flow - return + return "" } if chain.HasPriorNonDeviating { // if we de-indent the "else" block then a previous branch // might flow into it, affecting program behaviour - return + return "" } if chain.If.Returns() { // avoid overlapping with indent-error-flow - return + return "" } if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.Else.HasDecls) { // avoid increasing variable scope - return + return "" } return fmt.Sprintf("if block ends with %v, so drop this else and outdent its block", chain.If.LongString()) diff --git a/test/utils_test.go b/test/utils_test.go index e37698604..7cc5f2560 100644 --- a/test/utils_test.go +++ b/test/utils_test.go @@ -173,7 +173,6 @@ func parseInstructions(t *testing.T, filename string, src []byte) []instruction t.Fatalf("At %v:%d: %v", filename, ln, err) } ins = append(ins, jsonInst) - break case "classic": match, err := extractPattern(line) if err != nil { @@ -198,9 +197,7 @@ func parseInstructions(t *testing.T, filename string, src []byte) []instruction Match: match, Replacement: repl, }) - break } - } } return ins