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
6 changes: 6 additions & 0 deletions pkg/linters/deferinloop/deferinloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import (
"github.com/github/gh-aw/pkg/linters/internal/astutil"
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
"github.com/github/gh-aw/pkg/linters/internal/nolint"
"github.com/github/gh-aw/pkg/logger"
)

var log = logger.New("linters:deferinloop")

// Analyzer is the defer-in-loop analysis pass.
var Analyzer = &analysis.Analyzer{
Name: "deferinloop",
Expand All @@ -27,6 +30,8 @@ var Analyzer = &analysis.Analyzer{
}

func run(pass *analysis.Pass) (any, error) {
log.Printf("analyzing package %s", pass.Pkg.Path())

insp, err := astutil.Inspector(pass)
if err != nil {
return nil, err
Expand All @@ -51,6 +56,7 @@ func run(pass *analysis.Pass) (any, error) {
continue
}

log.Printf("flagging defer inside loop at %s", pos)
pass.ReportRangef(deferStmt,
"defer inside a loop does not execute at the end of each iteration; it runs when the enclosing function returns, which can cause resource leaks")
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/linters/excessivefuncparams/excessivefuncparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"github.com/github/gh-aw/pkg/linters/internal/astutil"
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
"github.com/github/gh-aw/pkg/linters/internal/nolint"
"github.com/github/gh-aw/pkg/logger"
)

var log = logger.New("linters:excessivefuncparams")

// DefaultMaxParams is the default maximum number of parameters allowed in a function declaration.
const DefaultMaxParams = 8

Expand All @@ -34,6 +37,8 @@ func init() {
}

func run(pass *analysis.Pass) (any, error) {
log.Printf("analyzing package %s (max-params=%d)", pass.Pkg.Path(), maxParams)

insp, err := astutil.Inspector(pass)
if err != nil {
return nil, err
Expand Down Expand Up @@ -70,6 +75,7 @@ func run(pass *analysis.Pass) (any, error) {
if nolint.HasDirective(position, noLintLinesByFile) {
return
}
log.Printf("flagging %s: %d parameters exceeds limit %d", fn.Name.Name, params, maxParams)
pass.ReportRangef(
fn.Name,
"%s has %d parameters (limit: %d); consider using an options struct",
Expand Down
7 changes: 7 additions & 0 deletions pkg/linters/httprespbodyclose/httprespbodyclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import (
"github.com/github/gh-aw/pkg/linters/internal/astutil"
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
"github.com/github/gh-aw/pkg/linters/internal/nolint"
"github.com/github/gh-aw/pkg/logger"
)

var log = logger.New("linters:httprespbodyclose")

// Analyzer is the http-resp-body-close analysis pass.
var Analyzer = &analysis.Analyzer{
Name: "httprespbodyclose",
Expand All @@ -25,6 +28,8 @@ var Analyzer = &analysis.Analyzer{
}

func run(pass *analysis.Pass) (any, error) {
log.Printf("analyzing package %s", pass.Pkg.Path())

insp, err := astutil.Inspector(pass)
if err != nil {
return nil, err
Expand Down Expand Up @@ -172,6 +177,8 @@ type respVarState struct {
}

func reportMissingDefer(pass *analysis.Pass, state *respVarState) {
log.Printf("flagging non-deferred Body.Close() at %s", pass.Fset.PositionFor(state.assignPos, false))

diag := analysis.Diagnostic{
Pos: state.assignPos,
Message: "HTTP response Body.Close() should be deferred immediately after receiving the response to prevent resource leaks",
Expand Down
7 changes: 7 additions & 0 deletions pkg/linters/httpstatuscode/httpstatuscode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import (
"github.com/github/gh-aw/pkg/linters/internal/astutil"
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
"github.com/github/gh-aw/pkg/linters/internal/nolint"
"github.com/github/gh-aw/pkg/logger"
)

var log = logger.New("linters:httpstatuscode")

var Analyzer = &analysis.Analyzer{
Name: "httpstatuscode",
Doc: "reports integer HTTP status code literals used in comparisons that should use http.Status* named constants",
Expand Down Expand Up @@ -91,6 +94,8 @@ var httpStatusNames = map[int]string{
}

func run(pass *analysis.Pass) (any, error) {
log.Printf("analyzing package %s", pass.Pkg.Path())

root, err := astutil.Root(pass)
if err != nil {
return nil, err
Expand Down Expand Up @@ -149,6 +154,8 @@ func checkAndReport(pass *analysis.Pass, lit *ast.BasicLit, noLintLinesByFile ma
return
}

log.Printf("flagging magic HTTP status code %d at %s", code, pos)

if name, ok := httpStatusNames[code]; ok {
pass.Reportf(lit.Pos(), "use %s instead of magic HTTP status code %d", name, code)
return
Expand Down
6 changes: 6 additions & 0 deletions pkg/linters/largefunc/largefunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"github.com/github/gh-aw/pkg/linters/internal/astutil"
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
"github.com/github/gh-aw/pkg/linters/internal/nolint"
"github.com/github/gh-aw/pkg/logger"
)

var log = logger.New("linters:largefunc")

// DefaultMaxLines is the default maximum number of lines allowed in a function body.
const DefaultMaxLines = 60

Expand All @@ -34,6 +37,8 @@ func init() {
}

func run(pass *analysis.Pass) (any, error) {
log.Printf("analyzing package %s (max-lines=%d)", pass.Pkg.Path(), maxLines)

insp, err := astutil.Inspector(pass)
if err != nil {
return nil, err
Expand Down Expand Up @@ -79,6 +84,7 @@ func run(pass *analysis.Pass) (any, error) {
if nolint.HasDirective(position, noLintLinesByFile) {
return
}
log.Printf("flagging %s: %d lines exceeds limit %d", name, lines, maxLines)
pass.ReportRangef(
reportNode,
"%s is %d lines long (limit: %d); consider breaking it up",
Expand Down
Loading