checkstyle / checkstyle/checkstyle

"basicOffset" and "lineWrappingIndentation" conflict each other

Open
#6,633 1 comment 0 reactions 0 assignees View on GitHub
indentation
Dominant language
Java
Stars
9.6k
Forks
4.2k
Avg merge
22h 23m
Merged PRs (30d)
232

Description

```
/var/tmp $ cat build.gradle
plugins {
id 'checkstyle'
}

repositories {
mavenCentral()
}

apply plugin: 'java'

/var/tmp $ cat src/main/java/hello/HelloWorld.java
package hello;

import java.util.ArrayList;
import java.util.function.Function;
import java.util.Map;
import java.util.Optional;
import javafx.util.Pair;

public class HelloWorld {
public static void main(String[] args) {
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());

ArrayList, Optional>> pairs = new ArrayList, Optional>>();
pairs.add(new Pair(new Pair("this", "that"), Optional.of(new Integer(1))));

Function, Optional>>, Integer> fn = (ps) -> {
int c = 0;
for (Pair,
Optional> p : ps ) {**
System.out.println(p.getKey());
c ++;
}
return c;
};

fn.apply(pairs);
}
}

/var/tmp $ cat src/main/java/hello/Greeter.java
package hello;

public class Greeter {
public String sayHello() {
return "Hello world!";
}
}

/var/tmp $ cat config/checkstyle/checkstyle.xml










/var/tmp $ gradle checkstyleMain

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 up-to-date
/var/tmp$ gradle checkstyleMain

> Task :compileJava
Note: /var/tmp/src/main/java/hello/HelloWorld.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :checkstyleMain
[ant:checkstyle] [WARN] /var/tmp/src/main/java/hello/HelloWorld.java:20: 'member def type' has incorrect indentation level 10, expected level should be 8. [Indentation]
Checkstyle rule violations were found. See the report at: file:///var/tmp/build/reports/checkstyle/main.html
Checkstyle files with violations: 1
Checkstyle violations by severity: [warning:1]

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

/var/tmp $ cat src/main/java/hello/HelloWorld.java
package hello;

import java.util.ArrayList;
import java.util.function.Function;
import java.util.Map;
import java.util.Optional;
import javafx.util.Pair;

public class HelloWorld {
public static void main(String[] args) {
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());

ArrayList, Optional>> pairs = new ArrayList, Optional>>();
pairs.add(new Pair(new Pair("this", "that"), Optional.of(new Integer(1))));

Function, Optional>>, Integer> fn = (ps) -> {
int c = 0;
for (Pair,
Optional> p : ps ) {
System.out.println(p.getKey());
c ++;
}
return c;
};

fn.apply(pairs);
}
}

/var/tmp $ gradle checkstyleMain

> Task :compileJava
Note: /var/tmp/src/main/java/hello/HelloWorld.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :checkstyleMain
[ant:checkstyle] [WARN] /var/tmp/src/main/java/hello/HelloWorld.java:20: 'Optional' has incorrect indentation level 8, expected level should be 10. [Indentation]
Checkstyle rule violations were found. See the report at: file:///var/tmp/build/reports/checkstyle/main.html
Checkstyle files with violations: 1
Checkstyle violations by severity: [warning:1]

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

```

---------------

The initialization statement inside the 'for' statement causes a conflicting indentation error when "basicOffset" and "lineWrappingIndentation" are not same.
When the initialization statement is complicated, a random line break will cause conflicting error, which does not allow any correct indentation except having a line break after type declaration.
```
for (Pair,
Optional> p : ps ) {
```

If the code looks like below, checkstyle allows one indentation.
```
for (Pair, Optional>
p : ps ) {
```

--------------

Contributor guide

Open the contributing guide

Research direction

Reproduce the warning with the Gradle checkstyleMain task and the configuration in config/checkstyle/checkstyle.xml, focusing on the Indentation module. Compare the two wrapped for-statement layouts in src/main/java/hello/HelloWorld.java and trace how basicOffset and lineWrappingIndentation determine the expected indentation. Done means the conflicting error is resolved without requiring a particular line break after the type declaration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.