
【免费下载链接】foundryFoundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.项目地址: https://gitcode.com/GitHub_Trending/fo/foundrySeverity:High | Med | Low | Info | Gas | CodeSizeID:str_idWhat it doesExplain precisely what the lint flags.Why is this bad?Explain the impact (security, correctness, gas, readability).Example// triggering exampleUse instead:// non-triggering, recommended exampleConfigurationDocument any inline-config orfoundry.tomloptions that affect this lint. Omit this section when the lint has no additional configuration.### 结构校验的硬性规则 建议之外还有一组**被测试强制执行的硬性规则**。从 [crates/lint/src/sol/mod.rs](https://link.gitcode.com/i/db6f3449f79070650db74575ca5632c1) 的 validate_doc 函数可以看到文档结构必须满足 - **一个 H1 标题**标题非空且全文只允许这一个 # 标题 - **元数据紧跟标题**第二行必须是 **Severity**: \级别\且级别必须与注册 lint 的 severity 完全一致第三行必须是 **ID**: \str_id\且 ID 必须与注册 ID、文件名三者一致 - **正文以 ## What it does 开头**不允许在它之前有任何导读摘要式的段落 - **恰好一个理由章节**在 Why is this bad? 与 Why restrict this? 中二选一不能同时出现也不能都没有 - **## Example 必须包含 Use instead: 分隔符**且该分隔符必须恰好出现一次 - **Example 两侧各需一个非空的 solidity 代码围栏** - **What it does 与理由章节必须包含解释性文字**不能只有代码块 - **可选章节白名单**Configuration、Notes、Limitations、Known limitations 是仅有的可选章节且一旦出现就必须非空出现其他章节名如 Scope and controls会直接导致测试失败 - **禁止 Bad/Good 小节标题**示例的对比必须用 Use instead:不能用 ### Bad、### Good 这类标题 - **元数据位置唯一****Severity**、**ID** 只允许出现在标题下方正文中出现会报错 - 检查只验证**结构**不验证解释文字或 Solidity 示例的语义正确性——示例是否真的触发该 lint、建议的改法是否语义等价仍需人工把关。 ## 面向 lint 用户的内容写作规范 文档是写给 **lint 使用者**看的不是写给实现者看的。为此crates/lint/docs/README.md 明确了一系列写作准则 - **What it does**精确描述实际检查的条件包括相关的排除项例如 零值调用、构造函数内调用、带明确 gas 上限的调用被排除而不是解释检测器如何实现 - **Why is this bad? / Why restrict this? 的选择依据**按**目的**而非严重级别选择。属于安全性、正确性或 gas 类问题用 Why is this bad?属于风格或策略类、且允许合理取舍的检查改用 Why restrict this? 并解释权衡。例如 [unsafe-cheatcode.md](https://link.gitcode.com/i/ccc00a6f00a2a98230f6badcb42bfaec) 与 [missing-inheritance.md](https://link.gitcode.com/i/9269cd0071be852949eed471037f4419) 都采用 Why restrict this?因为禁用不安全 cheatcode显式继承接口属于策略性约定而非必然缺陷 - **Example**给出一个简短的触发示例随后是 Use instead: 和对应的非触发替代写法。在可能的情况下保持原行为不变如果建议的改法改变了行为要解释权衡。必要时注明所需的上下文、导入、编译器版本或 lint 配置。如果局部抑制inline suppression才是合适方案先解释被标记代码为何可接受再展示抑制写法 - **正文中的代码一律使用反引号包裹** - **安全、正确性、gas 声明要具体**说明限制条件而不是暗示每条匹配都是 bug或每个建议改动总是安全的 - **不要写实现细节**省略 AST/HIR 细节、别名追踪、遍历规则、分析预算、诊断落点、与其他检测器的对比。仅在影响读者理解或处理告警时才保留注意事项如误报说明、漏检情形不要把有限的检查写成完全覆盖。 ## 从真实规则页面看规范落地 仓库中 90 余个规则页面完整落实了上述规范。以高严重级别示例 [reentrancy-eth.md](https://link.gitcode.com/i/e453ed3f8fd40dfc30e598ffef7a49a7) 为例 solidity function withdraw() external { uint256 amount balances[msg.sender]; (bool ok, ) payable(msg.sender).call{value: amount}(); require(ok, transfer failed); balances[msg.sender] 0; } Use instead先写状态、后发调用 solidity function withdraw() external { uint256 amount balances[msg.sender]; balances[msg.sender] 0; (bool ok, ) payable(msg.sender).call{value: amount}(); require(ok, transfer failed); } 页面在 What it does 中精确说明触发条件调用前读取的状态变量在调用后被写入并在 Why is this bad? 中解释低层 call 默认转发全部剩余 gas、恶意接收方可在状态更新前重入排除项也一一列明仅事件顺序问题、无关状态写入、零值调用、构造函数内调用、带具体 gas 上限的调用被排除并特别提醒gas 上限本身不是重入防护。 再看 Gas 级别示例 [costly-loop.md](https://link.gitcode.com/i/14ac80bac2b1d11f82777c54d7618e07)它在示例代码中直接以注释标出每轮迭代一次 SSTORE与循环后单次 SSTORE的差异让读者直观理解收益来源。 ## 带配置的 lint 如何写 Configuration 章节 当 lint 支持额外配置时文档必须提供 ## Configuration 章节说明影响该 lint 的内联配置或 foundry.toml 选项没有额外配置时则省略该章节。 仓库中典型例子是命名类 lint 的 mixed_case_exceptions 配置见 [mixed-case-function.md](https://link.gitcode.com/i/0839fd2952922fab171591cb5154ae40) toml [lint.lint_specific] mixed_case_exceptions [ERC, URI, NFT] 另一个例子是 [multi-contract-file.md](https://link.gitcode.com/i/ac80482c9997117d5a66996f4b8b7625) 的 multi_contract_file_exceptions toml [lint.lint_specific] multi_contract_file_exceptions [interface, library, abstract_contract] 这两项配置的默认值可以在 [crates/config/src/lint.rs](https://link.gitcode.com/i/6d95c3596b1bc6be6488e093e381d6d3) 的 LintSpecificConfig 中找到mixed_case_exceptions 默认允许 ERC、URI、ID、URL、API、JSON、XML、HTML、HTTP、HTTPS 等常见大写缩写以允许 rescueERC20、ERC721TokenReceiver、tokenURI 这类命名而 multi_contract_file_exceptions 默认为空数组即所有类型的多合约定义都会被标记且普通 contract 永远无法豁免——该逻辑由 is_exempted 方法实现[crates/config/src/lint.rs](https://link.gitcode.com/i/6d95c3596b1bc6be6488e093e381d6d3#L94-L106)。 ## 文档与 lint 元数据如何关联源码验证 文档与规则元数据的关联在 CI 中由两个测试把关位于 [crates/lint/src/sol/mod.rs](https://link.gitcode.com/i/db6f3449f79070650db74575ca5632c1) - registered_lints_have_docs遍历 all_lints()逐一断言存在 docs/id.md 且能通过 validate_doc 校验同时反向扫描 docs/ 目录任何未被注册的 .md 页面排除 README、_template都会报 no registered lint - registered_lints_have_canonical_help_url断言每个 lint 的 help() 恰好等于 https://getfoundry.sh/forge/linting/id与 declare_forge_lint! 宏生成的 URL 保持一致。 validate_doc 还内置了容错能力支持 3 个反引号以上长度的围栏 【免费下载链接】foundryFoundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.项目地址: https://gitcode.com/GitHub_Trending/fo/foundry创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考