TylerDavidBailey/duperr

GitHub: TylerDavidBailey/duperr

duperr 是一个 Go 语言 linter,用于检测同一 package 内重复的 error message,帮助开发者在调试时精确追溯错误的来源调用点。

Stars: 0 | Forks: 0

# duperr [![CI](https://static.pigsec.cn/wp-content/uploads/repos/cas/ad/ad5834178f7599af9fdda11629d49cae07f2997beec49821b2920eff5bfd50e7.svg)](https://github.com/TylerDavidBailey/duperr/actions/workflows/ci.yml) [![Go Reference](https://pkg.go.dev/badge/github.com/TylerDavidBailey/duperr.svg)](https://pkg.go.dev/github.com/TylerDavidBailey/duperr) [![Go Report Card](https://goreportcard.com/badge/github.com/TylerDavidBailey/duperr)](https://goreportcard.com/report/github.com/TylerDavidBailey/duperr) [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) 一个 Go linter,用于报告同一个 package 内重复的 error message。 ``` var errDial = errors.New("connecting to db") ... var errPing = errors.New("connecting to db") // duplicate error message "connecting to db" (first used at dial.go:11) ``` 在 debug 时,由相同的 message 构建的两个 error 是无法区分的:携带该 message 的一条 log 或一个 test failure 无法追溯到一个单一的 call site。现有的 linter 无法捕获这种情况——`dupl` 作用于代码片段,而 `goconst` 建议了相反的修复方式(共享该字符串)。成熟的代码库中会积累此类问题:duperr 在 Caddy、Hugo 和 Prometheus 中分别报告了 20 到 139 处重复。一个典型的发现,来自 Hugo 的 config loader: ``` configs, err = fromLoadConfigResult(d.Fs, d.Logger, res) if err != nil { return nil, fmt.Errorf("failed to create config from modules config: %w", err) } if err := configs.transientErr(); err != nil { return nil, fmt.Errorf("failed to create config from modules config: %w", err) } ``` 当该 message 出现在 log 中时,无法判断这两个调用中是哪一个失败了。 ## 它检查什么 - 传递给 `errors.New` 和 `fmt.Errorf` 的常量 message 字符串,按 package 进行比较。具名常量和常量拼接会被计算在内;动态 message 会被忽略。 - `fmt.Errorf` 的 format string 仅在不包含除 `%w` 之外的动词时才会进行比较——动态动词(`%s`、`%d` 等)已经使得生成的 message 在 runtime 时各不相同。 - 以 `_test.go` 结尾的文件和生成的文件会被跳过。 第一个之后的每次出现都会被报告,并指回第一个。 ## 用法 ### 与 `go vet` 一起使用 ``` go install github.com/TylerDavidBailey/duperr/cmd/duperr@latest go vet -vettool=$(which duperr) ./... ``` ### 与 golangci-lint (module plugin) 一起使用 duperr 将自身注册为 [module plugin](https://golangci-lint.run/plugins/module-plugins/)。 在您的 `.golangci.yml` 旁边创建 `.custom-gcl.yml`: ``` version: v2.12.2 plugins: - module: github.com/TylerDavidBailey/duperr import: github.com/TylerDavidBailey/duperr/plugin version: v0.1.0 ``` 使用 `golangci-lint custom` 构建一次自定义二进制文件,然后在 `.golangci.yml` 中启用该 linter: ``` linters: enable: - duperr settings: custom: duperr: type: module description: reports duplicate error messages within a package ``` 运行构建好的二进制文件(默认为 `./custom-gcl`)而不是 `golangci-lint`。 ## AI 使用情况 此代码库的某些部分是在 AI (Anthropic 的 Claude) 的协助下编写的。所有更改均经过人工审查,并由 test suite 覆盖。 ## 许可证 [MIT](LICENSE)
标签:EVTX分析, Go, Linter, pptx, Ruby工具, SOC Prime, 云安全监控, 代码规范, 开发工具, 日志审计, 静态分析