From fcf891083e4fc9e3d73617d3d1fe9d3a939a88a8 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 1 Feb 2024 15:43:26 +0100 Subject: [PATCH] Create rule S6888: "case null" should be preferred over "if" pre-condition (#3574) --- rules/S6888/java/metadata.json | 24 +++++++++++++ rules/S6888/java/rule.adoc | 63 ++++++++++++++++++++++++++++++++++ rules/S6888/metadata.json | 2 ++ 3 files changed, 89 insertions(+) create mode 100644 rules/S6888/java/metadata.json create mode 100644 rules/S6888/java/rule.adoc create mode 100644 rules/S6888/metadata.json diff --git a/rules/S6888/java/metadata.json b/rules/S6888/java/metadata.json new file mode 100644 index 0000000000..beb3a3fa58 --- /dev/null +++ b/rules/S6888/java/metadata.json @@ -0,0 +1,24 @@ +{ + "title": "\"case null\" should be preferred over \"if\" pre-condition", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "java21" + ], + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-6888", + "sqKey": "S6888", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S6888/java/rule.adoc b/rules/S6888/java/rule.adoc new file mode 100644 index 0000000000..c876a0b1e4 --- /dev/null +++ b/rules/S6888/java/rule.adoc @@ -0,0 +1,63 @@ +== Why is this an issue? + +Java 21 introduces `case null` for `switch`. It is a more concise and readable way to handle nullability compared to an `if` statement before a `switch`. +[source,java] +---- +switch (s) { + case null: /* code if null */ + // ... +} +---- +When the selector expression evaluates to null and the `case null` is present, switch statements don't throw a NullPointerException anymore. +It is not mandatory anymore to test for `null` before a `switch`, and it is more readable to use the `case null` form. + +This rule identifies `if` statements that could be replaced by a more readable `case null` label. +[source,java] +---- +if (s == null) { + /* code if null */ +} +switch (s) { + // ... +} +---- + +== How to fix it + +Move the null check inside the switch statement. + +=== Code examples + +==== Noncompliant code example + +[source,java,diff-id=1,diff-type=noncompliant] +---- +static void f(String s) { + if (s == null) { // Noncompliant, we can move the null check in the switch statement. + System.out.println("Oops!"); + return; + } + switch (s) { + case "Foo", "Bar" -> System.out.println("Great"); + default -> System.out.println("Ok"); + } +} +---- + +==== Compliant solution + +[source,java,diff-id=1,diff-type=compliant] +---- +static void f(String s) { + switch (s) { + case null -> System.out.println("Oops!"); + case "Foo", "Bar" -> System.out.println("Great"); + default -> System.out.println("Ok"); + } +} +---- + + + +== Resources +* https://openjdk.org/jeps/441[JEP 441: Pattern Matching for Switch] diff --git a/rules/S6888/metadata.json b/rules/S6888/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S6888/metadata.json @@ -0,0 +1,2 @@ +{ +}