Skip to content

Commit

Permalink
go/analysis/passes/tests: Use ReportRangef to refactor some code in c…
Browse files Browse the repository at this point in the history
…heckTest

Use ReportRangef to replace the original Reportf
to try to implement two TODOs.

Change-Id: I9dfc0f47881d7638e460164a9dc5573394b92eee
Reviewed-on: https://go-review.googlesource.com/c/tools/+/579615
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
Reviewed-by: Tim King <taking@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
gz83 authored and gopherbot committed Apr 22, 2024
1 parent 97ea816 commit e8c9d81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 15 additions & 4 deletions go/analysis/passes/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ func checkExampleName(pass *analysis.Pass, fn *ast.FuncDecl) {
}
}

type tokenRange struct {
p, e token.Pos
}

func (r tokenRange) Pos() token.Pos {
return r.p
}

func (r tokenRange) End() token.Pos {
return r.e
}

func checkTest(pass *analysis.Pass, fn *ast.FuncDecl, prefix string) {
// Want functions with 0 results and 1 parameter.
if fn.Type.Results != nil && len(fn.Type.Results.List) > 0 ||
Expand All @@ -464,12 +476,11 @@ func checkTest(pass *analysis.Pass, fn *ast.FuncDecl, prefix string) {
if tparams := fn.Type.TypeParams; tparams != nil && len(tparams.List) > 0 {
// Note: cmd/go/internal/load also errors about TestXXX and BenchmarkXXX functions with type parameters.
// We have currently decided to also warn before compilation/package loading. This can help users in IDEs.
// TODO(adonovan): use ReportRangef(tparams).
pass.Reportf(fn.Pos(), "%s has type parameters: it will not be run by go test as a %sXXX function", fn.Name.Name, prefix)
at := tokenRange{tparams.Opening, tparams.Closing}
pass.ReportRangef(at, "%s has type parameters: it will not be run by go test as a %sXXX function", fn.Name.Name, prefix)
}

if !isTestSuffix(fn.Name.Name[len(prefix):]) {
// TODO(adonovan): use ReportRangef(fn.Name).
pass.Reportf(fn.Pos(), "%s has malformed name: first letter after '%s' must not be lowercase", fn.Name.Name, prefix)
pass.ReportRangef(fn.Name, "%s has malformed name: first letter after '%s' must not be lowercase", fn.Name.Name, prefix)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func printfWrapper(format string, args ...interface{}) {
}

// tests
func Testbad(t *testing.T) { //@diag("", re"Testbad has malformed name: first letter after 'Test' must not be lowercase")
func Testbad(t *testing.T) { //@diag("Testbad", re"Testbad has malformed name: first letter after 'Test' must not be lowercase")
}

// timeformat
Expand Down

0 comments on commit e8c9d81

Please sign in to comment.