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

Added literal-or-new-object-identity codemod #186

Merged
merged 3 commits into from
Dec 21, 2023
Merged

Conversation

andrecsilva
Copy link
Contributor

Overview

Added a new codemod that replaces is and is not comparisons with literals and new objects with ==

Copy link

codecov bot commented Dec 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (47e9179) 96.25% compared to head (b76ee5e) 96.28%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #186      +/-   ##
==========================================
+ Coverage   96.25%   96.28%   +0.03%     
==========================================
  Files          81       82       +1     
  Lines        3795     3826      +31     
==========================================
+ Hits         3653     3684      +31     
  Misses        142      142              
Files Coverage Δ
src/core_codemods/__init__.py 100.00% <100.00%> (ø)
...rc/core_codemods/literal_or_new_object_identity.py 100.00% <100.00%> (ø)

@andrecsilva andrecsilva marked this pull request as ready for review December 21, 2023 12:13
Copy link
Member

@drdavella drdavella left a comment

Choose a reason for hiding this comment

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

Approving with comments. My main substantive comment/question is about expressions with literals on the LHS.

def leave_Comparison(
self, original_node: cst.Comparison, updated_node: cst.Comparison
) -> cst.BaseExpression:
if self.filter_by_path_includes_or_excludes(self.node_position(original_node)):
Copy link
Member

Choose a reason for hiding this comment

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

I realized I've forgotten this filter in many of my own recent codemods so I'll need to go back and make updates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another reason to have another look at the API. This should be automated in these simple cases.

case cst.Comparison(
left=left, comparisons=[cst.ComparisonTarget() as target]
):
if isinstance(target.operator, cst.Is | cst.IsNot):
Copy link
Member

Choose a reason for hiding this comment

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

Might be ever so slightly cleaner if you match here? But possibly I'm just obsessed with match right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, I'm not using the operator in any place. Using match would add an extra indentation which is why I avoid in those simple cases.

@drdavella
Copy link
Member

One additional comment but Sonar calls this out as a non-compliant example as well:

    mylist = []  # mylist is assigned a new object
    param is mylist  # Noncompliant: always False

I know it's harder for us to do constant propagation without semgrep but we might want to add a ticket for this.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions

C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

idea Catch issues before they fail your Quality Gate with our IDE extension SonarLint SonarLint

@andrecsilva
Copy link
Contributor Author

One additional comment but Sonar calls this out as a non-compliant example as well:

    mylist = []  # mylist is assigned a new object
    param is mylist  # Noncompliant: always False

I know it's harder for us to do constant propagation without semgrep but we might want to add a ticket for this.

I'll do this in a future PR. We kinda have the solution to that already but I need to do some testing here.

@andrecsilva andrecsilva added this pull request to the merge queue Dec 21, 2023
Merged via the queue into main with commit 4d87b31 Dec 21, 2023
13 of 14 checks passed
@andrecsilva andrecsilva deleted the object-creation-id branch December 21, 2023 17:17
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