Skip to content

MinimumSwitchCases should keep comments #6

Closed

Description

@koppor

Current:

-                switch (keyBinding.get()) {
-                    case DELETE_ENTRY:
-                        new DeleteFileAction(dialogService, preferencesService, databaseContext,
-                                viewModel, listView).execute();
-                        event.consume();
-                        break;
-                    default:
-                        // Pass other keys to children
+                if (keyBinding.get() == KeyBinding.DELETE_ENTRY) {
+                    new DeleteFileAction(dialogService, preferencesService, databaseContext,
+                            viewModel, listView).execute();
+                    event.consume();
+                } else {

Expected:

               if (keyBinding.get() == KeyBinding.DELETE_ENTRY) {
                   new DeleteFileAction(dialogService, preferencesService, databaseContext,
                           viewModel, listView).execute();
                   event.consume();
               } else {
                   // Pass other keys to children

Activity

  1. added
    bug
    on May 1, 2023
  2. moved this to Backlog in OpenRewriteon May 1, 2023
  3. knutwannheden commented on May 6, 2023

    @knutwannheden
    Contributor

    @koppor The recipe could certainly try to preserve more of the comments. AFAICT any comments before or after the break would currently get lost. In your case it appears that you have a default case with only a comment in it.

    I just checked what IntelliJ IDEA does in this case: It generates an if statement without else and drops the comment. I think we should either do that or generate an else with only the comment in it. Personally I would opt for IntelliJ's behavior, as I think an otherwise empty else block looks rather strange (IntelliJ, for example, would annotate it with a warning marker). As a developer you could still decide if you think you need to add that comment before or after the if before integrating the change. What do you think?

  4. koppor commented on May 6, 2023

    @koppor
    ContributorAuthor

    I personally like to have comments very close to the logical position. If an else branch is matched and nothing is done here for a reason, I sometimes put comments there. Pass other keys to children is a perfect example there.

    However, I am in the minority here - and it is OK for me to have these comments somewhere else.

    When the comments are deleted, there is manual investigation needed to restore the comments based on the git diff. Therefore, this is the least good option for me.

  5. added this to the milestone on May 30, 2023
  6. removed this from the milestone on Nov 30, 2023
  7. moved this from Backlog to Done in OpenRewriteon Apr 9, 2026
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

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions