Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DidierLoiseau opened this issue May 23, 2024 · 1 comment
Labels

Comments

@DidierLoiseau
Copy link

Consider the following code and try inlining isEmpty2:

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<String> getCollection() {
      return List.of();
    }
  }
}

Not only does it fail (without reporting it), but it adds an additional if (true):

  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:

  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
(I did not try to reproduce this with an MRE, I guess it is a side effect of the first issue)

@DidierLoiseau
Copy link
Author

I just noticed that the same issue occurs if you simply try to extract a local variable from within the if condition instead of performing an inlining. IntelliJ would normally split the if in a similar way.

I guess this is what IntelliJ tries to do under the hood for the inlining (as shown when the plugin is disabled), and the at the source of the problem.

@DidierLoiseau DidierLoiseau changed the title Intellij Plugin breaks method inlining inside if condition Intellij Plugin breaks method inlining and variable extraction from inside if condition May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants