require: fix invalid examples in doc comments (codegen)
Summary
Correct invalid examples in require package doc comments. require does not return a boolean value; its responsibility is to fail immediately.
Changes
Updated examples to reflect proper require usage without if. Added a new helper requireCommentParseIf to sanitize and transform invalid doc comments in bulk, ensuring consistency across the package.
Removed conditional forms such as:
if require.NoError(t, err) {
// ...
}
Updated examples to demonstrate proper usage of require:
// NoError asserts that a function returned no error (i.e. `nil`).
//
// actualObj, err := SomeFunction()
// require.NoError(t, err)
// require.Equal(t, expectedObj, actualObj)
func NoError(t TestingT, err error, msgAndArgs ...interface{})
Motivation
Unlike assert, require functions do not return a boolean value. The previous examples suggested patterns such as if require.NoError(t, err) { ... }, which are invalid. This PR fixes those examples and introduces an automated parser (requireCommentParseIf) to prevent such mistakes from persisting in the future.
Related issues
Closes #1776
@ccoVeille Thanks, I also felt the code was a bit messy, so I tried to simplify it further.
Regarding your comment about tests: since this is only used for code generation, I'm not sure if it makes sense to add dedicated tests here. Do you have any suggestion for a good approach?
Is there anything more I should do, or anything I might be misunderstanding?