Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't warn on benchmarks #14

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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++ {
}
}
Loading