google / google/google-java-format

Intellij Plugin breaks method inlining and variable extraction from inside if condition

未關閉
#1,101 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
IntelliJ
主要語言
Java
星號
6.2k
分支
937
平均合併
6 分鐘
30 天內合併 PR
3

描述

Consider the following code and try inlining `isEmpty2`:
```java
import java.util.Collection;
import java.util.List;

public class InliningWithJavaFormat {
static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}

static boolean isEmpty2(Collection c) {
return isEmpty(c);
}

public static void main(String[] args){
var pojo = new MyPojo();
if (pojo != null && !isEmpty2(pojo.getCollection())) {
System.out.println("empty");
}
}

static class MyPojo {
Collection getCollection() {
return List.of();
}
}
}
```
Not only does it fail (without reporting it), but it adds an additional `if (true)`:
```java
public static void main(String[] args){
var pojo = new MyPojo();
if (pojo != null && !isEmpty2(pojo.getCollection())) {
if (true) {
System.out.println("empty");
}
}
}
```
with the plugin disabled, it works:
```java
public static void main(String[] args){
var pojo = new MyPojo();
if (pojo != null) {
Collection c = pojo.getCollection();
if (!isEmpty(c)) {
System.out.println("empty");
}
}
}
```
(not the ideal result but at least it does not fail)

In addition, when I do that on my actual project I get an error notification from the plugin and the formatting gets broken:
![error notification](https://github.com/google/google-java-format/assets/6871742/0831ae81-6bd5-4fb7-a3d2-d9537eaa589e)
(I did not try to reproduce this with an MRE, I guess it is a side effect of the first issue)

貢獻指南

開啟貢獻指南

研究方向

從 issue 中的 Java 重現開始,調查 IntelliJ plugin 的格式化與 if 條件中的方法內嵌和變數擷取之間的互動。在啟用 plugin 的情況下重現轉換,並與停用 plugin 時的結果比較;當重構不再插入不必要的 if (true)、回報錯誤或留下損壞的格式時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
java
領域
developer-experience, tooling
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。