Repository navigation
fix(linters): unwrap parenthesized callees and labeled range loops #77043
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ func analyzeBlock(pass *analysis.Pass, n ast.Node, generatedFiles filecheck.Gene | |
| } | ||
| for offset, stmt := range block.List[i+1:] { | ||
| j := i + 1 + offset | ||
| if rangeStmt, ok := stmt.(*ast.RangeStmt); ok { | ||
| if rangeStmt, ok := unlabeledStmt(stmt).(*ast.RangeStmt); ok { | ||
| if hasKnownRangeSize(pass, rangeStmt.X) && !containsReference(pass, rangeStmt.X, target) && | ||
| appendsOneElement(pass, rangeStmt, target) && rangeAvailableBefore(pass, rangeStmt.X, block.List[i+1:j]) { | ||
| reportDiagnostic(pass, call, sliceType, rangeStmt.X, generatedFiles, noLintIndex) | ||
|
|
@@ -68,6 +68,18 @@ func analyzeBlock(pass *analysis.Pass, n ast.Node, generatedFiles filecheck.Gene | |
| } | ||
| } | ||
|
|
||
| // unlabeledStmt strips any labels (e.g. "outer:") from stmt so that labeled | ||
| // range loops are analyzed the same way as unlabeled ones. | ||
| func unlabeledStmt(stmt ast.Stmt) ast.Stmt { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/codebase-design] 💡 Suggested changeAdd an exported helper such as @copilot please address this. |
||
| for { | ||
| labeled, ok := stmt.(*ast.LabeledStmt) | ||
| if !ok { | ||
| return stmt | ||
| } | ||
| stmt = labeled.Stmt | ||
| } | ||
| } | ||
|
|
||
| // Moving the range expression before an early exit may evaluate it on a path | ||
| // that previously never reached the loop. | ||
| func containsEarlyExit(stmt ast.Stmt) bool { | ||
|
|
@@ -194,7 +206,7 @@ func appendsOneElement(pass *analysis.Pass, rangeStmt *ast.RangeStmt, target typ | |
| if !ok || len(call.Args) != 2 || call.Ellipsis.IsValid() || !refersTo(pass, call.Args[0], target) { //nolint:uncheckedsliceindex // len(call.Args) is checked before indexing | ||
| return false | ||
| } | ||
| ident, ok := call.Fun.(*ast.Ident) | ||
| ident, ok := astutil.UnwrapParenExpr(call.Fun).(*ast.Ident) | ||
| return ok && pass.TypesInfo.ObjectOf(ident) == types.Universe.Lookup("append") | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,6 +99,26 @@ func badParenMakeCallee(items []string) { | |
| _ = s | ||
| } | ||
|
|
||
| func badLabeledRange(items []string) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The labeled-loop fixture only covers the positive case. Add a labeled negative case that must not be reported, for example an 💡 Suggested testA labeled loop with a body that fails @copilot please address this. |
||
| s := make([]string, 0) // want `make\(\[\]string, 0\) before this range loop can use capacity len\(items\)` | ||
| outer: | ||
| for _, item := range items { | ||
| s = append(s, item) | ||
| } | ||
| if len(s) > len(items) { | ||
| goto outer // keeps the label used so the fixture compiles | ||
| } | ||
| _ = s | ||
| } | ||
|
|
||
| func badParenAppendCallee(items []string) { | ||
| s := make([]string, 0) // want `make\(\[\]string, 0\) before this range loop can use capacity len\(items\)` | ||
| for _, item := range items { | ||
| s = (append)(s, item) | ||
| } | ||
| _ = s | ||
| } | ||
|
|
||
| func suppressed(items []string) { | ||
| //nolint:slicemakezerolength | ||
| s := make([]string, 0) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should fix: This label-unwrapping helper duplicates
unlabeledStmtinpkg/linters/bufioscannererunchecked/bufioscannererunchecked.go; move one copy topkg/linters/internal/astutiland use it in both linters, so the same label handling only needs to be maintained once.