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

refactored StringConcatFinder pattern #560

Merged
merged 5 commits into from
Jul 24, 2020
Merged

refactored StringConcatFinder pattern #560

merged 5 commits into from
Jul 24, 2020

Conversation

Vitaly-Protasov
Copy link
Collaborator

Solved in case of #528

aibolit/patterns/string_concat/string_concat.py Outdated Show resolved Hide resolved
aibolit/patterns/string_concat/string_concat.py Outdated Show resolved Hide resolved
elif node.operandr.line:
lines.add(node.operandr.line)

elif hasattr(node.operandr, 'value') and isinstance(node.operandr.value, str) and \
Copy link
Contributor

Choose a reason for hiding this comment

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

It sounds like we check only if one argument of BINARY_OPERATION is string, while comment in class says we are looking for both arguments to be string. If that correct, we have to check both operands to be string.

Copy link
Member

Choose a reason for hiding this comment

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

we search for any string concatenation. but there can be different concatenation:
"String" + "4"
int a = "b";
a+ "b";
b + "a";
"; " + message.toString()


            Integer d = 1 + b;
            System.out.println(a + b);

etc.

look at the tests, there are examples there.

Copy link
Contributor

Choose a reason for hiding this comment

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

So it just look for using concatenation with any string literal?
If so, we better create a single function to check for a string literal and use it to check left and right side of operators.

Copy link
Member

Choose a reason for hiding this comment

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

ok

Copy link
Contributor

Choose a reason for hiding this comment

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

@Vitaly-Protasov please move logic of checking operand to separate function and call it in operandr and operandl here.

Copy link
Member

@lyriccoder lyriccoder left a comment

Choose a reason for hiding this comment

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

ok

elif node.operandr.line:
lines.add(node.operandr.line)

elif hasattr(node.operandr, 'value') and isinstance(node.operandr.value, str) and \
Copy link
Member

Choose a reason for hiding this comment

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

we search for any string concatenation. but there can be different concatenation:
"String" + "4"
int a = "b";
a+ "b";
b + "a";
"; " + message.toString()


            Integer d = 1 + b;
            System.out.println(a + b);

etc.

look at the tests, there are examples there.

@@ -33,18 +33,20 @@ def value(self, filename: str) -> List[int]:
ast = AST.build_from_javalang(build_ast(filename))
for node in ast.get_proxy_nodes(ASTNodeType.BINARY_OPERATION):
if node.operator == '+':
# by this check we want to filter nodes and consider only
# [Literal, MemberReference, MethodInvocation, This]
Copy link
Contributor

Choose a reason for hiding this comment

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

How did you get this list?
When I check node types for value field, I get following:

  • ASSERT_STATEMENT
  • ASSIGNMENT
  • ELEMENT_VALUE_PAIR
  • LITERAL
  • TRY_RESOURCE

By the way, can you explain, how other than LITERAL nodes meet this pattern?

Comment on lines 40 to 42
if node.line:
lines.add(node.line)
elif node.line:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please pay attention to the code you write.

Copy link
Contributor

Choose a reason for hiding this comment

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

We expect .line to return always int. So simply add it to the set.

Comment on lines 47 to 50
if node.line:
lines.add(node.line)
elif node.line:
lines.add(node.line)
Copy link
Contributor

Choose a reason for hiding this comment

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

Also here

Comment on lines 40 to 42
if node.line:
lines.add(node.line)
elif node.line:
Copy link
Contributor

Choose a reason for hiding this comment

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

We expect .line to return always int. So simply add it to the set.

elif node.operandr.line:
lines.add(node.operandr.line)

elif hasattr(node.operandr, 'value') and isinstance(node.operandr.value, str) and \
Copy link
Contributor

Choose a reason for hiding this comment

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

@Vitaly-Protasov please move logic of checking operand to separate function and call it in operandr and operandl here.

@acheshkov
Copy link
Member

@rultor merge

@rultor
Copy link
Collaborator

rultor commented Jul 24, 2020

@rultor merge

@acheshkov OK, I'll try to merge now. You can check the progress of the merge here

@rultor rultor merged commit a0efec4 into master Jul 24, 2020
@rultor
Copy link
Collaborator

rultor commented Jul 24, 2020

@rultor merge

@acheshkov Done! FYI, the full log is here (took me 18min)

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.

5 participants