Skip to content

Issue #15058: New Check UnusedCatchParameterShouldBeUnnamed#15358

Merged
rnveach merged 1 commit into
checkstyle:masterfrom
checkstyle-GSoC25:new-check-unusedcatch
Aug 6, 2024
Merged

Issue #15058: New Check UnusedCatchParameterShouldBeUnnamed#15358
rnveach merged 1 commit into
checkstyle:masterfrom
checkstyle-GSoC25:new-check-unusedcatch

Conversation

@mahfouz72

@mahfouz72 mahfouz72 commented Jul 24, 2024

Copy link
Copy Markdown
Member

@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate site

@romani romani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Item:

Comment thread config/checkstyle-checks.xml

@nrmancuso nrmancuso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Item to discuss before anything else:

@nrmancuso nrmancuso self-assigned this Jul 25, 2024

@nrmancuso nrmancuso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

First pass:

*/
private void checkIdentifier(DetailAST identAst) {
if (catchParameter != null
&& !isLeftHandSideValue(identAst)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So we don't count reassignment as usage? Judging by the test cases below, we currently do. What am I missing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes we don't count reassignment as usage. here is its test case :

Judging by the test cases below, we currently do.

can you point to this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note I don't consider the test case below to be a reassignment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

#15358 (comment) is weird to me.If we were to make e unnamed, we would have to remove this reassignment (even though it does nothing). I suppose I am good with this, let's get this check into the hands of users and go from there.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Leaving this one unresolved so others can see.

@mahfouz72 mahfouz72 force-pushed the new-check-unusedcatch branch 4 times, most recently from e7960cb to c566762 Compare July 26, 2024 01:18

@nrmancuso nrmancuso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Last from me:

@mahfouz72 mahfouz72 force-pushed the new-check-unusedcatch branch 5 times, most recently from a1da5b6 to f4c2b67 Compare July 27, 2024 23:21
@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate report

@github-actions

Copy link
Copy Markdown
Contributor

@nrmancuso nrmancuso force-pushed the new-check-unusedcatch branch from f4c2b67 to 052fb36 Compare July 28, 2024 01:14

@nrmancuso nrmancuso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have spot checked the report, there is far too many violations to check all of them. This is a good first implementation that we can build on, let's get this check into the hands of users.

Great work!

@nrmancuso nrmancuso assigned romani and unassigned nrmancuso Jul 28, 2024
@romani romani assigned rnveach and unassigned romani Jul 28, 2024
@romani romani requested a review from rnveach July 28, 2024 02:06
@mahfouz72

Copy link
Copy Markdown
Member Author

github, generate report

@github-actions

github-actions Bot commented Aug 2, 2024

Copy link
Copy Markdown
Contributor

@mahfouz72

Copy link
Copy Markdown
Member Author

@rnveach @nrmancuso do we agree to suppress this? adding a non-nullity condition will be a surviving mutation

  <checkerFrameworkError unstable="false">
    <fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.java</fileName>
    <specifier>dereference.of.nullable</specifier>
    <message>dereference of possibly-null reference catchParameter</message>
    <lineContent>if (!&quot;_&quot;.equals(catchParameter.getName()) &amp;&amp; !catchParameter.isUsed()) {</lineContent>
  </checkerFrameworkError>

@mahfouz72 mahfouz72 force-pushed the new-check-unusedcatch branch 3 times, most recently from e146622 to bd7624c Compare August 2, 2024 23:18
@nrmancuso

Copy link
Copy Markdown
Contributor

@rnveach @nrmancuso do we agree to suppress this? adding a non-nullity condition will be a surviving mutation

  <checkerFrameworkError unstable="false">
    <fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnusedCatchParameterShouldBeUnnamedCheck.java</fileName>
    <specifier>dereference.of.nullable</specifier>
    <message>dereference of possibly-null reference catchParameter</message>
    <lineContent>if (!&quot;_&quot;.equals(catchParameter.getName()) &amp;&amp; !catchParameter.isUsed()) {</lineContent>
  </checkerFrameworkError>

We can probably make all static analysis happy and also be a bit more expressive:

            final Optional<CatchParameterDetails> unusedCatchParameter = Optional.ofNullable(catchParameters.peek())
                    .filter(p -> !"_".equals(p.getName()))
                    .filter(p -> !p.isUsed());

            unusedCatchParameter.ifPresent(catchParameterDetails ->
                    log(catchParameterDetails.getParameterDefinition(),
                            MSG_UNUSED_CATCH_PARAMETER,
                            catchParameterDetails.getName()));

@mahfouz72 mahfouz72 force-pushed the new-check-unusedcatch branch from bd7624c to f86a9b0 Compare August 4, 2024 01:50
@mahfouz72 mahfouz72 force-pushed the new-check-unusedcatch branch 2 times, most recently from 0d4cf07 to 4867842 Compare August 4, 2024 02:16
@mahfouz72

Copy link
Copy Markdown
Member Author

@rnveach ping

@mahfouz72 mahfouz72 force-pushed the new-check-unusedcatch branch 3 times, most recently from c70de7d to 66e0f2a Compare August 6, 2024 00:44
@rnveach rnveach force-pushed the new-check-unusedcatch branch from 66e0f2a to 771eae7 Compare August 6, 2024 02:14
@rnveach

rnveach commented Aug 6, 2024

Copy link
Copy Markdown
Contributor

Rebasing to ensure no issues on merge.

@rnveach

rnveach commented Aug 6, 2024

Copy link
Copy Markdown
Contributor

checkstyle/contribution#883 (comment) is needed for a merge

@mahfouz72

Copy link
Copy Markdown
Member Author

@rnveach done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: New Check UnusedCatchParameterShouldBeUnnamed

4 participants