-
Notifications
You must be signed in to change notification settings - Fork 1.7k
QL: add restrictive transitive closure query #8411
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
Open
erik-krogh
wants to merge
4
commits into
github:main
Choose a base branch
from
erik-krogh:starRestrict
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @name Restrictive transitive closure | ||
* @description The transitive closure operation might restrict the type even when taking zero steps. | ||
* @kind problem | ||
* @problem.severity warning | ||
* @id ql/restrictive-transitive-closure | ||
* @tags correctness | ||
* maintainability | ||
* @precision high | ||
*/ | ||
|
||
import ql | ||
|
||
Class base(Call c) { | ||
result = c.(MemberCall).getBase().getType().getDeclaration() | ||
or | ||
not c instanceof MemberCall and | ||
result = c.getTarget().(ClassPredicate).getParent() | ||
} | ||
|
||
Call closureCall() { result.isClosure("*") } | ||
|
||
Class return(Call c) { result = c.getTarget().getReturnType().getDeclaration() } | ||
|
||
Class superClass(Class c) { result = c.getASuperType().getResolvedType().getDeclaration() } | ||
|
||
from Call c, Class return, Class base | ||
where | ||
c = closureCall() and | ||
return = return(c) and | ||
base = base(c) and | ||
// We aren't restricted anyway from the surrounding code. | ||
not superClass*(base) = return and | ||
not exists(InstanceOf inst | | ||
inst.getExpr() = c and | ||
superClass*(inst.getType().getResolvedType().getDeclaration()) = return | ||
) and | ||
not exists(ComparisonFormula comp, Expr operand | comp.getOperator() = "=" | | ||
comp.getAnOperand() = c and | ||
operand != c and | ||
operand = comp.getAnOperand() and | ||
superClass*(operand.getType().getDeclaration()) = return | ||
) and | ||
// If the result is used in a call, then we only flag if the "closure in the middle" could be removed. | ||
forall(MemberCall memberCall | memberCall.getBase() = c | | ||
exists(ClassPredicate pred | | ||
pred = superClass*(base).getClassPredicate(memberCall.getMemberName()) and | ||
memberCall.getNumberOfArguments() = pred.getArity() | ||
) | ||
) | ||
select c, "Closure restricts type from $@ to $@, even when taking zero steps", base, base.getName(), | ||
return, return.getName() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.