DuplicateBranches misses duplicate ternary branches when one branch is parenthesized

未关闭 适合新手
#6,216 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java
领域
testing-qa

调研方向

找到 DuplicateBranches 检查及其分支比较,然后阅读它如何处理 ParenthesizedTree 和 ASTHelpers.stripParentheses。使用 issue 中的两个三元表达式示例添加或更新回归测试。两个示例都各自产生一个发现结果,且相关检查的测试通过,即为完成。

由索引模型根据 Issue 内容生成。

描述

Error Prone version

Error Prone 2.49.0 (javac 21.0.8, --source/--target 21)

Affected checks

DuplicateBranches

Description

Adding redundant parentheses around one branch of a conditional expression causes DuplicateBranches to miss duplicate branches. The check reports true ? 1 * 5 : 1 * 5, but does not report true ? 1 * 5 : (1 * 5), even though the two branches still compute the same expression.

Per JLS §15.8.5, parentheses around an expression do not change its value, type, or constant-expression status. Parenthesizing one branch should therefore not change whether the branches are considered duplicates.

I verified the following pair on Error Prone 2.49.0:

Check Rewrite BEFORE AFTER Effect
DuplicateBranches 1 * 5 → (1 * 5) in one branch 1 0 false negative

The branch comparison appears not to look through ParenthesizedTree. A possible fix is to unwrap the branch expressions before comparing them, for example with ASTHelpers.stripParentheses.

Reproducers
DuplicateBranches — (1 * 5) defeats the branch comparison

The two ternary branches remain identical; only one is parenthesized.

// BEFORE — 1 finding
public class Sample {
  int x;
  void m() { x = true ? 1 * 5 : 1 * 5; }
}
// AFTER — 0 findings
public class Sample {
  int x;
  void m() { x = true ? 1 * 5 : (1 * 5); }
}
Expected behavior

Both versions should produce 1 DuplicateBranches finding. The two ternary branches remain duplicates when one of them is enclosed in redundant parentheses.

Actual behavior

The BEFORE version produces 1 finding. The AFTER version produces 0 findings after the false branch 1 * 5 is parenthesized.

主要语言
Java
星标
7.3k
派生
830
平均合并
18 小时 31 分钟
30 天内合并 PR
61

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/error-prone 的其他 Issue

查看 google/error-prone 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。