Skip to content

Commit

Permalink
revive: add revive.toml for linting revive itself (#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Nov 4, 2024
1 parent eb18252 commit 9a8587c
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ jobs:

- name: Run Revive Action
uses: morphy2k/revive-action@v2
with:
config: revive.toml
10 changes: 5 additions & 5 deletions lint/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
38 changes: 38 additions & 0 deletions revive.toml
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 2 additions & 2 deletions rule/add-constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion rule/comments-density.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}
}
Expand Down
12 changes: 6 additions & 6 deletions rule/early-return.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions rule/filename-format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions rule/indent-error-flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 7 additions & 6 deletions rule/superfluous-else.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rule

import (
"fmt"

"github.com/mgechev/revive/internal/ifelse"
"github.com/mgechev/revive/lint"
)
Expand All @@ -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())
Expand Down
3 changes: 0 additions & 3 deletions test/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -198,9 +197,7 @@ func parseInstructions(t *testing.T, filename string, src []byte) []instruction
Match: match,
Replacement: repl,
})
break
}

}
}
return ins
Expand Down

0 comments on commit 9a8587c

Please sign in to comment.