Skip to content

simplify expressions that could be type-casts #7668

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

Merged
merged 1 commit into from
Jan 20, 2022

Conversation

erik-krogh
Copy link
Contributor

@erik-krogh erik-krogh commented Jan 20, 2022

Fixes the issues identified by this QL-for-QL query.


Consider the below:

predicate even(int i) { exists(Even even | even = i) }

That is basically just a type-cast, so we might as well simplify it as such:

predicate even(int i) { i instanceof Even }

There are also some cases where the resulting type-cast would be redundant.
Here's an example:

predicate hasAttribute(string name) { exists(XMLAttribute a | a = this.getAttribute(name)) }

The getAttribute predicate has XMLAttribute as its result type, so adding a type-cast would be redundant.
It is therefore simplified as follows:

predicate hasAttribute(string name) { exists(this.getAttribute(name)) }

The patch was generated automatically by a tool.
(GitHub people: you can see how in the backref below).

@erik-krogh erik-krogh added the no-change-note-required This PR does not need a change note label Jan 20, 2022
@erik-krogh erik-krogh marked this pull request as ready for review January 20, 2022 11:18
@erik-krogh erik-krogh requested review from a team as code owners January 20, 2022 11:18
Copy link
Contributor

@nickrolfe nickrolfe left a comment

Choose a reason for hiding this comment

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

👍 for Ruby

Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Java 👍

Copy link
Contributor

@tausbn tausbn left a comment

Choose a reason for hiding this comment

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

Python 👍

Comment on lines 39 to -42
exists(DataFlow::Node r | r = requestNode.getAMethodCall("body") | result = r)
or
// Otherwise, treat the response as the response body.
not exists(DataFlow::Node r | r = requestNode.getAMethodCall("body")) and
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if the line

exists(DataFlow::Node r | r = requestNode.getAMethodCall("body") | result = r)

could also be simplified to

result = requestNode.getAMethodCall("body")

if the query was extended slightly? (We know that result has type DataFlow::Node in this case.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could, and originally my query did just that (see the discussion in the PR).
But that had too many FPs, so I dropped it.

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

C# 👍

@erik-krogh erik-krogh merged commit a77b2b0 into github:main Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants