Skip to content
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

fix: fix qodana warning with proper call to Array.toString() #4536

Merged

Conversation

MartinWitt
Copy link
Collaborator

Change Log

The following bad smells are refactored:

ArraysToString

array.toString() is not the best way to print an array. Use Arrays.toString(array) instead.

The following has changed in the code:

ArraysToString

  • Replaced str.toString() with Arrays.toString(str).

 The following has changed in the code:
Replaced str.toString() with Arrays.toString(str).
@algomaster99
Copy link
Contributor

algomaster99 commented Jan 18, 2022

@MartinWitt where can I see the list of all Qodana warnings? We have a tool that can automatically repair such types of warnings - Sorald. You can see the list of rules it handles as of now here.

EDIT: I guess you have already contributed to it ASSERT-KTH/sorald#368 :)

@MartinWitt
Copy link
Collaborator Author

MartinWitt commented Jan 18, 2022

Do you want a list of all we have or all qodana has?
For all Qodana(IntelliJ) supports, see. https://www.jetbrains.com/help/idea/list-of-java-inspections.html

@algomaster99
Copy link
Contributor

@MartinWitt Both would be good. I will see if we can fix any violations using Sorald.

@MartinWitt
Copy link
Collaborator Author

For all we have, see here https://github.com/INRIA/spoon/pull/4481/files (we disabled this view in a recent PR) or run sudo docker run --rm -it -p 8080:8080 -v $PWD/:/data/project/ jetbrains/qodana-jvm-community:2021.3 --show-report
For all rules, see here https://www.jetbrains.com/help/idea/list-of-java-inspections.html
Maybe you can open the project in IntelliJ and run qodana/the inspections there.

Copy link
Contributor

@algomaster99 algomaster99 left a comment

Choose a reason for hiding this comment

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

Delete the new line and then we can merge.

@@ -7,6 +7,7 @@
*/
package spoon.support.sniper.internal;


Copy link
Contributor

Choose a reason for hiding this comment

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

You accidentally added this new line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I still have problems of added newlines while sniper printing. I removed it.

@@ -720,7 +721,7 @@ private void onCharSequence(CharType type, StringBuilder buff, Consumer<SourceFr
}
// nothing has matched, best effort token
if (longestMatcher == null) {
consumer.accept(new TokenSourceFragment(str.toString(), TokenType.CODE_SNIPPET));
consumer.accept(new TokenSourceFragment(Arrays.toString(str), TokenType.CODE_SNIPPET));
Copy link
Collaborator

Choose a reason for hiding this comment

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

very confusing to have an array named str, charArray is better?

Copy link
Collaborator Author

@MartinWitt MartinWitt Jan 19, 2022

Choose a reason for hiding this comment

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

Yes, I renamed it

@MartinWitt MartinWitt force-pushed the refactor/qodana/ElementSourceFragment branch from 214c44c to aa4f970 Compare January 19, 2022 13:17
longestMatcher = strMatcher.getLonger(longestMatcher);
}
}
// nothing has matched, best effort token
if (longestMatcher == null) {
consumer.accept(new TokenSourceFragment(str.toString(), TokenType.CODE_SNIPPET));
consumer.accept(new TokenSourceFragment(Arrays.toString(charArray), TokenType.CODE_SNIPPET));
Copy link
Collaborator

Choose a reason for hiding this comment

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

this looks like this best-effort token never worked. I guess we don't have any single test here :)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looking at the coverage, there should exist a testcase for this line https://coveralls.io/builds/45751341/source?filename=src%2Fmain%2Fjava%2Fspoon%2Fsupport%2Fsniper%2Finternal%2FElementSourceFragment.java#L724. But I have not found a way in the UI to find it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking at the coverage, there should exist a testcase for this line coveralls.io/builds/45751341/source?filename=src%2Fmain%2Fjava%2Fspoon%2Fsupport%2Fsniper%2Finternal%2FElementSourceFragment.java#L724. But I have not found a way in the UI to find it.

argh, this is #4541

Copy link
Collaborator

Choose a reason for hiding this comment

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

anyway, I'd be in favor in writing an assertion on the content of this token. Otherwise, we fix broken code by something which may be buggy as well

would you be able to add such an assertion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

After some debugging, I found the testcase

public void testPrintLocalVariableDeclaration2() {
using this line with a string 0,

Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe the current input token is simply wrong.

Agree, that's why we need a new test or a new assertion to assert the actual correct content. Would you write this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can only trigger this line with joint declarations but currently the sniper is not able to work with them, see #3386. It looks like either this token was never relevant or is needed in a future version and we can't great asserts for the value currently.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we are unable to specify this line, then the best option is to simply remove it. Could you give it a try?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It looks like the token is for some strange reasons important for the sniper printing and removing it adds unwanted whitespaces. So we more or less have to keep it and fix this after fixing the linked problem.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ack, then let's revert this last commit and proceed with the change, it's not harmful. we replace code we don't understand by code we still don't understand but less confusing.

@monperrus monperrus changed the title refactor: fix qodana warning array.toString() fix: fix qodana warning with proper call to Array.toString() Feb 5, 2022
@monperrus monperrus merged commit f71e9e6 into INRIA:master Feb 5, 2022
@monperrus
Copy link
Collaborator

Thanks @MartinWitt

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.

3 participants