Skip to content

Issue #15098: Indentation of switch block when no braces is now validated #16721

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

Merged

Conversation

Aziz-755
Copy link
Contributor

@Aziz-755 Aziz-755 commented Apr 1, 2025

@Aziz-755
Copy link
Contributor Author

Aziz-755 commented Apr 1, 2025

GitHub, generate report

Copy link
Contributor

github-actions bot commented Apr 1, 2025

@Aziz-755
Copy link
Contributor Author

Aziz-755 commented Apr 1, 2025

@romani I did a refork, and now report generation is working perfectly.
Please take a look.

@romani
Copy link
Member

romani commented Apr 8, 2025

lineWrappingIndentation=4
not clear why this is violation
https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/674efc9_2025201338/reports/diff/camunda/index.html#A1

file looks like using google style, so indentation is valid, am I miss somthing ?

@romani
Copy link
Member

romani commented Apr 8, 2025

@pnagy-cldr , @mohitsatr , is this potential fix for lambda in case ?

@Aziz-755
Copy link
Contributor Author

Aziz-755 commented Apr 8, 2025

lineWrappingIndentation=4 not clear why this is violation https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/674efc9_2025201338/reports/diff/camunda/index.html#A1

file looks like using google style, so indentation is valid, am I miss somthing ?

I based the logic in the fix on using basicOffset (which is 2 in this case) to compute the indentation for the lambda's child.
I wasn't 100% sure whether to rely on basicOffset or lineWrappingIndentation since it's not clearly a line continuation, it might actually be treated more like a new "block."

To align with how Checkstyle handles blocks inside switch lambdas with curlies, I tested the same code but added braces {} to the lambda:

/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
package io.camunda.authentication.config;

import io.camunda.security.configuration.SecurityConfiguration;
import io.camunda.security.entity.AuthenticationMethod;

public final class AuthenticationProperties {
  public static final String METHOD = "camunda.security.authentication.method";
  public static final String API_UNPROTECTED = "camunda.security.authentication.unprotected-api";

  private AuthenticationProperties() {}

  public static String getUnprotectedApiEnvVar() {
    return API_UNPROTECTED.replace(".", "_").replace("-", "").toUpperCase();
  }

  public static void applyToSecurityConfig(
      final SecurityConfiguration securityConfig, final String key, final Object value) {
    switch (key) {
      case METHOD -> {
          AuthenticationMethod.parse(String.valueOf(value))
              .ifPresent(securityConfig.getAuthentication()::setMethod);
      }
      case API_UNPROTECTED -> {
          securityConfig
              .getAuthentication()
              .setUnprotectedApi(Boolean.parseBoolean(String.valueOf(value)));
      }
      default -> {}
    }
  }
}
$ java -jar checkstyle-10.21.4-all.jar -c config.xml test.java
Starting audit...
[WARN] D:\Working on Checkstyle\test.java:27:11: 'block' child has incorrect indentation level 10, expected level should be 8. [custom]
[WARN] D:\Working on Checkstyle\test.java:31:11: 'block' child has incorrect indentation level 10, expected level should be 8. [custom]
Audit done.

So I followed that same logic in the fix. WDYT?

System.out.println("Test");
System.out.println("Another statement");
}
default -> System.out.println("test");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aziz-755 just curious, If we specify different values for basicOffset and lineWrappingIndentation, it would cause the indentation of cases children to be misaligned across the switch block, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohitsatr So if the case blocks were of this format:

case 1 -> {
  // some code
}

or of this format:

case 1 -> 
 // some code

They will be aligned ( here they are using the basicOffset value )

However if the case was like this:

case 1
    ->  // lineWrapping used here
      int x; // basicOffset used here

For this case it will be misaligned in regard of the other cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case 1 -> 
    // should be lineWrapping, not basicOffset

according to docs :

Basic offset indentation is used for indentation inside code blocks( {} ). For any lines that span more than 1, line wrapping indentation is used for those lines after the first

