Skip to content
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
2 changes: 1 addition & 1 deletion pkg/linters/bytesbufferstring/bytesbufferstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func matchBufBytesArg(pass *analysis.Pass, call *ast.CallExpr) (*ast.CallExpr, *
if !ok {
return nil, nil, false
}
sel, ok := inner.Fun.(*ast.SelectorExpr)
sel, ok := astutil.UnwrapParenExpr(inner.Fun).(*ast.SelectorExpr)
if !ok || sel.Sel.Name != "Bytes" {
return nil, nil, false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func BadParenStringOfBytesCall() string {
return string((buf.Bytes())) // want `string\(buf\.Bytes\(\)\) can be simplified to buf\.String\(\)`
}

func BadParenBytesCallee() string {
var buf bytes.Buffer
buf.WriteString("hello")
return string((buf.Bytes)()) // want `string\(buf\.Bytes\(\)\) can be simplified to buf\.String\(\)`
}

func BadStringOfBytesCallPtr() string {
buf := &bytes.Buffer{}
buf.WriteString("world")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func BadParenStringOfBytesCall() string {
return buf.String() // want `string\(buf\.Bytes\(\)\) can be simplified to buf\.String\(\)`
}

func BadParenBytesCallee() string {
var buf bytes.Buffer
buf.WriteString("hello")
return buf.String() // want `string\(buf\.Bytes\(\)\) can be simplified to buf\.String\(\)`
}

func BadStringOfBytesCallPtr() string {
buf := &bytes.Buffer{}
buf.WriteString("world")
Expand Down
6 changes: 4 additions & 2 deletions pkg/linters/goroutineresourceleak/goroutine_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"go/types"

"golang.org/x/tools/go/analysis"

"github.com/github/gh-aw/pkg/linters/internal/astutil"
)

func buildParentMap(files []*ast.File) map[ast.Node]ast.Node {
Expand Down Expand Up @@ -131,7 +133,7 @@ func hasWaitGroupMethod(pass *analysis.Pass, node ast.Node, method string, key r
if !ok {
return true
}
selector, ok := call.Fun.(*ast.SelectorExpr)
selector, ok := astutil.UnwrapParenExpr(call.Fun).(*ast.SelectorExpr)
if !ok || selector.Sel.Name != method {
return true
}
Expand All @@ -155,7 +157,7 @@ func waitGroupMethodKeys(pass *analysis.Pass, node ast.Node, method string, befo
if !ok {
return true
}
selector, ok := call.Fun.(*ast.SelectorExpr)
selector, ok := astutil.UnwrapParenExpr(call.Fun).(*ast.SelectorExpr)
if !ok || selector.Sel.Name != method || (before.IsValid() && call.Pos() >= before) {
return true
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/linters/goroutineresourceleak/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ func goroutineInLoopWithWaitGroup() {
wg.Wait()
}

func goroutineInLoopWithParenWaitGroup() {
var wg sync.WaitGroup
for i := 0; i < 1; i++ {
(wg.Add)(1)
go func() {
defer wg.Done()
}()
}
(wg.Wait)()
}

func goroutineInLoopWithImmediateExit() {
for range []int{1} {
go doSomeWorkWithID(1)
Expand Down
16 changes: 14 additions & 2 deletions pkg/linters/slicemakezerolength/slicemakezerolength.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {

Copy link
Copy Markdown
Contributor Author

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 unlabeledStmt in pkg/linters/bufioscannererunchecked/bufioscannererunchecked.go; move one copy to pkg/linters/internal/astutil and use it in both linters, so the same label handling only needs to be maintained once.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/codebase-design] unlabeledStmt is now a copy of the helper in bufioscannererunchecked (same body, different package). Two copies of label-stripping logic will drift apart. Consider moving it into pkg/linters/internal/astutil next to UnwrapParenExpr and calling it from both linters.

💡 Suggested change

Add an exported helper such as UnwrapLabeledStmt(stmt ast.Stmt) ast.Stmt to astutil.go with this body, delete both local copies, and call the shared helper at the range check. A small unit test in astutil_test.go can cover it.

@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 {
Expand Down Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ func badParenMakeCallee(items []string) {
_ = s
}

func badLabeledRange(items []string) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 outer: range loop whose body appends two elements or whose range expression references the slice being built. This guards the label-stripping path against over-matching, not just against missed matches.

💡 Suggested test

A labeled loop with a body that fails appendsOneElement should produce no diagnostic. Put it next to badLabeledRange with no want comment.

@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)
Expand Down
Loading