google / google/google-java-format

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

Đang mở
#1,101 3 bình luận 0 reaction 0 người được giao Xem trên GitHub
IntelliJ
Ngôn ngữ chính
Java
Star
6.2k
Fork
937
Merge trung bình
6 phút
Pull request đã merge (30 ngày)
3

Mô tả

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)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.