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

Catch more patterns where range len could be simpler #30

Merged
merged 1 commit into from
Sep 10, 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
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