Skip to content

Commit e8c9d81

Browse files
gz83gopherbot
authored andcommitted
go/analysis/passes/tests: Use ReportRangef to refactor some code in checkTest
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 <[email protected]> Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Tim King <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 97ea816 commit e8c9d81

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

go/analysis/passes/tests/tests.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,18 @@ func checkExampleName(pass *analysis.Pass, fn *ast.FuncDecl) {
447447
}
448448
}
449449

450+
type tokenRange struct {
451+
p, e token.Pos
452+
}
453+
454+
func (r tokenRange) Pos() token.Pos {
455+
return r.p
456+
}
457+
458+
func (r tokenRange) End() token.Pos {
459+
return r.e
460+
}
461+
450462
func checkTest(pass *analysis.Pass, fn *ast.FuncDecl, prefix string) {
451463
// Want functions with 0 results and 1 parameter.
452464
if fn.Type.Results != nil && len(fn.Type.Results.List) > 0 ||
@@ -464,12 +476,11 @@ func checkTest(pass *analysis.Pass, fn *ast.FuncDecl, prefix string) {
464476
if tparams := fn.Type.TypeParams; tparams != nil && len(tparams.List) > 0 {
465477
// Note: cmd/go/internal/load also errors about TestXXX and BenchmarkXXX functions with type parameters.
466478
// We have currently decided to also warn before compilation/package loading. This can help users in IDEs.
467-
// TODO(adonovan): use ReportRangef(tparams).
468-
pass.Reportf(fn.Pos(), "%s has type parameters: it will not be run by go test as a %sXXX function", fn.Name.Name, prefix)
479+
at := tokenRange{tparams.Opening, tparams.Closing}
480+
pass.ReportRangef(at, "%s has type parameters: it will not be run by go test as a %sXXX function", fn.Name.Name, prefix)
469481
}
470482

471483
if !isTestSuffix(fn.Name.Name[len(prefix):]) {
472-
// TODO(adonovan): use ReportRangef(fn.Name).
473-
pass.Reportf(fn.Pos(), "%s has malformed name: first letter after '%s' must not be lowercase", fn.Name.Name, prefix)
484+
pass.ReportRangef(fn.Name, "%s has malformed name: first letter after '%s' must not be lowercase", fn.Name.Name, prefix)
474485
}
475486
}

gopls/internal/test/marker/testdata/diagnostics/analyzers.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func printfWrapper(format string, args ...interface{}) {
3434
}
3535

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

4040
// timeformat

0 commit comments

Comments
 (0)