Skip to content

Commit a36df4d

Browse files
internal/lint: add linter rule to disallow panic(fmt.Sprintf())
Add a lint check that disallows panic(fmt.Sprintf(...)) in favor of panic(errors.AssertionFailedf(...)). Using errors.AssertionFailedf ensures panic messages are not redacted in CockroachDB logs and include proper stack traces. The rule supports a lint:ignore PanicFmtSprintf directive for cases where the pattern is intentionally retained. Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
1 parent 0e1a672 commit a36df4d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

internal/lint/lint_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ func TestLint(t *testing.T) {
191191
}
192192
})
193193

194+
// Disallow panic(fmt.Sprintf(...)); use panic(errors.AssertionFailedf(...))
195+
// instead so that panic messages are not redacted in CockroachDB logs and
196+
// include proper stack traces.
197+
t.Run("TestPanicFmtSprintf", func(t *testing.T) {
198+
t.Parallel()
199+
200+
if err := stream.ForEach(
201+
stream.Sequence(
202+
dirCmd(t, pkg.Dir, "git", "grep", "-B1", `panic(fmt\.Sprintf(`, "--", "*.go"),
203+
lintIgnore("lint:ignore PanicFmtSprintf"),
204+
stream.GrepNot(`^internal/lint/lint_test.go`),
205+
), func(s string) {
206+
t.Errorf("\n%s <- please use \"panic(errors.AssertionFailedf(...))\" instead", s)
207+
}); err != nil {
208+
t.Error(err)
209+
}
210+
})
211+
194212
t.Run("TestFmtErrorf", func(t *testing.T) {
195213
t.Parallel()
196214

0 commit comments

Comments
 (0)