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

ast/compile: unused imports follow up #4427

Conversation

srenatus
Copy link
Contributor

This is a folllow-up to #4377.

The approach I had advised to take in #4377 fell a little short in the case of shadowed vars: Considering

package p
import data.foo # unused
r { f(1) }
f(foo) = foo == 1

and

package p
import data.foo # unused
r { foo := true; foo }

we have a really hard time figuring out that the vars in those bodies are not actually making use of the imports.

To remedy this, we'll need to do the same work done in resolveAllRefs, which turned out to be much more involved than an ast.Walk*.

In the last commit, we're merging the check with the resolveAllRefs stage, because it's avoiding some extra work; and gives us better coverage if there are ever any lazily loaded modules.

@srenatus srenatus marked this pull request as ready for review March 11, 2022 09:48
johanfylling
johanfylling previously approved these changes Mar 22, 2022
Copy link
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

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

LGTM!

ast/compile.go Show resolved Hide resolved
Message: "import data.foo unused",
},
},
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also add test cases for when an import is used/not used as a rule value? E.g.

package p
import data.foo # unused
import data.bar

r = bar { true }

and

package p
import data.foo # unused
import data.bar #unused

r = data.bar { true }

* keep future imports untouched in check for unused imports

  Rationale: you might start out with a default header when creating a new
  rego policy -- there shouldn't be anything stopping you from using all the
  latest features then.

* capture unused imports that are shadowed

  This would pretty much copy the work done in resolveAllRefs; so the extra
  check done as part of resolveAllRefs is advantageous:

  - we capture the cases when extra modules are lazily loaded
  - we don't walk all the code twice

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
@srenatus srenatus force-pushed the sr/ast/strict/unused-imports-follow-up branch from 17082e9 to 9e71a27 Compare March 23, 2022 09:19
@srenatus
Copy link
Contributor Author

Will merge when green ✅

@srenatus srenatus merged commit dc0e03f into open-policy-agent:main Mar 23, 2022
@srenatus srenatus deleted the sr/ast/strict/unused-imports-follow-up branch March 23, 2022 09:26
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.

None yet

2 participants