# Lint（软件）

Lint 是一种静态分析工具，它在不运行源代码的情况下检查源代码，标记可疑结构、风格违规和可能存在的缺陷。该名称来自最初的 `lint`——一款由斯蒂芬·C·约翰逊（Stephen C. Johnson）在[贝尔实验室](https://www.edgepedia.org/zh/bell-labs)（Bell Laboratories）编写、于 1978 年发布的 C 程序检查器；此后，它成为一类称为 lint 工具（linters）的工具的统称。<sup>[1](https://www.cs.tufts.edu/comp/150IPL/papers/nethercote03valgrind.pdf)</sup><sup> • </sup><sup>[2](https://research.cs.wisc.edu/mist/SoftwareSecurityCourse/Chapters/36-StaticAnalysisTools.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 原始工具 | `lint`，斯蒂芬·C·约翰逊 1978 年在 AT&T 贝尔实验室（AT&T Bell Laboratories）编写的 C 程序检查器<sup>[1](https://www.cs.tufts.edu/comp/150IPL/papers/nethercote03valgrind.pdf)</sup> |
| 目的 | 通过执行当时的编译器不会执行的检查来补充编译器，例如可疑的可移植性问题与类型不匹配问题<sup>[2](https://research.cs.wisc.edu/mist/SoftwareSecurityCourse/Chapters/36-StaticAnalysisTools.pdf)</sup> |
| 现代示例 | C++ 的 clang-tidy、JavaScript 的 ESLint、Go 的 go vet<sup>[3](https://clang.llvm.org/extra/clang-tidy/)</sup><sup> • </sup><sup>[4](https://eslint.org/docs/latest/use/configure/plugins)</sup><sup> • </sup><sup>[5](https://pkg.go.dev/github.com/golangci/govet)</sup> |
| 典型发现 | 风格违规、接口误用、未使用参数、低效操作，以及可通过静态分析推断出的缺陷<sup>[3](https://clang.llvm.org/extra/clang-tidy/)</sup> |
| 主要局限 | 误报（false positives）；在一项工业研究中，433 个静态分析告警中有 328 个是误报，每个误报大约耗费 10 到 20 分钟人工审查<sup>[6](https://arxiv.org/abs/2601.18844)</sup> |
| 与格式化工具的区别 | Prettier 等格式化工具（formatters）会重新输出代码以保持风格一致；lint 工具则捕获缺陷和代码质量问题<sup>[7](https://prettier.io/docs/comparison)</sup> |

## 最初的 lint

最初的 C 编译器对所接受的代码检查得相对很少，因此，1978 年，贝尔实验室的计算机科学家斯蒂芬·C·约翰逊推出了 lint，作为一款在编译之外对 C 代码执行多种检查的工具。<sup>[2](https://research.cs.wisc.edu/mist/SoftwareSecurityCourse/Chapters/36-StaticAnalysisTools.pdf)</sup> 约翰逊在 1978 年 7 月的一篇论文中描述了该工具的用途与实现；它随后被大量程序员使用，并启发了后来的静态分析工具。<sup>[8](https://projekter.aau.dk/projekter/files/422795230/thesis_theodor_constantin.pdf)</sup>

从机制上说，lint 构建在可移植 C 编译器（Portable C Compiler）之上，后者也是 IBM 370、[霍尼韦尔](https://www.edgepedia.org/zh/honeywell) 6000（Honeywell 6000）和 Interdata 8/32 的 C 编译器所共用的前端。这些编译器会为代码生成器写出一个中间文件，而 lint 则生成一个由 ASCII 文本行组成的中间文件，每一行记录一个外部变量名、该变量出现上下文（使用、定义、声明等）的编码、一个类型说明符，以及源文件名和行号。<sup>[9](https://bitsavers.trailing-edge.com/pdf/att/unix/Release_4.0_1981/UNIX_Release_4.0_Volume_1_198101.pdf)</sup> 约翰逊的报告《Lint：C 程序检查器》（Lint, a C Program Checker）于 1977 年 12 月作为《贝尔实验室计算机科学技术报告第 65 号》（Bell Laboratories Computer Science Technical Report 65）发表，并收录于《Unix 程序员手册》（Unix Programmer's Manual）。<sup>[1](https://www.cs.tufts.edu/comp/150IPL/papers/nethercote03valgrind.pdf)</sup><sup> • </sup><sup>[10](https://www2.dmst.aueb.gr/dds/bib/unix.htm)</sup>

## lint 工具检查什么

现代 lint 工具会检查代码中几大类问题。基于 Clang 的 C++ lint 工具 clang-tidy 将其目的描述为诊断并修复典型的编程错误，例如风格违规、接口误用，以及可通过静态分析推断出的缺陷。<sup>[3](https://clang.llvm.org/extra/clang-tidy/)</sup> 它的各个检查项体现了其覆盖范围：`misc-unused-parameters` 查找未使用的函数参数；如果本应使用另一个参数，这可能意味着缺陷；`performance-inefficient-vector-operation` 查找会导致反复重新分配内存的 `push_back` 循环，并建议调用 `reserve`；`misc-definitions-in-headers` 查找头文件中的非内联定义；当这些头文件被多个翻译单元包含时，这类定义可能违反单一定义规则（one-definition rule）。<sup>[11](https://clang.llvm.org/extra/clang-tidy/checks/misc/unused-parameters.html)</sup><sup> • </sup><sup>[12](https://clang.llvm.org/extra/clang-tidy/checks/performance/inefficient-vector-operation.html)</sup><sup> • </sup><sup>[13](https://clang.llvm.org/extra/clang-tidy/checks/misc/definitions-in-headers.html)</sup>

有些检查项超出了模式匹配的范围。clang-tidy 的检查项 `bugprone-unchecked-optional-access` 使用流敏感静态分析来判断是否在未先检查的情况下访问了类似 `std::optional` 的值；其内存和 CPU 开销高于普通检查项。<sup>[14](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/unchecked-optional-access.html)</sup> 独立的 Clang 静态分析器（Clang Static Analyzer）实现了基于符号执行的路径敏感、过程间分析，用于查找 C、C++ 和 [Objective-C](https://www.edgepedia.org/zh/objective-c) 程序中的缺陷；clang-tidy 也可以运行它的检查项。<sup>[15](https://clang.llvm.org/docs/ClangStaticAnalyzer.html)</sup>

## 配置与可扩展性

**检查项选择。** clang-tidy 将其检查项组织为带前缀的命名组，例如 `bugprone-`、`performance-`、`readability-`、`portability-` 和 `clang-analyzer-`，并通过一个以逗号分隔的正向和负向通配符模式列表来选择检查项；`-checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus*` 会禁用所有默认检查项，启用分析器检查项，然后移除其中的 C++ 检查项。<sup>[3](https://clang.llvm.org/extra/clang-tidy/)</sup><sup> • </sup><sup>[16](https://clang.llvm.org/extra/doxygen/ClangTidy_8h.html)</sup> `-fix` 选项会在检查项支持的情况下应用建议的修复；`-warnings-as-errors=` 会将选定的警告升级为错误。<sup>[3](https://clang.llvm.org/extra/clang-tidy/)</sup>

**抑制。** 由于没有哪种分析能完全精确，lint 工具提供了多种方式，用于屏蔽已知正确代码上的警告。clang-tidy 提供针对具体检查项的抑制方式（例如，在变量被移出后重新初始化它，可屏蔽 `bugprone-use-after-move`），还提供通用的 `NOLINT`、`NOLINTNEXTLINE` 和 `NOLINTBEGIN`/`NOLINTEND` 注释；这些注释可以在括号中指定具体检查项。<sup>[3](https://clang.llvm.org/extra/clang-tidy/)</sup>

**插件。** [JavaScript](https://www.edgepedia.org/zh/javascript) lint 工具 [ESLint](https://www.edgepedia.org/zh/eslint) 将可扩展性视为核心特性：插件是符合公认接口的对象，可以提供自定义规则、自定义配置、从其他类型文件中提取或预处理 JavaScript 的自定义处理器，并且自 2024 年起还可以提供语言插件，让 ESLint 能够对非 JavaScript 语言进行 lint。<sup>[4](https://eslint.org/docs/latest/use/configure/plugins)</sup><sup> • </sup><sup>[17](https://eslint.org/blog/2024/07/whats-coming-next-for-eslint/)</sup>

**配置系统。** lint 工具的配置本身也在演进。ESLint 的扁平配置（flat config）最早在 2019 年的一篇 RFC 中提出，在 2024 年 4 月 5 日发布的 ESLint v9.0.0 中成为默认配置格式；较旧的 eslintrc 格式已被弃用，并在 v10 中完全移除。<sup>[18](https://eslint.org/blog/2024/04/eslint-v9.0.0-released/)</sup><sup> • </sup><sup>[19](https://eslint.org/blog/2023/10/flat-config-rollout-plans/)</sup> clang-tidy 读取 `.clang-tidy` 配置文件，并支持在命令行上使用 YAML 或 JSON 配置。<sup>[3](https://clang.llvm.org/extra/clang-tidy/)</sup>

## lint 工具与格式化工具

lint 工具与代码格式化工具有重叠，但二者不同。[Prettier](https://www.edgepedia.org/zh/prettier) 是一款面向 JavaScript、TypeScript、CSS 及其他语言、有明确主张的格式化工具；它把 lint 规则分为两类：一类是 `max-len`、`keyword-spacing` 等格式规则，Prettier 通过一致地重新输出整个程序，使这些规则变得不必要；另一类是 `no-unused-vars`、`prefer-promise-reject-errors` 等代码质量规则，Prettier 不会尝试处理这些规则，并称它们是 lint 工具提供的最重要规则，因为它们能够捕获真正的缺陷。<sup>[7](https://prettier.io/docs/comparison)</sup><sup> • </sup><sup>[20](https://prettier.io/docs/index.html)</sup> 它的建议是：用格式化工具负责格式，用 lint 工具负责捕获缺陷。<sup>[7](https://prettier.io/docs/comparison)</sup>

## 有效性与误报

lint 的实际价值取决于发现的缺陷与产生的噪声之间的平衡。一项针对 C、C++、JavaScript、Java 和 Go 的 lint 工具研究的综述将它们描述为轻量级静态分析工具，通常用于检查复杂代码、定位格式违规、检测漏洞和修复缺陷，同时指出特定问题常常会漏检。<sup>[21](https://www.sciencedirect.com/science/article/abs/pii/S0164121226000312)</sup>

误报是反复出现的代价。2026 年在腾讯（Tencent）开展的一项实证研究检查了某企业静态分析器产生的 433 个告警，其中 328 个是误报；开发人员对每个告警大约花费 10 到 20 分钟进行人工检查。将大语言模型与静态分析相结合的混合技术消除了 94% 至 98% 的误报，同时保持了高召回率；每个告警耗时 2.1 至 109.5 秒，花费 0.0011 至 0.12 美元，远低于人工成本。<sup>[6](https://arxiv.org/abs/2601.18844)</sup> 关于语言模型 lint 工具的研究则从另一侧追求同一方向：2024 年的一项研究训练语言模型分类器来检测代码是否有问题并对问题类型进行分类，二分类器达到 84.9% 的准确率，多标签分类器达到 83.6% 的准确率，目标是得到一种与语言无关、覆盖多种问题类型的 lint 工具。<sup>[22](https://arxiv.org/abs/2406.19508)</sup>

## References

1. [S. C. Johnson. *Lint, a C Program Checker.* AT&T Bell Laboratories, 1978 (cited in Nethercote, Valgrind technical report bibliography)](https://www.cs.tufts.edu/comp/150IPL/papers/nethercote03valgrind.pdf)
2. [Static Analysis Tools chapter, Software Security Course, University of Wisconsin-Madison](https://research.cs.wisc.edu/mist/SoftwareSecurityCourse/Chapters/36-StaticAnalysisTools.pdf)
3. [Clang-Tidy, Extra Clang Tools documentation](https://clang.llvm.org/extra/clang-tidy/)
4. [Configure Plugins, ESLint documentation](https://eslint.org/docs/latest/use/configure/plugins)
5. [govet package, Go Packages](https://pkg.go.dev/github.com/golangci/govet)
6. [Reducing False Positives in Static Bug Detection with LLMs: An Empirical Study in Industry, arXiv](https://arxiv.org/abs/2601.18844)
7. [Prettier vs. Linters, Prettier documentation](https://prettier.io/docs/comparison)
8. [Theodor Constantin, thesis section 2.1, The original lint, Aalborg University](https://projekter.aau.dk/projekter/files/422795230/thesis_theodor_constantin.pdf)
9. [UNIX Release 4.0, Volume 1 (1981), LINT manual page, bitsavers](https://bitsavers.trailing-edge.com/pdf/att/unix/Release_4.0_1981/UNIX_Release_4.0_Volume_1_198101.pdf)
10. [Unix bibliography, Diomidis Spinellis](https://www2.dmst.aueb.gr/dds/bib/unix.htm)
11. [clang-tidy misc-unused-parameters](https://clang.llvm.org/extra/clang-tidy/checks/misc/unused-parameters.html)
12. [clang-tidy performance-inefficient-vector-operation](https://clang.llvm.org/extra/clang-tidy/checks/performance/inefficient-vector-operation.html)
13. [clang-tidy misc-definitions-in-headers](https://clang.llvm.org/extra/clang-tidy/checks/misc/definitions-in-headers.html)
14. [clang-tidy bugprone-unchecked-optional-access](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/unchecked-optional-access.html)
15. [Clang Static Analyzer documentation](https://clang.llvm.org/docs/ClangStaticAnalyzer.html)
16. [ClangTidy.h File Reference, clang-tools doxygen](https://clang.llvm.org/extra/doxygen/ClangTidy_8h.html)
17. [What's coming next for ESLint, ESLint blog](https://eslint.org/blog/2024/07/whats-coming-next-for-eslint/)
18. [ESLint v9.0.0 released, ESLint blog](https://eslint.org/blog/2024/04/eslint-v9.0.0-released/)
19. [Flat config rollout plans, ESLint blog](https://eslint.org/blog/2023/10/flat-config-rollout-plans/)
20. [What is Prettier?, Prettier documentation](https://prettier.io/docs/index.html)
21. [An empirical assessment of go linters on real-world issues, ScienceDirect](https://www.sciencedirect.com/science/article/abs/pii/S0164121226000312)
22. [Code Linting using Language Models, arXiv](https://arxiv.org/abs/2406.19508)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/lint-software

English original: https://www.edgepedia.org/lint-software
