Code Review v2
Intelligent Review Workflow
Phase 1: Context Analysis
Before reviewing code, understand the context:
- 1. Identify change scope: What files/modules are affected?
- Understand intent: Read PR description, linked issues, commit messages
- Assess risk level:
-🔴High
: Core logic, auth, payment, data handling, public APIs
-Medium
: Business logic, internal APIs, utilities
-Low
: Tests, docs, config, minor refactoring
- 4. Determine review depth based on risk level
Phase 2: Automated Analysis
Run these checks mentally before manual review:
CODEBLOCK0
Phase 3: Manual Review
Follow the
CHECKLIST.md for systematic review.
Phase 4: Feedback Generation
Use templates from
TEMPLATES.md for structured feedback.
Quick Reference: Review Dimensions
| Dimension | Focus Area | Key Questions |
|---|
| Correctness | Logic, edge cases | Does it work correctly in all scenarios? |
| Security |
Vulnerabilities, data protection | Are there security risks or data leaks? |
|
Performance | Efficiency, resource usage | Will this scale? Any bottlenecks? |
|
Maintainability | Readability, structure | Can others understand and modify this? |
|
Observability | Logging, monitoring, debugging | Can we detect and diagnose issues? |
|
Testing | Coverage, quality | Are changes adequately tested? |
Severity Classification
| Level | Icon | When to Use | Response Required |
|---|
| Blocker | Security vulnerability, data loss, crash | Must fix before merge |
| Critical |
🔴 Bug, incorrect logic, broken functionality | Must fix before merge |
|
Warning | Code smell, suboptimal pattern, minor issue | Should address |
|
Suggestion |💡 Improvement opportunity, alternative approach | Consider |
|
Info |ℹ | Observation, documentation note | Optional |
Language-Specific Quick Checks
Python
CODEBLOCK1
JavaScript / TypeScript
CODEBLOCK2
Java
CODEBLOCK3
Go
CODEBLOCK4
Rust
CODEBLOCK5
C#
CODEBLOCK6
Ruby
CODEBLOCK7
PHP
□ Type declarations on parameters/returns
□ Prepared statements (no SQL injection)
□ Proper error handling (try-catch)
□ No global state
□ PSR standards followed
Common Anti-Patterns to Flag
Security
- - Hardcoded credentials or API keys
- SQL/NoSQL injection via string interpolation
- XSS via unescaped output
- Insecure deserialization
- Missing rate limiting on public endpoints
- Overly permissive CORS configuration
Performance
- - N+1 query patterns
- Unbounded result sets (missing LIMIT)
- Synchronous operations in hot paths
- Missing caching for expensive computations
- Inefficient data structures (O(n²) where O(n log n) possible)
- Memory leaks (unclosed resources, growing caches)
Maintainability
- - God classes or functions over 50 lines
- Deep nesting (3+ levels)
- Magic numbers without constants
- Duplicated logic across files
- Tight coupling between modules
- Missing or outdated documentation
Concurrency
- - Race conditions on shared state
- Missing synchronization primitives
- Deadlock potential (lock ordering)
- Thread-unsafe collections
- Improper async/await usage
Review Output Formats
Format 1: Markdown (Default)
See
TEMPLATES.md for detailed markdown templates.
Format 2: JSON (Machine-Readable)
CODEBLOCK9
Format 3: Checklist Report
Review Summary for PR #123
==========================
[✓] Correctness - 2 issues found
[✓] Security - 1 critical issue
[✓] Performance - No issues
[✓] Maintainability - 3 suggestions
[✓] Testing - Coverage adequate
[✓] Observability - Missing error context
Recommendation: Changes required before merge
Additional Resources
代码审查 v2
智能审查工作流
阶段一:上下文分析
在审查代码前,理解上下文:
- 1. 识别变更范围:哪些文件/模块受到影响?
- 理解意图:阅读PR描述、关联问题、提交信息
- 评估风险等级:
-🔴高风险
:核心逻辑、认证、支付、数据处理、公共API
-中等风险
:业务逻辑、内部API、工具类
-低风险
:测试、文档、配置、小型重构
- 4. 根据风险等级确定审查深度
阶段二:自动化分析
在手动审查前,在脑海中运行以下检查:
□ 静态分析模式(类型不匹配、未使用导入、死代码)
□ 安全模式(注入、不安全反序列化、硬编码密钥)
□ 性能模式(N+1查询、无界循环、缺少索引)
□ 并发模式(竞态条件、死锁、缺少同步)
阶段三:手动审查
遵循
CHECKLIST.md进行系统性审查。
阶段四:反馈生成
使用
TEMPLATES.md中的模板生成结构化反馈。
快速参考:审查维度
| 维度 | 关注点 | 关键问题 |
|---|
| 正确性 | 逻辑、边界情况 | 在所有场景下都能正确运行吗? |
| 安全性 |
漏洞、数据保护 | 是否存在安全风险或数据泄露? |
|
性能 | 效率、资源使用 | 能否扩展?是否存在瓶颈? |
|
可维护性 | 可读性、结构 | 其他人能理解和修改吗? |
|
可观测性 | 日志、监控、调试 | 能否检测和诊断问题? |
|
测试 | 覆盖率、质量 | 变更是否经过充分测试? |
严重程度分类
| 级别 | 图标 | 使用场景 | 响应要求 |
|---|
| 阻塞 | 安全漏洞、数据丢失、崩溃 | 合并前必须修复 |
| 严重 |
🔴 错误、逻辑不正确、功能损坏 | 合并前必须修复 |
|
警告 | 代码异味、次优模式、小问题 | 应处理 |
|
建议 |💡 改进机会、替代方案 | 考虑 |
|
信息 |ℹ | 观察、文档说明 | 可选 |
语言特定快速检查
Python
□ 公共函数有类型提示
□ 无可变默认参数
□ 使用上下文管理器管理资源
□ 使用f-string而非.format()或%
□ 正确的异常处理(非裸except)
□ init.py导出是有意为之
JavaScript / TypeScript
□ async/await配合try-catch(无未处理的promise)
□ 无隐式any(TypeScript严格模式)
□ 正确处理null/undefined
□ React中无直接DOM操作
□ 列表渲染中的key
□ useEffect依赖完整
Java
□ 对Closeable使用try-with-resources
□ 对可空返回值使用Optional
□ 正确实现equals/hashCode
□ 无原始类型(使用泛型)
□ 适当使用Stream API
□ 考虑线程安全
Go
□ 错误处理(不忽略)
□ 使用defer进行清理
□ Context作为第一个参数传递
□ 无goroutine泄漏
□ 正确使用互斥锁
□ go vet和golangci-lint检查通过
Rust
□ 无不必要的克隆
□ 正确的错误类型(Result)
□ 生命周期标注正确
□ 无未经说明的unsafe块
□ 适当使用迭代器链替代循环
□ Clippy警告已处理
C#
□ async/await模式正确
□ 对IDisposable使用using语句
□ 启用可空引用类型
□ LINQ查询高效
□ 正确的异常过滤
□ 使用CancellationToken
Ruby
□ 无N+1查询(includes/eager_load)
□ 正确的错误处理(rescue)
□ 突变操作使用bang方法
□ 冻结字符串字面量
□ RuboCop检查通过
PHP
□ 参数/返回值有类型声明
□ 使用预处理语句(无SQL注入)
□ 正确的错误处理(try-catch)
□ 无全局状态
□ 遵循PSR标准
常见反模式标记
安全
- - 硬编码的凭据或API密钥
- 通过字符串插值导致的SQL/NoSQL注入
- 未转义输出导致的XSS
- 不安全反序列化
- 公共端点缺少速率限制
- CORS配置过于宽松
性能
- - N+1查询模式
- 无界结果集(缺少LIMIT)
- 热路径中的同步操作
- 缺少对昂贵计算的缓存
- 低效的数据结构(O(n²)而可能为O(n log n))
- 内存泄漏(未关闭的资源、不断增长的缓存)
可维护性
- - 超过50行的上帝类或函数
- 深度嵌套(3层以上)
- 无常量的魔法数字
- 跨文件重复的逻辑
- 模块间紧耦合
- 缺少或过时的文档
并发
- - 共享状态的竞态条件
- 缺少同步原语
- 潜在死锁(锁顺序)
- 线程不安全的集合
- 不正确的async/await使用
审查输出格式
格式一:Markdown(默认)
详细Markdown模板请参见
TEMPLATES.md。
格式二:JSON(机器可读)
json
{
summary: 简要概述,
issues: [
{
severity: 严重,
file: src/auth.py,
line: 42,
category: 安全,
message: SQL注入漏洞,
suggestion: 使用参数化查询
}
],
positive_notes: [类型提示使用得当],
recommendation: 批准但需修改
}
格式三:检查清单报告
PR #123 审查总结
==========================
[✓] 正确性 - 发现2个问题
[✓] 安全性 - 1个严重问题
[✓] 性能 - 无问题
[✓] 可维护性 - 3条建议
[✓] 测试 - 覆盖率足够
[✓] 可观测性 - 缺少错误上下文
建议:合并前需要修改
附加资源