Skip to content

Commit

Permalink
Catch more patterns where range len could be simpler (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckaznocha authored Sep 10, 2024
1 parent e534483 commit 7411425
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
60 changes: 44 additions & 16 deletions intrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var (
)

const (
msg = "for loop can be changed to use an integer range (Go 1.22+)"
msgLenRange = "for loop can be changed to `i := range %s`"
msg = "for loop can be changed to use an integer range (Go 1.22+)"
msgLenRange = "for loop can be changed to `%s := range %s`"
msgLenRangeNoIdent = "for loop can be changed to `range %s`"
)

func run(pass *analysis.Pass) (any, error) {
Expand Down Expand Up @@ -243,21 +244,26 @@ func checkForStmt(pass *analysis.Pass, forStmt *ast.ForStmt) {
}

func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
if rangeStmt.Key == nil {
if rangeStmt.Value != nil {
return
}

ident, ok := rangeStmt.Key.(*ast.Ident)
if !ok {
return
}
startPos := rangeStmt.Range
usesKey := rangeStmt.Key != nil
identName := ""

if ident.Name == "_" {
return
}
if usesKey {
ident, ok := rangeStmt.Key.(*ast.Ident)
if !ok {
return
}

if rangeStmt.Value != nil {
return
if ident.Name == "_" {
usesKey = false
}

identName = ident.Name
startPos = ident.Pos()
}

if rangeStmt.X == nil {
Expand Down Expand Up @@ -295,18 +301,40 @@ func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
return
}

if usesKey {
pass.Report(analysis.Diagnostic{
Pos: startPos,
End: x.End(),
Message: fmt.Sprintf(msgLenRange, identName, arg.Name),
SuggestedFixes: []analysis.SuggestedFix{
{
Message: fmt.Sprintf("Replace `len(%s)` with `%s`", arg.Name, arg.Name),
TextEdits: []analysis.TextEdit{
{
Pos: x.Pos(),
End: x.End(),
NewText: []byte(arg.Name),
},
},
},
},
})

return
}

pass.Report(analysis.Diagnostic{
Pos: ident.Pos(),
Pos: startPos,
End: x.End(),
Message: fmt.Sprintf(msgLenRange, arg.Name),
Message: fmt.Sprintf(msgLenRangeNoIdent, arg.Name),
SuggestedFixes: []analysis.SuggestedFix{
{
Message: fmt.Sprintf("Replace `len(%s)` with `%s`", arg.Name, arg.Name),
TextEdits: []analysis.TextEdit{
{
Pos: x.Pos(),
Pos: startPos,
End: x.End(),
NewText: []byte(arg.Name),
NewText: []byte(fmt.Sprintf("range %s", arg.Name)),
},
},
},
Expand Down
22 changes: 22 additions & 0 deletions testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ func issue27() {
print(i)
}

for n := range len(someSlice) { // want "for loop can be changed to `n := range someSlice`"
print(n)
}

for _ = range len(someSlice) { // want "for loop can be changed to `range someSlice`"
}

for range len(someSlice) { // want "for loop can be changed to `range someSlice`"
}

for i := range notLen(someSlice) {
print(i)
}
Expand All @@ -256,6 +266,12 @@ func issue27() {
print(i)
}

for _ = range len(someArray) { // want "for loop can be changed to `range someArray`"
}

for range len(someArray) { // want "for loop can be changed to `range someArray`"
}

for i := range notLen(someArray) {
print(i)
}
Expand All @@ -277,6 +293,12 @@ func issue27() {
print(i)
}

for _ = range len(someMap) {
}

for range len(someMap) {
}

for i := range notLen(someMap) {
print(i)
}
Expand Down
22 changes: 22 additions & 0 deletions testdata/main.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ func issue27() {
print(i)
}

for n := range someSlice { // want "for loop can be changed to `n := range someSlice`"
print(n)
}

for range someSlice { // want "for loop can be changed to `range someSlice`"
}

for range someSlice { // want "for loop can be changed to `range someSlice`"
}

for i := range notLen(someSlice) {
print(i)
}
Expand All @@ -256,6 +266,12 @@ func issue27() {
print(i)
}

for range someArray { // want "for loop can be changed to `range someArray`"
}

for range someArray { // want "for loop can be changed to `range someArray`"
}

for i := range notLen(someArray) {
print(i)
}
Expand All @@ -277,6 +293,12 @@ func issue27() {
print(i)
}

for _ = range len(someMap) {
}

for range len(someMap) {
}

for i := range notLen(someMap) {
print(i)
}
Expand Down

0 comments on commit 7411425

Please sign in to comment.