diff --git a/.golangci.yml b/.golangci.yml index f725635..2ad830d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -94,3 +94,6 @@ linters: - wastedassign - whitespace - wsl +run: + skip-dirs: + - testdata/ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9540e6a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cSpell.words": ["intrange"] +} diff --git a/README.md b/README.md index af655f3..05d78b6 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ func main() { Running `intrange` on the above code will produce the following output: ```bash -main.go:5:2: for loop can use an int range +main.go:5:2: for loop can be changed to use an integer range (Go 1.22+) ``` The loop can be rewritten as: diff --git a/intrange.go b/intrange.go index b0d44fe..0a7ae3d 100644 --- a/intrange.go +++ b/intrange.go @@ -2,6 +2,7 @@ package intrange import ( "errors" + "fmt" "go/ast" "go/token" "strconv" @@ -14,24 +15,33 @@ import ( var ( Analyzer = &analysis.Analyzer{ Name: "intrange", - Doc: "intrange is a linter to find places where for loops can make use of an int range.", + Doc: "intrange is a linter to find places where for loops could make use of an integer range.", Run: run, Requires: []*analysis.Analyzer{inspect.Analyzer}, } - ErrNoResult = errors.New("no inspect result") - ErrResultType = errors.New("inspect result has wrong type") + errFailedAnalysis = errors.New("failed analysis") ) +const msg = "for loop can be changed to use an integer range (Go 1.22+)" + func run(pass *analysis.Pass) (any, error) { result, ok := pass.ResultOf[inspect.Analyzer] if !ok { - return nil, ErrNoResult + return nil, fmt.Errorf( + "%w: %s", + errFailedAnalysis, + inspect.Analyzer.Name, + ) } resultInspector, ok := result.(*inspector.Inspector) if !ok { - return nil, ErrResultType + return nil, fmt.Errorf( + "%w: %s", + errFailedAnalysis, + inspect.Analyzer.Name, + ) } resultInspector.Preorder([]ast.Node{(*ast.ForStmt)(nil)}, check(pass)) @@ -201,7 +211,7 @@ func check(pass *analysis.Pass) func(node ast.Node) { pass.Report(analysis.Diagnostic{ Pos: forStmt.Pos(), - Message: "for loop can use an int range", + Message: msg, }) } } diff --git a/testdata/main.go b/testdata/main.go index 9e0e5c0..4c87f33 100644 --- a/testdata/main.go +++ b/testdata/main.go @@ -19,55 +19,55 @@ func main() { i = i + 1 } - for i := 0; i < 10; i++ { // want `for loop can use an int range` + for i := 0; i < 10; i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := uint32(0); i < 10; i++ { // want `for loop can use an int range` + for i := uint32(0); i < 10; i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0x0; i < 10; i++ { // want `for loop can use an int range` + for i := 0x0; i < 10; i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < 10; i += 1 { // want `for loop can use an int range` + for i := 0; i < 10; i += 1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < 10; i += 0x1 { // want `for loop can use an int range` + for i := 0; i < 10; i += 0x1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < 10; i = i + 1 { // want `for loop can use an int range` + for i := 0; i < 10; i = i + 1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < 10; i = i + 0x1 { // want `for loop can use an int range` + for i := 0; i < 10; i = i + 0x1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < 10; i = 1 + i { // want `for loop can use an int range` + for i := 0; i < 10; i = 1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < 10; i = 0x1 + i { // want `for loop can use an int range` + for i := 0; i < 10; i = 0x1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; 10 > i; i++ { // want `for loop can use an int range` + for i := 0; 10 > i; i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0x0; 10 > i; i++ { // want `for loop can use an int range` + for i := 0x0; 10 > i; i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; 10 > i; i += 1 { // want `for loop can use an int range` + for i := 0; 10 > i; i += 1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; 10 > i; i += 0x1 { // want `for loop can use an int range` + for i := 0; 10 > i; i += 0x1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; 10 > i; i = i + 1 { // want `for loop can use an int range` + for i := 0; 10 > i; i = i + 1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; 10 > i; i = i + 0x1 { // want `for loop can use an int range` + for i := 0; 10 > i; i = i + 0x1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; 10 > i; i = 1 + i { // want `for loop can use an int range` + for i := 0; 10 > i; i = 1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; 10 > i; i = 0x1 + i { // want `for loop can use an int range` + for i := 0; 10 > i; i = 0x1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } const x = 10 @@ -78,51 +78,51 @@ func main() { for i := 0; i < x; i += 2 { } - for i := 0; i < x; i++ { // want `for loop can use an int range` + for i := 0; i < x; i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := uint32(0); i < uint32(x); i++ { // want `for loop can use an int range` + for i := uint32(0); i < uint32(x); i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0x0; i < x; i++ { // want `for loop can use an int range` + for i := 0x0; i < x; i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < x; i += 1 { // want `for loop can use an int range` + for i := 0; i < x; i += 1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < x; i += 0x1 { // want `for loop can use an int range` + for i := 0; i < x; i += 0x1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < x; i = i + 1 { // want `for loop can use an int range` + for i := 0; i < x; i = i + 1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < x; i = i + 0x1 { // want `for loop can use an int range` + for i := 0; i < x; i = i + 0x1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < x; i = 1 + i { // want `for loop can use an int range` + for i := 0; i < x; i = 1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; i < x; i = 0x1 + i { // want `for loop can use an int range` + for i := 0; i < x; i = 0x1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; x > i; i++ { // want `for loop can use an int range` + for i := 0; x > i; i++ { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; x > i; i += 1 { // want `for loop can use an int range` + for i := 0; x > i; i += 1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; x > i; i += 0x1 { // want `for loop can use an int range` + for i := 0; x > i; i += 0x1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; x > i; i = i + 1 { // want `for loop can use an int range` + for i := 0; x > i; i = i + 1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; x > i; i = i + 0x1 { // want `for loop can use an int range` + for i := 0; x > i; i = i + 0x1 { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; x > i; i = 1 + i { // want `for loop can use an int range` + for i := 0; x > i; i = 1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } - for i := 0; x > i; i = 0x1 + i { // want `for loop can use an int range` + for i := 0; x > i; i = 0x1 + i { // want `for loop can be changed to use an integer range \(Go 1\.22\+\)` } }