Skip to content

Commit

Permalink
Don't warn on benchmarks (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckaznocha committed Mar 21, 2024
1 parent 114a649 commit 737670c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
30 changes: 30 additions & 0 deletions intrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func check(pass *analysis.Pass) func(node ast.Node) {

switch cond.Op {
case token.LSS: // ;i < x;
if isBenchmark(cond.Y) {
return
}

x, ok := cond.X.(*ast.Ident)
if !ok {
return
Expand All @@ -95,6 +99,10 @@ func check(pass *analysis.Pass) func(node ast.Node) {
return
}
case token.GTR: // ;x > i;
if isBenchmark(cond.X) {
return
}

y, ok := cond.Y.(*ast.Ident)
if !ok {
return
Expand Down Expand Up @@ -216,6 +224,28 @@ func check(pass *analysis.Pass) func(node ast.Node) {
}
}

func isBenchmark(expr ast.Expr) bool {
selectorExpr, ok := expr.(*ast.SelectorExpr)
if !ok {
return false
}

if selectorExpr.Sel.Name != "N" {
return false
}

ident, ok := selectorExpr.X.(*ast.Ident)
if !ok {
return false
}

if ident.Name == "b" {
return true
}

return false
}

type bodyChecker struct {
initIdent *ast.Ident
modified bool
Expand Down
9 changes: 9 additions & 0 deletions testdata/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "testing"

func main() {
for i := 2; i < 10; i++ {
}
Expand Down Expand Up @@ -125,4 +127,11 @@ func main() {

for i := 0; x > i; i = 0x1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)`
}

var b *testing.B
for i := 0; i < b.N; i++ {
}

for i := 0; b.N >= i; i++ {
}
}

0 comments on commit 737670c

Please sign in to comment.