-
Notifications
You must be signed in to change notification settings - Fork 31
Create rule S7473: Use unnamed variable in pattern matching #4919
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
base: master
Are you sure you want to change the base?
Conversation
3fe67de
to
b2276a1
Compare
rules/S7473/java/metadata.json
Outdated
"constantCost": "1min" | ||
}, | ||
"tags": [ | ||
"unused" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add the java23
tag
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean java22
?
rules/S7473/java/metadata.json
Outdated
"sqKey": "S7473", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can change the value for "quickfix" to targeted
as we should be able to implement a quickfix for this one
rules/S7473/java/rule.adoc
Outdated
@@ -0,0 +1,55 @@ | |||
A variable occurring in pattern matching is unused. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the value of this line
A variable occurring in pattern matching is unused. |
rules/S7473/java/rule.adoc
Outdated
|
||
== Why is this an issue? | ||
|
||
This may seem harmless, but could lead to confusion and potential errors in your code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can be a little bit more verbose and specific here, what about:
This may seem harmless, but could lead to confusion and potential errors in your code. | |
Unused variables in pattern matching can cause confusion and make the code harder to understand. They may also lead to potential errors. | |
For example, if a variable is declared but not used, it may be mistakenly assumed that it has some significance in the code. This can lead to misunderstandings and make the code less readable. |
rules/S7473/java/rule.adoc
Outdated
|
||
== How to fix it | ||
|
||
By replacing unused variables by an unnamed variable `_`, the function's behavior will be the same, but the programmer's intention will be clear. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By replacing unused variables by an unnamed variable `_`, the function's behavior will be the same, but the programmer's intention will be clear. | |
Replace unused variables with the unnamed variable `_`. This will not change the behavior of the function, but will make the code clearer. |
rules/S7473/java/rule.adoc
Outdated
* https://openjdk.org/jeps/361[JEP 361: Switch Expressions] | ||
* https://openjdk.org/jeps/394[JEP 394: Pattern Matching for instanceof] | ||
* https://openjdk.org/jeps/440[JEP 440: Record Patterns] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if it's valuable here to mention the documentation of switch expressions, pattern matching and records as the rule is not specific to those topics
…laced with unnamed variables
b2276a1
to
3a73d13
Compare
|
|
You can preview this rule here (updated a few minutes after each push).
This is a proposal for a new rule starting with Java 22 thanks to the possibility to use unnamed variables.
Rule idea: https://sonarsource.atlassian.net/browse/SONARJAVA-5433
Review
A dedicated reviewer checked the rule description successfully for: