Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Apr 8, 2025

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:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

@github-actions github-actions bot added the java label Apr 8, 2025
@romainbrenguier romainbrenguier changed the title Create rule S7473 Create rule S7473: use unnamed variable in pattern matching Apr 8, 2025
@romainbrenguier romainbrenguier changed the title Create rule S7473: use unnamed variable in pattern matching Create rule S7473: Use unnamed variable in pattern matching Apr 8, 2025
@romainbrenguier romainbrenguier marked this pull request as ready for review April 8, 2025 12:59
"constantCost": "1min"
},
"tags": [
"unused"

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean java22?

"sqKey": "S7473",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",

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

@@ -0,0 +1,55 @@
A variable occurring in pattern matching is unused.

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

Suggested change
A variable occurring in pattern matching is unused.


== Why is this an issue?

This may seem harmless, but could lead to confusion and potential errors in your code.

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:

Suggested change
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.


== 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines 53 to 55
* 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]

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

Copy link

sonarqube-next bot commented Apr 8, 2025

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link

sonarqube-next bot commented Apr 8, 2025

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants