Skip to content

Commit 9a8587c

Browse files
authored
revive: add revive.toml for linting revive itself (#1094)
1 parent eb18252 commit 9a8587c

File tree

10 files changed

+70
-31
lines changed

10 files changed

+70
-31
lines changed

.github/workflows/lint.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ jobs:
1616

1717
- name: Run Revive Action
1818
uses: morphy2k/revive-action@v2
19+
with:
20+
config: revive.toml

lint/package.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,17 @@ func (p *Package) scanSortable() {
166166

167167
// bitfield for which methods exist on each type.
168168
const (
169-
Len = 1 << iota
170-
Less
171-
Swap
169+
bfLen = 1 << iota
170+
bfLess
171+
bfSwap
172172
)
173-
nmap := map[string]int{"Len": Len, "Less": Less, "Swap": Swap}
173+
nmap := map[string]int{"Len": bfLen, "Less": bfLess, "Swap": bfSwap}
174174
has := make(map[string]int)
175175
for _, f := range p.files {
176176
ast.Walk(&walker{nmap, has}, f.AST)
177177
}
178178
for typ, ms := range has {
179-
if ms == Len|Less|Swap {
179+
if ms == bfLen|bfLess|bfSwap {
180180
p.sortable[typ] = true
181181
}
182182
}

revive.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This configuration for the revive linter used for linting the revive's codebase itself.
2+
# See .github/workflows/lint.yaml.
3+
4+
ignoreGeneratedHeader = false
5+
severity = "warning"
6+
confidence = 0.8
7+
errorCode = 1
8+
warningCode = 1
9+
10+
[rule.bare-return]
11+
[rule.blank-imports]
12+
[rule.context-as-argument]
13+
[rule.context-keys-type]
14+
[rule.dot-imports]
15+
[rule.empty-block]
16+
[rule.empty-lines]
17+
[rule.error-naming]
18+
[rule.error-return]
19+
[rule.error-strings]
20+
[rule.errorf]
21+
[rule.exported]
22+
[rule.increment-decrement]
23+
[rule.indent-error-flow]
24+
[rule.line-length-limit]
25+
arguments = [200]
26+
[rule.package-comments]
27+
[rule.range]
28+
[rule.receiver-naming]
29+
[rule.redefines-builtin-id]
30+
[rule.superfluous-else]
31+
[rule.time-naming]
32+
[rule.unexported-naming]
33+
[rule.unexported-return]
34+
[rule.unreachable-code]
35+
[rule.unused-parameter]
36+
[rule.useless-break]
37+
[rule.var-declaration]
38+
[rule.var-naming]

rule/add-constant.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ func (w *lintAddConstantRule) isIgnoredFunc(fName string) bool {
160160
}
161161

162162
func (w *lintAddConstantRule) checkStrLit(n *ast.BasicLit) {
163-
const IgnoreMarker = -1
163+
const ignoreMarker = -1
164164

165165
if w.allowList[kindSTRING][n.Value] {
166166
return
167167
}
168168

169169
count := w.strLits[n.Value]
170-
mustCheck := count > IgnoreMarker
170+
mustCheck := count > ignoreMarker
171171
if mustCheck {
172172
w.strLits[n.Value] = count + 1
173173
if w.strLits[n.Value] > w.strLitLimit {

rule/comments-density.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) [
5353
{
5454
Node: file.AST,
5555
Confidence: 1,
56-
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),
56+
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%%",
57+
density, commentsLines, statementsCount, r.minimumCommentsDensity),
5758
},
5859
}
5960
}

rule/early-return.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ func (*EarlyReturnRule) Name() string {
2121
return "early-return"
2222
}
2323

24-
// CheckIfElse evaluates the rule against an ifelse.Chain.
25-
func (*EarlyReturnRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) (failMsg string) {
24+
// CheckIfElse evaluates the rule against an ifelse.Chain and returns a failure message if applicable.
25+
func (*EarlyReturnRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) string {
2626
if !chain.Else.Deviates() {
2727
// this rule only applies if the else-block deviates control flow
28-
return
28+
return ""
2929
}
3030

3131
if chain.HasPriorNonDeviating && !chain.If.IsEmpty() {
3232
// if we de-indent this block then a previous branch
3333
// might flow into it, affecting program behaviour
34-
return
34+
return ""
3535
}
3636

3737
if chain.If.Deviates() {
3838
// avoid overlapping with superfluous-else
39-
return
39+
return ""
4040
}
4141

4242
if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.If.HasDecls) {
4343
// avoid increasing variable scope
44-
return
44+
return ""
4545
}
4646

4747
if chain.If.IsEmpty() {

rule/filename-format.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) []
2525
return nil
2626
}
2727

28-
failureMsg := fmt.Sprintf("Filename %s is not of the format %s.%s", filename, r.format.String(), r.getMsgForNonAsciiChars(filename))
28+
failureMsg := fmt.Sprintf("Filename %s is not of the format %s.%s", filename, r.format.String(), r.getMsgForNonASCIIChars(filename))
2929
return []lint.Failure{{
3030
Confidence: 1,
3131
Failure: failureMsg,
@@ -34,7 +34,7 @@ func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) []
3434
}}
3535
}
3636

37-
func (r *FilenameFormatRule) getMsgForNonAsciiChars(str string) string {
37+
func (r *FilenameFormatRule) getMsgForNonASCIIChars(str string) string {
3838
result := ""
3939
for _, c := range str {
4040
if c <= unicode.MaxASCII {

rule/indent-error-flow.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ func (*IndentErrorFlowRule) Name() string {
1818
return "indent-error-flow"
1919
}
2020

21-
// CheckIfElse evaluates the rule against an ifelse.Chain.
22-
func (*IndentErrorFlowRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) (failMsg string) {
21+
// CheckIfElse evaluates the rule against an ifelse.Chain and returns a failure message if applicable.
22+
func (*IndentErrorFlowRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) string {
2323
if !chain.If.Deviates() {
2424
// this rule only applies if the if-block deviates control flow
25-
return
25+
return ""
2626
}
2727

2828
if chain.HasPriorNonDeviating {
2929
// if we de-indent the "else" block then a previous branch
3030
// might flow into it, affecting program behaviour
31-
return
31+
return ""
3232
}
3333

3434
if !chain.If.Returns() {
3535
// avoid overlapping with superfluous-else
36-
return
36+
return ""
3737
}
3838

3939
if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.Else.HasDecls) {
4040
// avoid increasing variable scope
41-
return
41+
return ""
4242
}
4343

4444
return "if block ends with a return statement, so drop this else and outdent its block"

rule/superfluous-else.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rule
22

33
import (
44
"fmt"
5+
56
"github.com/mgechev/revive/internal/ifelse"
67
"github.com/mgechev/revive/lint"
78
)
@@ -19,27 +20,27 @@ func (*SuperfluousElseRule) Name() string {
1920
return "superfluous-else"
2021
}
2122

22-
// CheckIfElse evaluates the rule against an ifelse.Chain.
23-
func (*SuperfluousElseRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) (failMsg string) {
23+
// CheckIfElse evaluates the rule against an ifelse.Chain and returns a failure message if applicable.
24+
func (*SuperfluousElseRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) string {
2425
if !chain.If.Deviates() {
2526
// this rule only applies if the if-block deviates control flow
26-
return
27+
return ""
2728
}
2829

2930
if chain.HasPriorNonDeviating {
3031
// if we de-indent the "else" block then a previous branch
3132
// might flow into it, affecting program behaviour
32-
return
33+
return ""
3334
}
3435

3536
if chain.If.Returns() {
3637
// avoid overlapping with indent-error-flow
37-
return
38+
return ""
3839
}
3940

4041
if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.Else.HasDecls) {
4142
// avoid increasing variable scope
42-
return
43+
return ""
4344
}
4445

4546
return fmt.Sprintf("if block ends with %v, so drop this else and outdent its block", chain.If.LongString())

test/utils_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ func parseInstructions(t *testing.T, filename string, src []byte) []instruction
173173
t.Fatalf("At %v:%d: %v", filename, ln, err)
174174
}
175175
ins = append(ins, jsonInst)
176-
break
177176
case "classic":
178177
match, err := extractPattern(line)
179178
if err != nil {
@@ -198,9 +197,7 @@ func parseInstructions(t *testing.T, filename string, src []byte) []instruction
198197
Match: match,
199198
Replacement: repl,
200199
})
201-
break
202200
}
203-
204201
}
205202
}
206203
return ins

0 commit comments

Comments
 (0)