Skip to content

SONARPY-1075 Quick fixes for S3923 should not break the code#1177

Merged
nils-werner-sonarsource merged 5 commits into
masterfrom
SONARPY-1075
Sep 5, 2022
Merged

SONARPY-1075 Quick fixes for S3923 should not break the code#1177
nils-werner-sonarsource merged 5 commits into
masterfrom
SONARPY-1075

Conversation

@nils-werner-sonarsource

@nils-werner-sonarsource nils-werner-sonarsource commented Aug 25, 2022

Copy link
Copy Markdown
Contributor

No description provided.

@sonarsource-next

Copy link
Copy Markdown

@nils-werner-sonarsource nils-werner-sonarsource marked this pull request as ready for review August 26, 2022 08:06

@andreaguarino andreaguarino 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.

LGTM, I'm not sure I saw a test unit for the code snippet reported in the ticket?
Anyway I tested it manually on VSCode and the problem is fixed

}

@Test
public void wrapped_conditional_statements(){

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.

nitpick: conditional_expressions instead of conditional_statements

Comment on lines +53 to +56
" if b == 0:\n" +
" doSomething(); doOneMoreThing()\n"+
" else:\n" +
" doSomething(); doOneMoreThing()\n";

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.

IMO, for quickfixes we should write "noncompliant" and "fixed" code inside a python file. I think it's not super easy to read code inside a Java string and it might be error prone.
However, if you also agree, let's create a ticket and do that in a separate PR.

Comment on lines +166 to +167
String codeFixed = "doSomething() # false branch comment2\n" +
" # false branch comment3\n";

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.

can you please add a comment saying we only keep comments of the else branch?

Comment on lines +114 to +127
private static List<Token> tokens(Tree tree) {
List<Token> tokens = new ArrayList<>();
tree.children().forEach(child -> {
if (child.is(Tree.Kind.TOKEN)) {
tokens.add((Token) child);
((Token) child).trivia().stream()
.map(Trivia::token)
.forEach(tokens::add);
} else {
tokens.addAll(tokens(child));
}
});
return tokens;
}

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 think we have already TreeUtils.tokens() that should do this

Comment on lines +165 to +176
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PythonTextEdit that = (PythonTextEdit) o;
return startLine == that.startLine && startLineOffset == that.startLineOffset && endLine == that.endLine
&& endLineOffset == that.endLineOffset && Objects.equals(message, that.message);
}

@Override
public int hashCode() {
return Objects.hash(message, startLine, startLineOffset, endLine, endLineOffset);

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.

Good idea!

assertThat(edit.equals(new PythonTextEdit("",0, 1, 1, 1))).isFalse();
assertThat(edit.equals(new PythonTextEdit("",0, 0, 0, 1))).isFalse();
assertThat(edit.equals(new PythonTextEdit("",0, 0, 1, 0))).isFalse();
assertThat(edit.equals(new PythonTextEdit("a", 0, 0, 1, 1))).isFalse();

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 think we can add a test for hashCode as well

@@ -45,30 +45,31 @@ public void quickfix_one_statement() {
" doSomething()\n";

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 think I didn't see a test for the problematic code showed in the ticket?
More specifically about point 3)

The following statement do_something() gets pushed outside the scope of the function.

// Remove else branch
elseBranch.ifPresent(branch -> quickFixBuilder.addTextEdit(PythonTextEdit.remove(branch)));
// Shift all body statements to the left
// Skip first shift because already done by upper if statement removal

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.

do you mean it's done by the removeUntil?

@sonarsource-next

Copy link
Copy Markdown

@nils-werner-sonarsource nils-werner-sonarsource merged commit 0ec89e3 into master Sep 5, 2022
@nils-werner-sonarsource nils-werner-sonarsource deleted the SONARPY-1075 branch September 5, 2022 06:58
hashicorp-vault-sonar-prod Bot pushed a commit that referenced this pull request Jun 8, 2026
GitOrigin-RevId: 403a993d82aec502754212c85c436537787a0c25
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