Reject silent empty catch clauses
拒绝静默的空 catch 子句
An empty `catch` discards every failure of its `try`, including defects the author did not expect. The root rule requires such a clause to name the error, state why discarding it is safe, and keep its `try` to one statement, so that the clause discards only the failure its author considered. Without a check, new clauses drifted from the rule, and existing clauses broke it across many unrelated package and test owners
English
Problem
An empty catch discards every failure of its try, including defects the author did not expect. The root rule requires such a clause to name the error, state why discarding it is safe, and keep its try to one statement, so that the clause discards only the failure its author considered. Without a check, new clauses drifted from the rule, and existing clauses broke it across many unrelated package and test owners.
Decision
The checker, exposed as pnpm run verify-empty-catch, parses first-party JavaScript and TypeScript with the TypeScript syntax tree and reports an empty clause, one whose body holds no statement other than ;, that is unnamed, unexplained, or wraps a multi-statement try. A bare _ counts as unnamed; lint requires an unused binding to start with _, so a conforming name such as _alreadyGone states the expected failure. A clause body with any other statement is out of scope because syntax cannot show whether it handles the error. The checker establishes the form; review establishes that the name and the reason are true, using the guidance in dsh-error-handling.
Existing clauses have an exact baseline keyed by repository-relative path, a SHA-256 digest of the clause's syntax tokens together with its broken conditions, and occurrence count. Including the broken conditions makes turning a recorded one-statement try into a wider one, or deleting a recorded reason, a new finding, while edits inside the try that keep its conditions keep the clause's identity. The baseline follows the unknown-cast inventory, and both checkers share the inventory module: baseline edits only delete entries or decrease counts, --prune removes retired entries and refuses to write while new findings exist, and no command adds an allowance. Unlike the unknown-cast inventory, which requires resolving an assertion before moving it to an unlisted file, moving a file that holds recorded clauses renames its baseline key in the same change, because narrowing a recorded try belongs to its owner rather than to the change that moves the file.
The checker runs in the hygiene and check-all profiles and the CI static and primary modes of the gate runner. Empty Promise rejection handlers such as .catch(() => {}) are outside this rule and tracked separately in issue #8033.
Alternatives considered
Fix every existing clause before enforcing the rule. Rejected because narrowing a multi-statement try changes which failures the clause discards, which only each owner can judge. The baseline lets enforcement start while the existing clauses remain recorded findings for their owners to fix.
Fingerprint the whole try statement. Rejected because every edit inside a recorded try would become a new finding and force its owner to fix the clause during unrelated work. Fingerprinting the clause with its broken conditions rejects a new broken condition on a recorded clause without tying its identity to the statements inside the try; a try that is already wide can gain statements without a new finding.
A command that records clauses from a named revision. Rejected because a local command cannot prove that the revision is the target branch: naming the current branch would record the branch's own clauses. Once the target branch carries the checker, merging it brings that branch's baseline along, so no command is needed.
Search text for catch {}. Rejected because comments, strings, whitespace, and binding patterns defeat a text pattern, and the rule's conditions depend on the syntax tree.
Consequences
New and changed empty clauses must conform, and fixing a recorded clause requires pruning its baseline entry. A partial fix, such as adding a name while the try stays wide, changes the clause's identity and therefore fails; the owner fixes the clause completely before pruning. The baseline key carries no line number, so a recorded clause can move within its file; identical clauses in one file share one fingerprint whose count covers each of them.
Passing the checker establishes the form of every unrecorded empty clause. It does not establish that a reason is true, that a non-empty handler handles its error, or that a try holding one block or compound statement discards only one operation's failures.
中文
问题
空 catch 会丢弃其 try 的所有失败,包括作者未预料到的缺陷。根规则要求此类子句命名错误、说明丢弃该错误为何安全,并将其 try 限制为一条语句,使子句只丢弃作者考虑过的失败。没有检查时,新增子句逐渐偏离该规则,而违反规则的现有子句分布在许多互不相关的包和测试所属模块中。
决策
检查器通过 pnpm run verify-empty-catch 运行,使用 TypeScript 语法树解析第一方 JavaScript 和 TypeScript,并报告未命名、未说明原因或包裹多语句 try 的空子句;空子句指主体中除 ; 外不含任何语句的子句。单独的 _ 视为未命名;lint 要求未使用的绑定以 _ 开头,因此 _alreadyGone 这类符合要求的名称可以说明预期的失败。主体含有其他语句的子句不属于此规则的范围,因为语法无法表明它是否处理了错误。检查器确认形式;审查依据 dsh-error-handling 中的指引确认名称和原因属实。
现有子句使用精确基线记录,键由仓库相对路径、子句语法 token 连同其违反条件的 SHA-256 摘要及出现次数组成。摘要包含违反条件,因此把已记录的单语句 try 扩大为多语句 try,或删除已记录的原因,都会产生新的检查发现;而在 try 内部进行不改变其违反条件的修改,不改变子句的标识。基线沿用 unknown 类型断言清单的做法,两个检查器共用清单模块:基线修改只能删除条目或减少次数,--prune 删除已失效的条目并在存在新发现时拒绝写入,且没有任何命令可以添加放行。unknown 类型断言清单要求先解决断言,才能将其移至未登记的文件;与之不同,移动包含已记录子句的文件时,须在同一变更中重命名其基线键,因为收窄已记录的 try 由其所属模块负责,而不属于移动文件的变更。
检查器在门禁运行器的 hygiene 和 check-all 配置以及 CI 静态模式和主模式中运行。.catch(() => {}) 等空的 Promise 拒绝处理函数不属于此规则,另由 issue #8033 跟踪。
考虑过的替代方案
先修复所有现有子句,再执行规则。 不采用,因为收窄多语句 try 会改变子句丢弃哪些失败,只有各所属模块才能判断。基线使规则可以立即开始执行,同时现有子句仍作为已记录的检查发现,由其所属模块修复。
对整个 try 语句计算指纹。 不采用,因为已记录 try 内部的每次修改都会产生新的检查发现,迫使所属模块在无关工作中修复该子句。对子句连同其违反条件计算指纹,可以拒绝已记录子句上新增的违反条件,同时不让子句的标识依赖 try 内部的语句;已经过宽的 try 可以增加语句而不产生新的检查发现。
提供从指定修订版本记录子句的命令。 不采用,因为本地命令无法证明该修订版本就是目标分支:指定当前分支会记录该分支自身的子句。目标分支包含检查器后,合并目标分支会一并带入其基线,因此不需要这样的命令。
在文本中搜索 catch {}。 不采用,因为注释、字符串、空白和绑定模式都会使文本模式失效,而且规则的条件依赖语法树。
影响
新增和修改的空子句必须符合规则,修复已记录的子句时须清理其基线条目。部分修复,例如添加名称但 try 仍然过宽,会改变子句的标识,因此检查失败;所属模块须先完整修复子句,再清理基线条目。基线键不含行号,因此已记录的子句可以在所在文件内移动;同一文件中相同的子句共用一个指纹,其计数涵盖其中每一个子句。
通过检查器说明每个未记录的空子句都符合规定形式。它不证明原因属实,不证明非空处理代码处理了其错误,也不证明包含一个代码块或复合语句的 try 只丢弃一个操作的失败。