Skip to content

False-positive: Suppress indentation check when quotes start at the left margin #18228

@mohitsatr

Description

@mohitsatr

Follow-up of #17329
From docs: https://google.github.io/styleguide/javaguide.html#s4.2-block-indentation

[opening quotes] may either follow the same indentation rules as other constructs, or it may have no indentation at all (so it starts at the left margin).

Currently, TextBlockGoogleStyleFormatting Check covers:

Code:

/** somejavadoc. */
public class InputTextBlocksIndentation {

  public void textFunc() {

    String zeroIndentation = // False-positive below
"""
content of the block
""";                         // False-positive

    getData( OK, below
    """    
        Indentation of Text-block
    """,  // OK
        5
    );
  }

  public static void getData(String data, int length) {}

Cli:

$ java -jar checkstyle-11.1.0-all.jar -c google_checks.xml InputTextBlocksIndentation.java
Starting audit...
[WARN] InputTextBlocksIndentation.java:6:1: '"""' has incorrect indentation level 0, expected level should be 8. [Indentation]
[WARN] InputTextBlocksIndentation.java:9:1: '"""' has incorrect indentation level 0, expected level should be 8. [Indentation]
[WARN] InputTextBlocksIndentation.java:12:5: 'method call' child has incorrect indentation level 4, expected level should be 6. [Indentation]
[WARN] InputTextBlocksIndentation.java:14:5: 'method call' child has incorrect indentation level 4, expected level should be 6. [Indentation]
Audit done.

The first two violations are not expected, as Google-style allows for indenting opening quotes at the start of the line without any additional indentation. And because closing quotes should be aligned with opening quotes, they are also allowed to appear at the 0th indentation level.

The last two violations are expected because if the indentation level is not zero, then it has to follow the indentation rules like other blocks.

Google-Formatter output:

/** somejavadoc. */
public class InputTextBlocksIndentation {

  public void textFunc() {

    String zeroIndentation =
"""
content of the block
""";

    getData(
        """
            Indentation of Text-block
        """,
        5);
  }

  public static void getData(String data, int length) {}
}

Difference between Original and Formatted code:

$ diff -Naru InputTextBlocksIndentation.java FormattedInputTextBlocksIndentation.java
--- InputTextBlocksIndentation.java	2025-12-03 08:25:55.734581300 +0530
+++ FormattedInputTextBlocksIndentation.java	2025-12-03 08:26:53.807180600 +0530
@@ -6,14 +6,13 @@

     getData(
-    """
-        Indentation of Text-block
-    """,
-        5
-    );
+        """
+            Indentation of Text-block
+        """,
+        5);
   }

   public static void getData(String data, int length) {}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions