Skip to content

SONARJAVA-5146 Fix S5411 IndexOutOfBoundsException for lambda parameter#5026

Merged
alban-auzeill merged 2 commits intomasterfrom
alban/SONARJAVA-5146
Feb 13, 2025
Merged

SONARJAVA-5146 Fix S5411 IndexOutOfBoundsException for lambda parameter#5026
alban-auzeill merged 2 commits intomasterfrom
alban/SONARJAVA-5146

Conversation

@alban-auzeill
Copy link
Copy Markdown
Member

@alban-auzeill alban-auzeill commented Feb 13, 2025

Comment on lines +68 to +71
public static MethodInvocationTree parentMethodInvocationOfArgumentAtPos(@Nullable Tree argumentCandidate, int expectedArgumentPosition) {
if (argumentCandidate == null) {
return null;
}
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.

You could chose to not accept the null argumentCandidate case to simplify this method. It looks like the caller won't give us a null tree anyway.

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.

In utility classes, it's easy to support and test null. I assume it will ease the test coverage work in the caller code, even if for the current rule we don't need it.

Comment on lines +85 to +88
public static VariableTree lamdaArgumentAt(@Nullable LambdaExpressionTree lambdaExpressionTree, int argumentPosition) {
if (lambdaExpressionTree == null) {
return null;
}
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.

Same comment as for parentMethodInvocationOfArgumentAtPos

ClassTree classTree = (ClassTree) compilationUnitTree.types().get(0);
List<Tree> members = classTree.members();
LiteralTree literal1 = (LiteralTree) ((VariableTree) members.get(0)).initializer();
MethodInvocationTree mathRound = (MethodInvocationTree) ((VariableTree) members.get(1)).initializer();
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.

should it be called mathMax instead for clarity?

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.

Oops, by bad, I changed the method and forgot to rename the variable.

@sonarqube-next
Copy link
Copy Markdown

@alban-auzeill alban-auzeill merged commit c771db2 into master Feb 13, 2025
@alban-auzeill alban-auzeill deleted the alban/SONARJAVA-5146 branch February 13, 2025 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants