Skip to content

Bug: Formatting should result in stable output #1211

Closed

Description

@scch-michael-pfeiffer

The source code in this example has to be formatted two times until it does not change anymore.

Used command line:

java -jar google-java-format-1.25.2-all-deps.jar -r --skip-removing-unused-imports --skip-reflowing-long-strings <path_to>/Test.java

Original file com/test/Test.java

package com.test;

import java.util.Map;
import java.util.TreeMap;

public class Test {
  public static Map<String, String> test(Map<String, String> structure) {
    structure
      .entrySet()
      .stream()
      .filter(e -> e.getValue().isEmpty())
      .forEach(e -> {
        if (structure.containsValue(e.getKey())) { // The quick brown fox jumps over the lazy dog, and again the quick brown fox jumps over the lazy dog
          e.setValue(e.getKey());
        }
      });

    return new TreeMap<>(structure);
  }

}

After formatting the first time:

package com.test;

import java.util.Map;
import java.util.TreeMap;

public class Test {
  public static Map<String, String> test(Map<String, String> structure) {
    structure.entrySet().stream()
        .filter(e -> e.getValue().isEmpty())
        .forEach(
            e -> {
              if (structure.containsValue(
                  e
                      .getKey())) { // The quick brown fox jumps over the lazy dog, and again the
                                    // quick brown fox jumps over the lazy dog
                e.setValue(e.getKey());
              }
            });

    return new TreeMap<>(structure);
  }
}

After formatting the second time:

package com.test;

import java.util.Map;
import java.util.TreeMap;

public class Test {
  public static Map<String, String> test(Map<String, String> structure) {
    structure.entrySet().stream()
        .filter(e -> e.getValue().isEmpty())
        .forEach(
            e -> {
              if (structure.containsValue(
                  e.getKey())) { // The quick brown fox jumps over the lazy dog, and again the
                // quick brown fox jumps over the lazy dog
                e.setValue(e.getKey());
              }
            });

    return new TreeMap<>(structure);
  }
}

There are no changes after formatting the file again.

I admit that the comment is placed at an unusual location.
There is no issue if the comment appears in a separate line in front of the if-statement, or in the next line after the opening block {.

Activity

  1. cushon commented on Dec 19, 2024

    @cushon
    Collaborator

    Thanks for the report, I agree the output should be stable. I think this specific issue with line comments is the same as #211

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions