1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-21 17:16:40 +02:00

revive: add revive.toml for linting revive itself (#1094)

This commit is contained in:
Oleksandr Redko 2024-11-04 14:18:17 +02:00 committed by GitHub
parent eb18252088
commit 9a8587cc49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 70 additions and 31 deletions

View File

@ -16,3 +16,5 @@ jobs:
- name: Run Revive Action
uses: morphy2k/revive-action@v2
with:
config: revive.toml

View File

@ -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
}
}

38
revive.toml Normal file
View File

@ -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]

View File

@ -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 {

View File

@ -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),
},
}
}

View File

@ -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() {

View File

@ -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 {

View File

@ -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"

View File

@ -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())

View File

@ -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