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

Rust: Implement UnusedVariable.ql #17642

Merged
merged 2 commits into from
Oct 2, 2024
Merged

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Oct 2, 2024

Since this query is based entirely on syntax, we can implement it already now without SSA.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Oct 2, 2024
@hvitved hvitved marked this pull request as ready for review October 2, 2024 08:05
@hvitved hvitved requested a review from geoffw0 October 2, 2024 08:11
Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

LGTM.

A few questions and optional suggestions.

/** Holds if `e` occurs in the LHS of a (compound) assignment. */
private predicate assignLhs(Expr e) {
exists(BinaryExpr be |
be.getOperatorName().regexpMatch(".*=") 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 don't feel strongly about this one, but I believe matches is still preferred over regexpMatch where possible???

Suggested change
be.getOperatorName().regexpMatch(".*=") and
be.getOperatorName().matches("%=") and

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would expect matches to do the same thing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, there was a discussion a long while back about whether the optimiser is more aware of matches, I can't remember the answer. Anyway, up to you.

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll Outdated Show resolved Hide resolved
rust/ql/test/query-tests/unusedentities/main.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

LGTM.

I believe this is our first working problem query! ✨

@hvitved hvitved merged commit d6415cd into github:main Oct 2, 2024
15 checks passed
@hvitved hvitved deleted the rust/unused-variable branch October 2, 2024 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants