-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create rspec for rule "Don't test if the variable is null before a sw…
…itch"
- Loading branch information
1 parent
245726d
commit 3e9354b
Showing
2 changed files
with
29 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,47 @@ | ||
FIXME: add a description | ||
Don't test if the variable is null before a switch | ||
|
||
// If you want to factorize the description uncomment the following line and create the file. | ||
//include::../description.adoc[] | ||
|
||
== Why is this an issue? | ||
|
||
FIXME: remove the unused optional headers (that are commented out) | ||
Since Java 21, can accept null values. Thus, it not necessary anymore to check if a variable is null before matching it and should even be avoided, as it lead to less readable code. | ||
|
||
//=== What is the potential impact? | ||
|
||
== How to fix it | ||
//== How to fix it in FRAMEWORK NAME | ||
|
||
Move the null check inside the switch statement. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,text,diff-id=1,diff-type=noncompliant] | ||
[source,java,diff-id=1,diff-type=noncompliant] | ||
---- | ||
FIXME | ||
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,text,diff-id=1,diff-type=compliant] | ||
[source,java,diff-id=1,diff-type=compliant] | ||
---- | ||
FIXME | ||
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"); | ||
} | ||
} | ||
---- | ||
|
||
//=== How does this work? | ||
|
||
//=== Pitfalls | ||
|
||
//=== Going the extra mile | ||
|
||
|
||
//== Resources | ||
//=== Documentation | ||
//=== Articles & blog posts | ||
//=== Conference presentations | ||
//=== Standards | ||
//=== External coding guidelines | ||
//=== Benchmarks | ||
== Resources | ||
* https://openjdk.org/jeps/441[Pattern Matching for Switch] |