Skip to content

QL-for-QL: Add a could-be-cast query #7472

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 6 commits into from
Jan 19, 2022

Conversation

erik-krogh
Copy link
Contributor

@erik-krogh erik-krogh commented Dec 22, 2021

Inspired by #5160

I haven't fixed the issues identified by this query.


This query identifies exists(..)/any(..) that can be simplified to a type-cast:
exists(Type t | t = foo()) can be simplified to foo() instanceof Type.
And any(Type t | t = foo()) can be simplified to foo().(Type).

@erik-krogh erik-krogh force-pushed the redundant-aggregate branch 2 times, most recently from d0ccfd2 to c870d87 Compare December 23, 2021 08:42
@erik-krogh erik-krogh force-pushed the redundant-aggregate branch from c870d87 to 491cc76 Compare January 4, 2022 20:42
@erik-krogh erik-krogh force-pushed the redundant-aggregate branch from 491cc76 to ad11fec Compare January 4, 2022 20:47
@erik-krogh erik-krogh marked this pull request as ready for review January 4, 2022 20:56
@erik-krogh erik-krogh requested a review from tausbn as a code owner January 4, 2022 20:57
@aschackmull
Copy link
Contributor

I disagree with simplifying the exists case in general. There are many cases where it's nice to name an intermediate result to make code more readable. Changing a single-use exists-variable to an instanceof is probably a good thing, though.

Copy link
Contributor

@aschackmull aschackmull left a comment

Choose a reason for hiding this comment

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

The exists case catches too much.

@erik-krogh
Copy link
Contributor Author

The exists case catches too much.

I see. Yes, a decent amount of those warning are better as is.

I've done two things:

1: Change the severity to recommendation.

2: Restrict the flagged exists clauses to only those where the implicit type-cast is also redundant.
E.g. this code which can be changed like:

// old
exists(State state | state = getAState(this, str.length() - 1, str, _) |
  epsilonSucc*(state) = Accept(_)
)
// refactored 
epsilonSucc*(getAState(this, str.length() - 1, str, _)) = Accept(_)

@erik-krogh erik-krogh requested a review from a team January 14, 2022 10:53
@aschackmull
Copy link
Contributor

Note: an exists(..) is actually a qualifier and not an aggregate, but I'm unsure what else to call something that is either an exists(..) or an any(..).

I think it's fine to just call it an "exists" in the query name, and then use either "exists" or "any" in the message. In any case, calling it an aggregate when it definitely isn't an aggregate is somewhat problematic, I think.

@aschackmull
Copy link
Contributor

There's a FP here: https://github.com/github/codeql/security/code-scanning/5199?query=ref%3Arefs%2Fpull%2F7472%2Fmerge+ref%3Arefs%2Fpull%2F7472%2Fhead

@aschackmull
Copy link
Contributor

There are a couple of those FPs - when the single use is in just one branch of an or.

@aschackmull
Copy link
Contributor

Looking through some more results, I find that I generally agree with the "can replaced with an instanceof expression." suggestions, but I mostly wouldn't change any of the exists - the additional variable name typically helps by breaking up an otherwise large expression and documents an intermediate value through a variable name.

@aschackmull
Copy link
Contributor

So I think we should limit the query to the cases where the entire exists can be replaced by instanceof.

@erik-krogh
Copy link
Contributor Author

erik-krogh commented Jan 18, 2022

So I think we should limit the query to the cases where the entire exists can be replaced by instanceof.

I'll do that, and rewrite the query into a could be cast query.

@erik-krogh erik-krogh changed the title QL-for-QL: Add a redundant aggregate query QL-for-QL: Add a could-be-cast query Jan 18, 2022
Copy link
Contributor

@aschackmull aschackmull left a comment

Choose a reason for hiding this comment

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

Much better! Looking through a bunch of the alerts, they now all look like something I'd want to fix.

@erik-krogh
Copy link
Contributor Author

erik-krogh commented Jan 19, 2022

Much better! Looking through a bunch of the alerts, they now all look like something I'd want to fix.

Nice 🎉
And don't worry about fixing the issues, I'll write something to fix them all.


Can I get a QL-for-QL reviewer to sign off on this?
/ping @github/codeql-ql-for-ql-reviewers

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.

4 participants