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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions cpp/ql/lib/semmle/code/cpp/Class.qll
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ class Class extends UserType {
* it is callable by a particular caller. For C++11, there's also a question
* of whether to include members that are defaulted or deleted.
*/
deprecated predicate hasCopyConstructor() {
exists(CopyConstructor cc | cc = this.getAMemberFunction())
}
deprecated predicate hasCopyConstructor() { this.getAMemberFunction() instanceof CopyConstructor }

/**
* Holds if this class has a copy assignment operator that is either
Expand All @@ -224,7 +222,7 @@ class Class extends UserType {
* or deleted.
*/
deprecated predicate hasCopyAssignmentOperator() {
exists(CopyAssignmentOperator coa | coa = this.getAMemberFunction())
this.getAMemberFunction() instanceof CopyAssignmentOperator
}

/**
Expand Down Expand Up @@ -887,7 +885,7 @@ class NestedClass extends Class {
* pure virtual function.
*/
class AbstractClass extends Class {
AbstractClass() { exists(PureVirtualFunction f | this.getAMemberFunction() = f) }
AbstractClass() { this.getAMemberFunction() instanceof PureVirtualFunction }

override string getAPrimaryQlClass() { result = "AbstractClass" }
}
Expand Down
4 changes: 2 additions & 2 deletions cpp/ql/lib/semmle/code/cpp/Specifier.qll
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ class AttributeArgument extends Element, @attribute_arg {
override Location getLocation() { attribute_args(underlyingElement(this), _, _, _, result) }

override string toString() {
if exists(@attribute_arg_empty self | self = underlyingElement(this))
if underlyingElement(this) instanceof @attribute_arg_empty
then result = "empty argument"
else
exists(string prefix, string tail |
(if exists(this.getName()) then prefix = this.getName() + "=" else prefix = "") and
(
if exists(@attribute_arg_type self | self = underlyingElement(this))
if underlyingElement(this) instanceof @attribute_arg_type
then tail = this.getValueType().getName()
else tail = this.getValueText()
) and
Expand Down
2 changes: 1 addition & 1 deletion cpp/ql/lib/semmle/code/cpp/XML.qll
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
XMLAttribute getAttribute(string name) { result.getElement() = this and result.getName() = name }

/** Holds if this XML element has an attribute with the specified `name`. */
predicate hasAttribute(string name) { exists(XMLAttribute a | a = this.getAttribute(name)) }
predicate hasAttribute(string name) { exists(this.getAttribute(name)) }

/** Gets the value of the attribute with the specified `name`, if any. */
string getAttributeValue(string name) { result = this.getAttribute(name).getValue() }
Expand Down
4 changes: 2 additions & 2 deletions cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GuardCondition extends Expr {
exists(IRGuardCondition ir | this = ir.getUnconvertedResultExpression())
or
// no binary operators in the IR
exists(GuardCondition gc | this.(BinaryLogicalOperation).getAnOperand() = gc)
this.(BinaryLogicalOperation).getAnOperand() instanceof GuardCondition
or
// the IR short-circuits if(!x)
// don't produce a guard condition for `y = !x` and other non-short-circuited cases
Expand Down Expand Up @@ -98,7 +98,7 @@ class GuardCondition extends Expr {
*/
private class GuardConditionFromBinaryLogicalOperator extends GuardCondition {
GuardConditionFromBinaryLogicalOperator() {
exists(GuardCondition gc | this.(BinaryLogicalOperation).getAnOperand() = gc)
this.(BinaryLogicalOperation).getAnOperand() instanceof GuardCondition
}

override predicate controls(BasicBlock controlled, boolean testIsTrue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private class Argument extends Expr {
*/
class ArgumentNode extends Node {
ArgumentNode() {
exists(Argument arg | this.asExpr() = arg) or
this.asExpr() instanceof Argument or
this = getInstanceArgument(_)
}

Expand Down
12 changes: 5 additions & 7 deletions cpp/ql/lib/semmle/code/cpp/exprs/Access.qll
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class VariableAccess extends Access, @varaccess {
exists(Assignment a | a.getLValue() = this) or
exists(CrementOperation c | c.getOperand() = this) or
exists(AddressOfExpr addof | addof.getOperand() = this) or
exists(ReferenceToExpr rte | this.getConversion() = rte) or
exists(ArrayToPointerConversion atpc | this.getConversion() = atpc)
this.getConversion() instanceof ReferenceToExpr or
this.getConversion() instanceof ArrayToPointerConversion
}

/**
Expand All @@ -104,8 +104,8 @@ class VariableAccess extends Access, @varaccess {
predicate isRValue() {
not exists(AssignExpr ae | ae.getLValue() = this) and
not exists(AddressOfExpr addof | addof.getOperand() = this) and
not exists(ReferenceToExpr rte | this.getConversion() = rte) and
not exists(ArrayToPointerConversion atpc | this.getConversion() = atpc)
not this.getConversion() instanceof ReferenceToExpr and
not this.getConversion() instanceof ArrayToPointerConversion
}

/**
Expand Down Expand Up @@ -218,9 +218,7 @@ class PointerFieldAccess extends FieldAccess {
class DotFieldAccess extends FieldAccess {
override string getAPrimaryQlClass() { result = "DotFieldAccess" }

DotFieldAccess() {
exists(Class c | c = this.getQualifier().getFullyConverted().getUnspecifiedType())
}
DotFieldAccess() { this.getQualifier().getFullyConverted().getUnspecifiedType() instanceof Class }
}

/**
Expand Down
2 changes: 1 addition & 1 deletion cpp/ql/lib/semmle/code/cpp/exprs/Call.qll
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Call extends Expr, NameQualifiableElement, TCall {
*
* For example, `ptr->f()` has a qualifier, whereas plain `f()` does not.
*/
predicate hasQualifier() { exists(Expr e | this.getChild(-1) = e) }
predicate hasQualifier() { exists(this.getChild(-1)) }

/**
* Gets the expression to the left of the function name or function pointer variable name.
Expand Down
4 changes: 2 additions & 2 deletions cpp/ql/lib/semmle/code/cpp/exprs/Cast.qll
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ class SizeofOperator extends Expr, @runtime_sizeof {
* ```
*/
class SizeofExprOperator extends SizeofOperator {
SizeofExprOperator() { exists(Expr e | this.getChild(0) = e) }
SizeofExprOperator() { exists(this.getChild(0)) }

override string getAPrimaryQlClass() { result = "SizeofExprOperator" }

Expand Down Expand Up @@ -787,7 +787,7 @@ class AlignofOperator extends Expr, @runtime_alignof {
* ```
*/
class AlignofExprOperator extends AlignofOperator {
AlignofExprOperator() { exists(Expr e | this.getChild(0) = e) }
AlignofExprOperator() { exists(this.getChild(0)) }

/**
* Gets the contained expression.
Expand Down
40 changes: 20 additions & 20 deletions cpp/ql/lib/semmle/code/cpp/metrics/MetricClass.qll
Original file line number Diff line number Diff line change
Expand Up @@ -308,45 +308,45 @@ class MetricClass extends Class {
}

private string getAUsedHalsteadN1Operator() {
exists(CommaExpr e | e = this.getAnEnclosedExpression()) and result = "comma"
this.getAnEnclosedExpression() instanceof CommaExpr and result = "comma"
or
exists(ReferenceToExpr e | e = this.getAnEnclosedExpression()) and result = "refTo"
this.getAnEnclosedExpression() instanceof ReferenceToExpr and result = "refTo"
or
exists(PointerDereferenceExpr e | e = this.getAnEnclosedExpression()) and result = "dereference"
this.getAnEnclosedExpression() instanceof PointerDereferenceExpr and result = "dereference"
or
exists(CStyleCast e | e = this.getAnEnclosedExpression()) and result = "cCast"
this.getAnEnclosedExpression() instanceof CStyleCast and result = "cCast"
or
exists(StaticCast e | e = this.getAnEnclosedExpression()) and result = "staticCast"
this.getAnEnclosedExpression() instanceof StaticCast and result = "staticCast"
or
exists(ConstCast e | e = this.getAnEnclosedExpression()) and result = "constCast"
this.getAnEnclosedExpression() instanceof ConstCast and result = "constCast"
or
exists(ReinterpretCast e | e = this.getAnEnclosedExpression()) and result = "reinterpretCast"
this.getAnEnclosedExpression() instanceof ReinterpretCast and result = "reinterpretCast"
or
exists(DynamicCast e | e = this.getAnEnclosedExpression()) and result = "dynamicCast"
this.getAnEnclosedExpression() instanceof DynamicCast and result = "dynamicCast"
or
exists(SizeofExprOperator e | e = this.getAnEnclosedExpression()) and result = "sizeofExpr"
this.getAnEnclosedExpression() instanceof SizeofExprOperator and result = "sizeofExpr"
or
exists(SizeofTypeOperator e | e = this.getAnEnclosedExpression()) and result = "sizeofType"
this.getAnEnclosedExpression() instanceof SizeofTypeOperator and result = "sizeofType"
or
exists(IfStmt e | e = this.getAnEnclosedStmt()) and result = "ifVal"
this.getAnEnclosedStmt() instanceof IfStmt and result = "ifVal"
or
exists(SwitchStmt e | e = this.getAnEnclosedStmt()) and result = "switchVal"
this.getAnEnclosedStmt() instanceof SwitchStmt and result = "switchVal"
or
exists(ForStmt e | e = this.getAnEnclosedStmt()) and result = "forVal"
this.getAnEnclosedStmt() instanceof ForStmt and result = "forVal"
or
exists(DoStmt e | e = this.getAnEnclosedStmt()) and result = "doVal"
this.getAnEnclosedStmt() instanceof DoStmt and result = "doVal"
or
exists(WhileStmt e | e = this.getAnEnclosedStmt()) and result = "whileVal"
this.getAnEnclosedStmt() instanceof WhileStmt and result = "whileVal"
or
exists(GotoStmt e | e = this.getAnEnclosedStmt()) and result = "gotoVal"
this.getAnEnclosedStmt() instanceof GotoStmt and result = "gotoVal"
or
exists(ContinueStmt e | e = this.getAnEnclosedStmt()) and result = "continueVal"
this.getAnEnclosedStmt() instanceof ContinueStmt and result = "continueVal"
or
exists(BreakStmt e | e = this.getAnEnclosedStmt()) and result = "breakVal"
this.getAnEnclosedStmt() instanceof BreakStmt and result = "breakVal"
or
exists(ReturnStmt e | e = this.getAnEnclosedStmt()) and result = "returnVal"
this.getAnEnclosedStmt() instanceof ReturnStmt and result = "returnVal"
or
exists(SwitchCase e | e = this.getAnEnclosedStmt()) and result = "caseVal"
this.getAnEnclosedStmt() instanceof SwitchCase and result = "caseVal"
or
exists(IfStmt s | s = this.getAnEnclosedStmt() and s.hasElse()) and
result = "elseVal"
Expand Down
2 changes: 1 addition & 1 deletion cpp/ql/lib/semmle/code/cpp/padding/Padding.qll
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class PaddedType extends Class {
// Support only single inheritance for now. If multiple inheritance is
// supported, be sure to fix up the calls to getABaseClass*() to correctly
// handle the presence of multiple base class subojects with the same type.
not exists(ClassDerivation cd | cd = this.getDerivation(1))
not exists(this.getDerivation(1))
}

/**
Expand Down
2 changes: 1 addition & 1 deletion cpp/ql/lib/semmle/code/cpp/pointsto/PointsTo.qll
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ predicate lvalue(Element e) {
or
exists(Cast c | lvalue(c) and e.(Expr).getConversion() = c)
or
exists(ReferenceToExpr toref | e.(Expr).getConversion() = toref)
e.(Expr).getConversion() instanceof ReferenceToExpr
or
// If f is a function-pointer, then the following two
// calls are equivalent: f() and (*f)()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private predicate insideFunctionValueMoveTo(Element src, Element dest) {
format.getConversionChar(sourceArg - ffc.getTarget().getNumberOfParameters()) = ["s", "S"]
)
or
not exists(FormatLiteral fl | fl = c.(FormattingFunctionCall).getFormat())
not c.(FormattingFunctionCall).getFormat() instanceof FormatLiteral
or
not c instanceof FormattingFunctionCall
) and
Expand Down
4 changes: 2 additions & 2 deletions cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class IfStmt extends ConditionalStmt, @stmt_if {
* if (b) { x = 1; }
* ```
*/
predicate hasElse() { exists(Stmt s | this.getElse() = s) }
predicate hasElse() { exists(this.getElse()) }

override string toString() { result = "if (...) ... " }

Expand Down Expand Up @@ -357,7 +357,7 @@ class ConstexprIfStmt extends ConditionalStmt, @stmt_constexpr_if {
* if constexpr (b) { x = 1; }
* ```
*/
predicate hasElse() { exists(Stmt s | this.getElse() = s) }
predicate hasElse() { exists(this.getElse()) }

override string toString() { result = "if constexpr (...) ... " }

Expand Down
4 changes: 2 additions & 2 deletions cpp/ql/src/Critical/DeadCodeGoto.ql
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ where
// the next statement isn't breaking out of a switch
not s.(BreakStmt).getBreakable() instanceof SwitchStmt and
// the next statement isn't a loop that can be jumped into
not exists(LabelStmt ls | s.(Loop).getStmt().getAChild*() = ls) and
not exists(SwitchCase sc | s.(Loop).getStmt().getAChild*() = sc) and
not s.(Loop).getStmt().getAChild*() instanceof LabelStmt and
not s.(Loop).getStmt().getAChild*() instanceof SwitchCase and
// no preprocessor logic applies
not functionContainsPreprocCode(js.getEnclosingFunction())
select js, "This statement makes $@ unreachable.", s, s.toString()
6 changes: 2 additions & 4 deletions cpp/ql/src/Likely Bugs/Leap Year/LeapYear.qll
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class LeapYearFieldAccess extends YearFieldAccess {
op.getAnOperand() = this and
(
op instanceof AssignArithmeticOperation or
exists(BinaryArithmeticOperation bao | bao = op.getAnOperand()) or
op.getAnOperand() instanceof BinaryArithmeticOperation or
op instanceof CrementOperation
)
)
Expand Down Expand Up @@ -212,9 +212,7 @@ class ChecksForLeapYearFunctionCall extends FunctionCall {
class LeapYearCheckConfiguration extends DataFlow::Configuration {
LeapYearCheckConfiguration() { this = "LeapYearCheckConfiguration" }

override predicate isSource(DataFlow::Node source) {
exists(VariableAccess va | va = source.asExpr())
}
override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof VariableAccess }

override predicate isSink(DataFlow::Node sink) {
exists(ChecksForLeapYearFunctionCall fc | sink.asExpr() = fc.getAnArgument())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ class SetSecurityDescriptorDaclFunctionCall extends FunctionCall {
class NullDaclConfig extends DataFlow::Configuration {
NullDaclConfig() { this = "NullDaclConfig" }

override predicate isSource(DataFlow::Node source) {
exists(NullValue nullExpr | source.asExpr() = nullExpr)
}
override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof NullValue }

override predicate isSink(DataFlow::Node sink) {
exists(SetSecurityDescriptorDaclFunctionCall call, VariableAccess val | val = sink.asExpr() |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class CallUsedToHandleErrors extends FunctionCall {
not exists(this.(ControlFlowNode).getASuccessor())
or
// call throwing an exception
exists(ThrowExpr tex | tex = this.(ControlFlowNode).getASuccessor())
this.(ControlFlowNode).getASuccessor() instanceof ThrowExpr
or
// call logging a message, possibly an error
exists(FormattingFunction ff | ff = this.(ControlFlowNode).getASuccessor())
this.(ControlFlowNode).getASuccessor() instanceof FormattingFunction
or
// enabling recursive search
exists(CallUsedToHandleErrors fr | getTarget() = fr.getEnclosingFunction())
Expand All @@ -37,9 +37,9 @@ class CallUsedToHandleErrors extends FunctionCall {
/** Holds if the conditions for a call outside the wrapper function are met. */
predicate conditionsOutsideWrapper(FunctionCall fcp) {
fcp.getNumberOfArguments() > 0 and
not exists(ConditionalStmt cdtmp | fcp.getEnclosingStmt().getParentStmt*() = cdtmp) and
not exists(Loop lptmp | fcp.getEnclosingStmt().getParentStmt*() = lptmp) and
not exists(ReturnStmt rttmp | fcp.getEnclosingStmt().getParentStmt*() = rttmp) and
not fcp.getEnclosingStmt().getParentStmt*() instanceof ConditionalStmt and
not fcp.getEnclosingStmt().getParentStmt*() instanceof Loop and
not fcp.getEnclosingStmt().getParentStmt*() instanceof ReturnStmt and
not exists(FunctionCall fctmp2 | fcp = fctmp2.getAnArgument().getAChild*()) and
not exists(Assignment astmp | fcp = astmp.getRValue().getAChild*()) and
not exists(Initializer intmp | fcp = intmp.getExpr().getAChild*()) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CallMayNotReturn extends FunctionCall {
// call to another function that may not return
exists(CallMayNotReturn exit | getTarget() = exit.getEnclosingFunction())
or
exists(ThrowExpr tex | tex = this.(ControlFlowNode).getASuccessor())
this.(ControlFlowNode).getASuccessor() instanceof ThrowExpr
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ predicate similarArguments(FunctionCall fc, FunctionCall fc1) {

from FunctionCall fc, FunctionCall fc1
where
not exists(CallMayNotReturn fctmp | fctmp = fc.getASuccessor*()) and
not fc.getASuccessor*() instanceof CallMayNotReturn and
not exists(IfStmt ifs | ifs.getCondition().getAChild*() = fc) and
(
// detecting a repeated call situation within one function
Expand Down
2 changes: 1 addition & 1 deletion cpp/ql/src/jsf/4.10 Classes/AV Rule 97.1.ql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ from EqualityOperation e, PointerToMemberType t, Class c
where
e.getAnOperand().getType() = t and
t.getClass() = c and
exists(VirtualFunction f | c.getAMemberFunction() = f)
c.getAMemberFunction() instanceof VirtualFunction
select e,
"AV Rule 97.1: Neither operand of an equality operator shall be a pointer to a virtual member function."
2 changes: 1 addition & 1 deletion csharp/ql/lib/Linq/Helpers.qll
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ predicate missedAllOpportunity(ForeachStmt fes) {
bl = a.getRValue() and
bl.toString() = "false"
) and
exists(BreakStmt bs | bs = is.getThen().getAChild*())
is.getThen().getAChild*() instanceof BreakStmt
)
}

Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/lib/semmle/code/csharp/XML.qll
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
XMLAttribute getAttribute(string name) { result.getElement() = this and result.getName() = name }

/** Holds if this XML element has an attribute with the specified `name`. */
predicate hasAttribute(string name) { exists(XMLAttribute a | a = this.getAttribute(name)) }
predicate hasAttribute(string name) { exists(this.getAttribute(name)) }

/** Gets the value of the attribute with the specified `name`, if any. */
string getAttributeValue(string name) { result = this.getAttribute(name).getValue() }
Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/lib/semmle/code/csharp/dispatch/Dispatch.qll
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private module Internal {
pragma[nomagic]
predicate hasSubsumedQualifierTypeOverridden(Gvn::GvnType t, OverridableCallable c) {
this.hasSubsumedQualifierType(t) and
hasCallable(t, c, any(OverridableCallable oc | oc = this.getAStaticTargetExt()))
hasCallable(t, c, this.getAStaticTargetExt())
}

/**
Expand Down
4 changes: 2 additions & 2 deletions csharp/ql/src/Likely Bugs/Statements/UseBraces.ql
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Stmt getASuccessorStmt(Stmt s) {
}

class IfThenStmt extends IfStmt {
IfThenStmt() { not exists(Stmt s | getElse() = s) }
IfThenStmt() { not exists(getElse()) }
}

class IfThenElseStmt extends IfStmt {
IfThenElseStmt() { exists(Stmt s | getElse() = s) }
IfThenElseStmt() { exists(getElse()) }
}

Stmt getTrailingBody(Stmt s) {
Expand Down
Loading