Skip to content

Commit 4b2c76e

Browse files
authored
chore: cleanup code in rules (#1197)
1 parent 4ca2c11 commit 4b2c76e

15 files changed

+46
-36
lines changed

rule/bool_literal_in_expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ func isExprABooleanLit(n ast.Node) (lexeme string, ok bool) {
8888
return "", false
8989
}
9090

91-
return oper.Name, (oper.Name == "true" || oper.Name == "false")
91+
return oper.Name, oper.Name == "true" || oper.Name == "false"
9292
}

rule/cognitive_complexity.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ type cognitiveComplexityVisitor struct {
8989
}
9090

9191
// subTreeComplexity calculates the cognitive complexity of an AST-subtree.
92-
func (v cognitiveComplexityVisitor) subTreeComplexity(n ast.Node) int {
93-
ast.Walk(&v, n)
92+
func (v *cognitiveComplexityVisitor) subTreeComplexity(n ast.Node) int {
93+
ast.Walk(v, n)
9494
return v.complexity
9595
}
9696

@@ -122,7 +122,7 @@ func (v *cognitiveComplexityVisitor) Visit(n ast.Node) ast.Visitor {
122122
return nil
123123
case *ast.BinaryExpr:
124124
v.complexity += v.binExpComplexity(n)
125-
return nil // skip visiting binexp sub-tree (already visited by binExpComplexity)
125+
return nil // skip visiting binexp subtree (already visited by binExpComplexity)
126126
case *ast.BranchStmt:
127127
if n.Label != nil {
128128
v.complexity++
@@ -156,7 +156,7 @@ func (v *cognitiveComplexityVisitor) walk(complexityIncrement int, targets ...as
156156
v.nestingLevel = nesting
157157
}
158158

159-
func (cognitiveComplexityVisitor) binExpComplexity(n *ast.BinaryExpr) int {
159+
func (*cognitiveComplexityVisitor) binExpComplexity(n *ast.BinaryExpr) int {
160160
calculator := binExprComplexityCalculator{opsStack: []token.Token{}}
161161

162162
astutil.Apply(n, calculator.pre, calculator.post)

rule/comment_spacings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/mgechev/revive/lint"
88
)
99

10-
// CommentSpacingsRule check the whether there is a space between
10+
// CommentSpacingsRule check whether there is a space between
1111
// the comment symbol( // ) and the start of the comment text
1212
type CommentSpacingsRule struct {
1313
allowList []string

rule/constant_logical_expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (*lintConstantLogicalExpr) isInequalityOperator(t token.Token) bool {
9191
return false
9292
}
9393

94-
func (w lintConstantLogicalExpr) newFailure(node ast.Node, msg string) {
94+
func (w *lintConstantLogicalExpr) newFailure(node ast.Node, msg string) {
9595
w.onFailure(lint.Failure{
9696
Confidence: 1,
9797
Node: node,

rule/datarace.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (r *DataRaceRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
2222

2323
funcResults := funcDecl.Type.Results
2424

25+
// TODO: ast.Object is deprecated
2526
returnIDs := map[*ast.Object]struct{}{}
2627
if funcResults != nil {
2728
returnIDs = r.extractReturnIDs(funcResults.List)
@@ -34,7 +35,7 @@ func (r *DataRaceRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
3435
fl := &lintFunctionForDataRaces{
3536
onFailure: onFailure,
3637
returnIDs: returnIDs,
37-
rangeIDs: map[*ast.Object]struct{}{},
38+
rangeIDs: map[*ast.Object]struct{}{}, // TODO: ast.Object is deprecated
3839
go122for: isGo122,
3940
}
4041

@@ -49,6 +50,7 @@ func (*DataRaceRule) Name() string {
4950
return "datarace"
5051
}
5152

53+
// TODO: ast.Object is deprecated
5254
func (*DataRaceRule) extractReturnIDs(fields []*ast.Field) map[*ast.Object]struct{} {
5355
r := map[*ast.Object]struct{}{}
5456
for _, f := range fields {
@@ -63,9 +65,10 @@ func (*DataRaceRule) extractReturnIDs(fields []*ast.Field) map[*ast.Object]struc
6365
type lintFunctionForDataRaces struct {
6466
_ struct{}
6567
onFailure func(failure lint.Failure)
66-
returnIDs map[*ast.Object]struct{}
67-
rangeIDs map[*ast.Object]struct{}
68-
go122for bool
68+
returnIDs map[*ast.Object]struct{} // TODO: ast.Object is deprecated
69+
rangeIDs map[*ast.Object]struct{} // TODO: ast.Object is deprecated
70+
71+
go122for bool
6972
}
7073

7174
func (w lintFunctionForDataRaces) Visit(node ast.Node) ast.Visitor {

rule/identical_branches.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (w *lintIdenticalBranches) Visit(node ast.Node) ast.Visitor {
5959
return w
6060
}
6161

62-
func (lintIdenticalBranches) identicalBranches(branches []*ast.BlockStmt) bool {
62+
func (*lintIdenticalBranches) identicalBranches(branches []*ast.BlockStmt) bool {
6363
if len(branches) < 2 {
6464
return false // only one branch to compare thus we return
6565
}
@@ -77,7 +77,7 @@ func (lintIdenticalBranches) identicalBranches(branches []*ast.BlockStmt) bool {
7777
return true
7878
}
7979

80-
func (w lintIdenticalBranches) newFailure(node ast.Node, msg string) {
80+
func (w *lintIdenticalBranches) newFailure(node ast.Node, msg string) {
8181
w.onFailure(lint.Failure{
8282
Confidence: 1,
8383
Node: node,

rule/import_shadowing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (*ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail
2828
onFailure: func(failure lint.Failure) {
2929
failures = append(failures, failure)
3030
},
31-
alreadySeen: map[*ast.Object]struct{}{},
31+
alreadySeen: map[*ast.Object]struct{}{}, // TODO: ast.Object is deprecated
3232
skipIdents: map[*ast.Ident]struct{}{},
3333
}
3434

@@ -62,7 +62,7 @@ type importShadowing struct {
6262
packageNameIdent *ast.Ident
6363
importNames map[string]struct{}
6464
onFailure func(lint.Failure)
65-
alreadySeen map[*ast.Object]struct{}
65+
alreadySeen map[*ast.Object]struct{} // TODO: ast.Object is deprecated
6666
skipIdents map[*ast.Ident]struct{}
6767
}
6868

rule/range_val_address.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (w rangeValAddress) Visit(node ast.Node) ast.Visitor {
7070

7171
type rangeBodyVisitor struct {
7272
valueIsStarExpr bool
73-
valueID *ast.Object
73+
valueID *ast.Object // TODO: ast.Object is deprecated
7474
onFailure func(lint.Failure)
7575
}
7676

@@ -140,7 +140,7 @@ func (bw rangeBodyVisitor) isAccessingRangeValueAddress(exp ast.Expr) bool {
140140
v, ok := u.X.(*ast.Ident)
141141
if !ok {
142142
var s *ast.SelectorExpr
143-
s, ok = u.X.(*ast.SelectorExpr)
143+
s, ok = u.X.(*ast.SelectorExpr) // TODO: possible BUG: if it's `=` and not `:=`, it means that in the last return `ok` is always true
144144
if !ok {
145145
return false
146146
}
@@ -154,7 +154,7 @@ func (bw rangeBodyVisitor) isAccessingRangeValueAddress(exp ast.Expr) bool {
154154
}
155155
}
156156

157-
return ok && v.Obj == bw.valueID
157+
return ok && v.Obj == bw.valueID // TODO: ok is always true due to the previous TODO remark
158158
}
159159

160160
func (bw rangeBodyVisitor) newFailure(node ast.Node) lint.Failure {

rule/redefines_builtin_id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (w *lintRedefinesBuiltinID) Visit(node ast.Node) ast.Visitor {
198198
return w
199199
}
200200

201-
func (w lintRedefinesBuiltinID) addFailure(node ast.Node, msg string) {
201+
func (w *lintRedefinesBuiltinID) addFailure(node ast.Node, msg string) {
202202
w.onFailure(lint.Failure{
203203
Confidence: 1,
204204
Node: node,

rule/string_format.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (*StringFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint
2222
failures = append(failures, failure)
2323
}
2424

25-
w := lintStringFormatRule{onFailure: onFailure}
25+
w := &lintStringFormatRule{onFailure: onFailure}
2626
err := w.parseArguments(arguments)
2727
if err != nil {
2828
return newInternalFailureError(err)
@@ -39,7 +39,7 @@ func (*StringFormatRule) Name() string {
3939
}
4040

4141
// ParseArgumentsTest is a public wrapper around w.parseArguments used for testing. Returns the error message provided to panic, or nil if no error was encountered
42-
func (StringFormatRule) ParseArgumentsTest(arguments lint.Arguments) *string {
42+
func (*StringFormatRule) ParseArgumentsTest(arguments lint.Arguments) *string {
4343
w := lintStringFormatRule{}
4444
c := make(chan any)
4545
// Parse the arguments in a goroutine, defer a recover() call, return the error encountered (or nil if there was no error)
@@ -101,7 +101,7 @@ func (w *lintStringFormatRule) parseArguments(arguments lint.Arguments) error {
101101
return nil
102102
}
103103

104-
func (w lintStringFormatRule) parseArgument(argument any, ruleNum int) (scopes stringFormatSubruleScopes, regex *regexp.Regexp, negated bool, errorMessage string, err error) {
104+
func (w *lintStringFormatRule) parseArgument(argument any, ruleNum int) (scopes stringFormatSubruleScopes, regex *regexp.Regexp, negated bool, errorMessage string, err error) {
105105
g, ok := argument.([]any) // Cast to generic slice first
106106
if !ok {
107107
return stringFormatSubruleScopes{}, regex, false, "", w.configError("argument is not a slice", ruleNum, 0)
@@ -179,21 +179,21 @@ func (w lintStringFormatRule) parseArgument(argument any, ruleNum int) (scopes s
179179
}
180180

181181
// Report an invalid config, this is specifically the user's fault
182-
func (lintStringFormatRule) configError(msg string, ruleNum, option int) error {
182+
func (*lintStringFormatRule) configError(msg string, ruleNum, option int) error {
183183
return fmt.Errorf("invalid configuration for string-format: %s [argument %d, option %d]", msg, ruleNum, option)
184184
}
185185

186186
// Report a general config parsing failure, this may be the user's fault, but it isn't known for certain
187-
func (lintStringFormatRule) parseError(msg string, ruleNum, option int) error {
187+
func (*lintStringFormatRule) parseError(msg string, ruleNum, option int) error {
188188
return fmt.Errorf("failed to parse configuration for string-format: %s [argument %d, option %d]", msg, ruleNum, option)
189189
}
190190

191191
// Report a general scope config parsing failure, this may be the user's fault, but it isn't known for certain
192-
func (lintStringFormatRule) parseScopeError(msg string, ruleNum, option, scopeNum int) error {
192+
func (*lintStringFormatRule) parseScopeError(msg string, ruleNum, option, scopeNum int) error {
193193
return fmt.Errorf("failed to parse configuration for string-format: %s [argument %d, option %d, scope index %d]", msg, ruleNum, option, scopeNum)
194194
}
195195

196-
func (w lintStringFormatRule) Visit(node ast.Node) ast.Visitor {
196+
func (w *lintStringFormatRule) Visit(node ast.Node) ast.Visitor {
197197
// First, check if node is a call expression
198198
call, ok := node.(*ast.CallExpr)
199199
if !ok {
@@ -218,7 +218,7 @@ func (w lintStringFormatRule) Visit(node ast.Node) ast.Visitor {
218218
}
219219

220220
// Return the name of a call expression in the form of package.Func or Func
221-
func (lintStringFormatRule) getCallName(call *ast.CallExpr) (callName string, ok bool) {
221+
func (*lintStringFormatRule) getCallName(call *ast.CallExpr) (callName string, ok bool) {
222222
if ident, ok := call.Fun.(*ast.Ident); ok {
223223
// Local function call
224224
return ident.Name, true
@@ -278,6 +278,12 @@ func (r *stringFormatSubrule) apply(call *ast.CallExpr, scope *stringFormatSubru
278278
return
279279
}
280280
}
281+
282+
// extra safety check
283+
if lit == nil {
284+
return
285+
}
286+
281287
// Unquote the string literal before linting
282288
unquoted := lit.Value[1 : len(lit.Value)-1]
283289
if r.stringIsOK(unquoted) {

rule/unconditional_recursion.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (*UnconditionalRecursionRule) Apply(file *lint.File, _ lint.Arguments) []li
1717
failures = append(failures, failure)
1818
}
1919

20-
w := lintUnconditionalRecursionRule{onFailure: onFailure}
20+
w := &lintUnconditionalRecursionRule{onFailure: onFailure}
2121
ast.Walk(w, file.AST)
2222
return failures
2323
}
@@ -57,7 +57,7 @@ type lintUnconditionalRecursionRule struct {
5757
// If we find conditional control exits, it means the function is NOT unconditionally-recursive
5858
// If we find a recursive call before finding any conditional exit, a failure is generated
5959
// In resume: if we found a recursive call control-dependent from the entry point of the function then we raise a failure.
60-
func (w lintUnconditionalRecursionRule) Visit(node ast.Node) ast.Visitor {
60+
func (w *lintUnconditionalRecursionRule) Visit(node ast.Node) ast.Visitor {
6161
switch n := node.(type) {
6262
case *ast.FuncDecl:
6363
var rec *ast.Ident
@@ -152,7 +152,7 @@ func (w *lintUnconditionalRecursionRule) updateFuncStatus(node ast.Node) {
152152
w.currentFunc.seenConditionalExit = w.hasControlExit(node)
153153
}
154154

155-
func (lintUnconditionalRecursionRule) hasControlExit(node ast.Node) bool {
155+
func (*lintUnconditionalRecursionRule) hasControlExit(node ast.Node) bool {
156156
// isExit returns true if the given node makes control exit the function
157157
isExit := func(node ast.Node) bool {
158158
switch n := node.(type) {

rule/unused_param.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type UnusedParamRule struct {
2121
//
2222
// Configuration implements the [lint.ConfigurableRule] interface.
2323
func (r *UnusedParamRule) Configure(args lint.Arguments) error {
24-
// while by default args is an array, i think it's good to provide structures inside it by default, not arrays or primitives
25-
// it's more compatible to JSON nature of configurations
24+
// while by default args is an array, it could be good to provide structures inside it by default, not arrays or primitives
25+
// as it's more compatible to JSON nature of configurations
2626
r.allowRegex = allowBlankIdentifierRegex
2727
r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it as _"
2828
if len(args) == 0 {
@@ -139,6 +139,7 @@ func (w lintUnusedParamRule) Visit(node ast.Node) ast.Visitor {
139139
return w // full method body was inspected
140140
}
141141

142+
// TODO: ast.Object is deprecated
142143
func retrieveNamedParams(params *ast.FieldList) map[*ast.Object]bool {
143144
result := map[*ast.Object]bool{}
144145
if params.List == nil {

rule/unused_receiver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type UnusedReceiverRule struct {
1919
//
2020
// Configuration implements the [lint.ConfigurableRule] interface.
2121
func (r *UnusedReceiverRule) Configure(args lint.Arguments) error {
22-
// while by default args is an array, i think it's good to provide structures inside it by default, not arrays or primitives
23-
// it's more compatible to JSON nature of configurations
22+
// while by default args is an array, it could be good to provide structures inside it by default, not arrays or primitives
23+
// as it's more compatible to JSON nature of configurations
2424
r.allowRegex = allowBlankIdentifierRegex
2525
r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it as _"
2626
if len(args) == 0 {

rule/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func isCallToExitFunction(pkgName, functionName string) bool {
123123
return exitFunctions[pkgName] != nil && exitFunctions[pkgName][functionName]
124124
}
125125

126-
// newInternalFailureError returns an slice of Failure with a single internal failure in it
126+
// newInternalFailureError returns a slice of Failure with a single internal failure in it
127127
func newInternalFailureError(e error) []lint.Failure {
128128
return []lint.Failure{lint.NewInternalFailure(e.Error())}
129129
}

rule/waitgroup_by_value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (w lintWaitGroupByValueRule) Visit(node ast.Node) ast.Visitor {
3838
return w
3939
}
4040

41-
// Check all function's parameters
41+
// Check all function parameters
4242
for _, field := range fd.Type.Params.List {
4343
if !w.isWaitGroup(field.Type) {
4444
continue

0 commit comments

Comments
 (0)