since there is no code block in this case, we must not use basicOffset property. look at similar cases

  void function1(Runnable x) { //indent:2 exp:2
    Runnable r1 = () -> { //indent:4 exp:4
      x.run(); //indent:6 exp:6
    }; //indent:4 exp:4

    Runnable r3 = () -> //indent:4 exp:4
        x.run(); //indent:8 exp:8

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the information. I have adjusted the implementation and used lineWrappingIndentation instead

@Aziz-755 Aziz-755 force-pushed the indentation-for-no-braces-switch-block branch 2 times, most recently from b73eff4 to b5dfd2b Compare April 14, 2025 20:29
@Aziz-755
Copy link
Contributor Author

GitHub, generate report

Copy link
Contributor

@Aziz-755
Copy link
Contributor Author

After some discussion and code refinement, I believe the current implementation is in a good state.

Upon reviewing the report, the remaining violations appear to be false positives caused by the existing switch statement usage
@romani, could you please have a look when you have a chance? Thanks!

@Aziz-755 Aziz-755 requested a review from mohitsatr April 16, 2025 14:17
@mohitsatr
Copy link
Member

mohitsatr commented Apr 17, 2025

@Aziz-755 can we end them in this PR. false-negatives are better than fals-positives.
try to find the reason why check thinks indentation value is wrong, where it is getting the wrong (wrong for us, correct for the Check) indentation value. who's the parent CaseHandler or Lambda Handler ?

it's okay if you feel stuck. it's not you, The check has design flaws.

@romani
Copy link
Member

romani commented Apr 19, 2025

what is strange in this diff report that that there is no removal of violations and bunch is added.

users should see reduction of bad violations, but we looks like add more.
Please recheck of we can avoid new violations.

@Aziz-755
Copy link
Contributor Author

@romani Actually the issue #15098 is about a false negative, so the fix here should in fact add a new violations.

The newly introduced issues in the diff report are also due to an already existing false positives about the usage of switch in lambda. I will take the opportunity to investigate those problems in this PR and see if I can solve them

@romani
Copy link
Member

romani commented Apr 23, 2025

yes, you are right.

new violations are expected, as you add validation for switches.
BUT we have extra challenge, as user dd not have violation at all, they did auto formatting by some tools or manually, and now checkstyle starting doing validation and produce false positives, so it will force users to disable Indentation check to avoid conflicts, so if we will not do extra step we will do worse for Check and for users, no beneficiaries.

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/b5dfd2b_2025203504/reports/diff/camunda/index.html#A1

295     return switch (resource) {
296       case brokers ->
297           switch (subResource) {
298             // POST /cluster/brokers/1/partitions/2
299             case partitions ->
300                 ClusterApiUtils.mapOperationResponse(
301                     requestSender
302                         .joinPartition(
303                             new JoinPartitionRequest(
304                                 MemberId.from(String.valueOf(resourceId)),
305                                 subResourceId,
306                                 priority,
307                                 dryRun))
308                         .join());
309             case brokers, changes -> new ResponseEntity<>(HttpStatusCode.valueOf(404));
310           };

it sounds like this project use +2 for indentaiton,
you used lineWrappingIndentation 4. Can we make it 2 inconfig and run diff report.
Copy some snippets from this project to Input and try to make confirg that matching their formatting.

@Aziz-755 Aziz-755 force-pushed the indentation-for-no-braces-switch-block branch from b5dfd2b to 2776652 Compare April 25, 2025 00:06
@Aziz-755
Copy link
Contributor Author

GitHub, generate report

Copy link
Contributor

@Aziz-755 Aziz-755 force-pushed the indentation-for-no-braces-switch-block branch from 2776652 to c854458 Compare April 25, 2025 22:15
@Aziz-755
Copy link
Contributor Author

GitHub, generate report

Copy link
Contributor

@Aziz-755 Aziz-755 force-pushed the indentation-for-no-braces-switch-block branch from c854458 to 160e295 Compare April 26, 2025 16:31
@Aziz-755
Copy link
Contributor Author

GitHub, generate report

Copy link
Contributor

@Aziz-755 Aziz-755 force-pushed the indentation-for-no-braces-switch-block branch from 160e295 to 38b6417 Compare April 26, 2025 18:07
@Aziz-755
Copy link
Contributor Author

Github, generate report for Indentation/all-examples-in-one

Copy link
Contributor

@Aziz-755
Copy link
Contributor Author

GitHub, generate report

Copy link
Contributor

@Aziz-755
Copy link
Contributor Author

Aziz-755 commented Apr 26, 2025

@romani @mohitsatr I addressed the false positives related to switch expressions when they are used with line wrapping. For example, see the nested switch starting at line 297: ( I think the false positives I fixed are the same as in this issue: #16762 )

295     return switch (resource) {
296       case brokers ->
297           switch (subResource) {
298             // POST /cluster/brokers/1/partitions/2
299             case partitions ->
300                 ClusterApiUtils.mapOperationResponse(
301                     requestSender
302                         .joinPartition(
303                             new JoinPartitionRequest(
304                                 MemberId.from(String.valueOf(resourceId)),
305                                 subResourceId,
306                                 priority,
307                                 dryRun))
308                         .join());
309             case brokers, changes -> new ResponseEntity<>(HttpStatusCode.valueOf(404));
310           };

Initially, this false positive was also causing the new logic (fixing the missing violations for case/default elements without curly braces) to incorrectly report violations.
After resolving the false positive, the new changes introduced in this PR now correctly detect violations without producing incorrect violations.
Here is the updated report: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/38b6417_2025192818/reports/diff/index.html

Copy link
Member

@mohitsatr mohitsatr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job. we actually removed all false-positives.
one last thing, we have only checked the regression on camunda project. Hbase also uses google-style-format, we need to check regression on that too unless there is a reason not to.
@romani

@romani
Copy link
Member

romani commented May 1, 2025

@Aziz-755 , please fix CI violations.

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job !!!

last minors:

@romani
Copy link
Member

romani commented May 1, 2025

GitHub, generate report

Copy link
Contributor

github-actions bot commented May 1, 2025

@Aziz-755 Aziz-755 force-pushed the indentation-for-no-braces-switch-block branch 2 times, most recently from 80d3eb3 to d1c81cb Compare May 1, 2025 20:58
@Aziz-755
Copy link
Contributor Author

Aziz-755 commented May 1, 2025

The CI check is complaining that I am misspelling "prefs" however, I am not using any word like that in my code.
@romani, do you know if I can do something about the CI failure?

@romani
Copy link
Member

romani commented May 2, 2025

Github, generate report for Indentation/all-examples-in-one

Copy link
Contributor

github-actions bot commented May 2, 2025

@romani romani force-pushed the indentation-for-no-braces-switch-block branch from d1c81cb to bce2ceb Compare May 2, 2025 14:43
@romani
Copy link
Member

romani commented May 3, 2025

@Aziz-755 Aziz-755 force-pushed the indentation-for-no-braces-switch-block branch from bce2ceb to d3b1012 Compare May 5, 2025 21:49
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge if CI pass.

Thanks. Lot!

@romani romani merged commit 8408f57 into checkstyle:master May 5, 2025
117 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indentation of the Block child of switch rule is not validated when no braces
3 participants