From 4cd808e3b0bc07ede2b1b988523fa9813d75df25 Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Wed, 1 Mar 2023 15:33:26 +0400 Subject: [PATCH] [java-inspections] DuplicateBranchesInSwitch: fix a comment IJ-CR-101312 GitOrigin-RevId: 11e80499c55677cea9ba2c2c3d1dc0dd0f83124d --- .../DuplicateBranchesInSwitchInspection.java | 10 +++++----- .../afterDeleteRedundantBranch7.java | 16 ++++++++++++++++ .../beforeDeleteRedundantBranch7.java | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 java/java-tests/testData/inspection/duplicateBranchesInSwitchFix/afterDeleteRedundantBranch7.java create mode 100644 java/java-tests/testData/inspection/duplicateBranchesInSwitchFix/beforeDeleteRedundantBranch7.java diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java index 2f51daf2cd49..b5af5ecc8cb8 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java @@ -385,19 +385,19 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo case R(): case null: case S(): - System.out.println(42); // Branch in 'switch' is a duplicate of the default branch - break; + return 42; // Branch in 'switch' is a duplicate of the default branch case String s: - System.out.println(0); - break; + return 0; default: - System.out.println(42); + return 42; } The 'case R():' and 'case S():' statements can be removed as redundant, because the corresponding branch is a duplicate of the default branch. But the 'default' case does not handle null values, so we cannot delete the 'case null:' and the 'return 42;' statement. + + See com.intellij.java.codeInspection.DuplicateBranchesInSwitchFixTest [DeleteRedundantBranch7.java] */ context.deleteStatements(); } diff --git a/java/java-tests/testData/inspection/duplicateBranchesInSwitchFix/afterDeleteRedundantBranch7.java b/java/java-tests/testData/inspection/duplicateBranchesInSwitchFix/afterDeleteRedundantBranch7.java new file mode 100644 index 000000000000..f375be6e7411 --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateBranchesInSwitchFix/afterDeleteRedundantBranch7.java @@ -0,0 +1,16 @@ +// "Delete redundant 'switch' branch" "GENERIC_ERROR_OR_WARNING" +class Test { + record R() {} + record S() {} + + void foo(Object obj) { + switch (obj) { + case null: + return 42; + case String s: + return 0; + default: + return 42; + } + } +} diff --git a/java/java-tests/testData/inspection/duplicateBranchesInSwitchFix/beforeDeleteRedundantBranch7.java b/java/java-tests/testData/inspection/duplicateBranchesInSwitchFix/beforeDeleteRedundantBranch7.java new file mode 100644 index 000000000000..e7f87ec3ff86 --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateBranchesInSwitchFix/beforeDeleteRedundantBranch7.java @@ -0,0 +1,18 @@ +// "Delete redundant 'switch' branch" "GENERIC_ERROR_OR_WARNING" +class Test { + record R() {} + record S() {} + + void foo(Object obj) { + switch (obj) { + case R(): + case null: + case S(): + return 42; + case String s: + return 0; + default: + return 42; + } + } +}