Skip to content

Commit 395f790

Browse files
authored
refactor: replace failure Category raw string with constant (#1196)
* refactor: replace Category raw strings with constants * Add type FailureCategory; add comments for constants
1 parent 57fe5b6 commit 395f790

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+151
-107
lines changed

lint/failure.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,53 @@ import (
55
"go/token"
66
)
77

8+
const (
9+
// FailureCategoryArgOrder indicates argument order issues.
10+
FailureCategoryArgOrder FailureCategory = "arg-order"
11+
// FailureCategoryBadPractice indicates bad practice issues.
12+
FailureCategoryBadPractice FailureCategory = "bad practice"
13+
// FailureCategoryCodeStyle indicates code style issues.
14+
FailureCategoryCodeStyle FailureCategory = "code-style"
15+
// FailureCategoryComments indicates comment issues.
16+
FailureCategoryComments FailureCategory = "comments"
17+
// FailureCategoryComplexity indicates complexity issues.
18+
FailureCategoryComplexity FailureCategory = "complexity"
19+
// FailureCategoryContent indicates content issues.
20+
FailureCategoryContent FailureCategory = "content"
21+
// FailureCategoryErrors indicates error handling issues.
22+
FailureCategoryErrors FailureCategory = "errors"
23+
// FailureCategoryImports indicates import issues.
24+
FailureCategoryImports FailureCategory = "imports"
25+
// FailureCategoryLogic indicates logic issues.
26+
FailureCategoryLogic FailureCategory = "logic"
27+
// FailureCategoryMaintenance indicates maintenance issues.
28+
FailureCategoryMaintenance FailureCategory = "maintenance"
29+
// FailureCategoryNaming indicates naming issues.
30+
FailureCategoryNaming FailureCategory = "naming"
31+
// FailureCategoryOptimization indicates optimization issues.
32+
FailureCategoryOptimization FailureCategory = "optimization"
33+
// FailureCategoryStyle indicates style issues.
34+
FailureCategoryStyle FailureCategory = "style"
35+
// FailureCategoryTime indicates time-related issues.
36+
FailureCategoryTime FailureCategory = "time"
37+
// FailureCategoryTypeInference indicates type inference issues.
38+
FailureCategoryTypeInference FailureCategory = "type-inference"
39+
// FailureCategoryUnaryOp indicates unary operation issues.
40+
FailureCategoryUnaryOp FailureCategory = "unary-op"
41+
// FailureCategoryUnexportedTypeInAPI indicates unexported type in API issues.
42+
FailureCategoryUnexportedTypeInAPI FailureCategory = "unexported-type-in-api"
43+
// FailureCategoryZeroValue indicates zero value issues.
44+
FailureCategoryZeroValue FailureCategory = "zero-value"
45+
46+
// failureCategoryInternal indicates internal failures.
47+
failureCategoryInternal FailureCategory = "REVIVE_INTERNAL"
48+
// failureCategoryValidity indicates validity issues.
49+
failureCategoryValidity FailureCategory = "validity"
50+
)
51+
52+
// FailureCategory is the type for the failure categories.
53+
type FailureCategory string
54+
855
const (
956
// SeverityWarning declares failures of type warning
1057
SeverityWarning = "warning"
@@ -25,7 +72,7 @@ type FailurePosition struct {
2572
type Failure struct {
2673
Failure string
2774
RuleName string
28-
Category string
75+
Category FailureCategory
2976
Position FailurePosition
3077
Node ast.Node `json:"-"`
3178
Confidence float64
@@ -38,17 +85,15 @@ func (f *Failure) GetFilename() string {
3885
return f.Position.Start.Filename
3986
}
4087

41-
const internalFailure = "REVIVE_INTERNAL"
42-
4388
// IsInternal returns true if this failure is internal, false otherwise.
4489
func (f *Failure) IsInternal() bool {
45-
return f.Category == internalFailure
90+
return f.Category == failureCategoryInternal
4691
}
4792

4893
// NewInternalFailure yields an internal failure with the given message as failure message.
4994
func NewInternalFailure(message string) Failure {
5095
return Failure{
51-
Category: internalFailure,
96+
Category: failureCategoryInternal,
5297
Failure: message,
5398
}
5499
}

lint/linter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func addInvalidFileFailure(filename, errStr string, failures chan Failure) {
223223
failures <- Failure{
224224
Confidence: 1,
225225
Failure: fmt.Sprintf("invalid file %s: %v", filename, errStr),
226-
Category: "validity",
226+
Category: failureCategoryValidity,
227227
Position: position,
228228
}
229229
}

rule/add_constant.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (w *lintAddConstantRule) checkStrLit(n *ast.BasicLit) {
171171
w.onFailure(lint.Failure{
172172
Confidence: 1,
173173
Node: n,
174-
Category: "style",
174+
Category: lint.FailureCategoryStyle,
175175
Failure: fmt.Sprintf("string literal %s appears, at least, %d times, create a named constant for it", n.Value, w.strLits[n.Value]),
176176
})
177177
w.strLits[n.Value] = -1 // mark it to avoid failing again on the same literal
@@ -187,7 +187,7 @@ func (w *lintAddConstantRule) checkNumLit(kind string, n *ast.BasicLit) {
187187
w.onFailure(lint.Failure{
188188
Confidence: 1,
189189
Node: n,
190-
Category: "style",
190+
Category: lint.FailureCategoryStyle,
191191
Failure: fmt.Sprintf("avoid magic numbers like '%s', create a named constant for it", n.Value),
192192
})
193193
}

rule/blank_imports.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failu
2323

2424
const (
2525
message = "a blank import should be only in a main or test package, or have a comment justifying it"
26-
category = "imports"
2726
embedImportPath = `"embed"`
2827
)
2928

@@ -55,7 +54,7 @@ func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failu
5554

5655
// This is the first blank import of a group.
5756
if imp.Doc == nil && imp.Comment == nil {
58-
failures = append(failures, lint.Failure{Failure: message, Category: category, Node: imp, Confidence: 1})
57+
failures = append(failures, lint.Failure{Failure: message, Category: lint.FailureCategoryImports, Node: imp, Confidence: 1})
5958
}
6059
}
6160

rule/bool_literal_in_expr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ func (w *lintBoolLiteral) Visit(node ast.Node) ast.Visitor {
5353
isConstant := (n.Op == token.LAND && lexeme == "false") || (n.Op == token.LOR && lexeme == "true")
5454

5555
if isConstant {
56-
w.addFailure(n, "Boolean expression seems to always evaluate to "+lexeme, "logic")
56+
w.addFailure(n, "Boolean expression seems to always evaluate to "+lexeme, lint.FailureCategoryLogic)
5757
} else {
58-
w.addFailure(n, "omit Boolean literal in expression", "style")
58+
w.addFailure(n, "omit Boolean literal in expression", lint.FailureCategoryStyle)
5959
}
6060
}
6161

6262
return w
6363
}
6464

65-
func (w lintBoolLiteral) addFailure(node ast.Node, msg, cat string) {
65+
func (w lintBoolLiteral) addFailure(node ast.Node, msg string, cat lint.FailureCategory) {
6666
w.onFailure(lint.Failure{
6767
Confidence: 1,
6868
Node: node,

rule/call_to_gc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (w lintCallToGC) Visit(node ast.Node) ast.Visitor {
6262
w.onFailure(lint.Failure{
6363
Confidence: 1,
6464
Node: node,
65-
Category: "bad practice",
65+
Category: lint.FailureCategoryBadPractice,
6666
Failure: "explicit call to the garbage collector",
6767
})
6868

rule/cognitive_complexity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (w cognitiveComplexityLinter) lintCognitiveComplexity() {
7373
if c > w.maxComplexity {
7474
w.onFailure(lint.Failure{
7575
Confidence: 1,
76-
Category: "maintenance",
76+
Category: lint.FailureCategoryMaintenance,
7777
Failure: fmt.Sprintf("function %s has cognitive complexity %d (> max enabled %d)", funcName(fn), c, w.maxComplexity),
7878
Node: fn,
7979
})

rule/comment_spacings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (r *CommentSpacingsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa
5757
failures = append(failures, lint.Failure{
5858
Node: comment,
5959
Confidence: 1,
60-
Category: "style",
60+
Category: lint.FailureCategoryStyle,
6161
Failure: "no space between comment delimiter and comment text",
6262
})
6363
}

rule/confusing_naming.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func checkMethodName(holder string, id *ast.Ident, w *lintConfusingNames) {
102102
Failure: fmt.Sprintf("Method '%s' differs only by capitalization to %s '%s' in %s", id.Name, kind, refMethod.id.Name, fileName),
103103
Confidence: 1,
104104
Node: id,
105-
Category: "naming",
105+
Category: lint.FailureCategoryNaming,
106106
})
107107

108108
return
@@ -176,7 +176,7 @@ func checkStructFields(fields *ast.FieldList, structName string, w *lintConfusin
176176
Failure: fmt.Sprintf("Field '%s' differs only by capitalization to other field in the struct type %s", id.Name, structName),
177177
Confidence: 1,
178178
Node: id,
179-
Category: "naming",
179+
Category: lint.FailureCategoryNaming,
180180
})
181181
} else {
182182
bl[normName] = true

rule/confusing_results.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fai
3434
failures = append(failures, lint.Failure{
3535
Node: result,
3636
Confidence: 1,
37-
Category: "naming",
37+
Category: lint.FailureCategoryNaming,
3838
Failure: "unnamed results of the same type may be confusing, consider using named results",
3939
})
4040

0 commit comments

Comments
 (0)