Skip to content

Commit a237137

Browse files
authored
Merge pull request #6865 from MathiasVP/fix-if-none
C++/C#/JS/Python: Replace 'if p() then q() else none()' with a conjunction
2 parents 2b0415e + bdc54bc commit a237137

File tree

10 files changed

+36
-44
lines changed

10 files changed

+36
-44
lines changed

cpp/ql/lib/semmle/code/cpp/Declaration.qll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,8 @@ class Declaration extends Locatable, @declaration {
275275
* `getTemplateArgumentKind(0)`.
276276
*/
277277
final Locatable getTemplateArgumentKind(int index) {
278-
if exists(getTemplateArgumentValue(index))
279-
then result = getTemplateArgumentType(index)
280-
else none()
278+
exists(getTemplateArgumentValue(index)) and
279+
result = getTemplateArgumentType(index)
281280
}
282281

283282
/** Gets the number of template arguments for this declaration. */

cpp/ql/lib/semmle/code/cpp/controlflow/internal/ConstantExprs.qll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,13 @@ private int convertIntToType(int val, IntegralType t) {
344344
then if val = 0 then result = 0 else result = 1
345345
else
346346
if t.isUnsigned()
347-
then if val >= 0 and val.bitShiftRight(t.getSize() * 8) = 0 then result = val else none()
347+
then val >= 0 and val.bitShiftRight(t.getSize() * 8) = 0 and result = val
348348
else
349349
if val >= 0 and val.bitShiftRight(t.getSize() * 8 - 1) = 0
350350
then result = val
351-
else
352-
if (-(val + 1)).bitShiftRight(t.getSize() * 8 - 1) = 0
353-
then result = val
354-
else none()
351+
else (
352+
(-(val + 1)).bitShiftRight(t.getSize() * 8 - 1) = 0 and result = val
353+
)
355354
}
356355

357356
/**

cpp/ql/lib/semmle/code/cpp/ir/internal/IntegerInterval.qll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ Overlap getOverlap(IntValue defStart, IntValue defEnd, IntValue useStart, IntVal
1818
else
1919
if isLE(defStart, useStart) and isGE(defEnd, useEnd)
2020
then result instanceof MustTotallyOverlap
21-
else
22-
if isLE(defEnd, useStart) or isGE(defStart, useEnd)
23-
then none()
24-
else result instanceof MayPartiallyOverlap
21+
else (
22+
not isLE(defEnd, useStart) and
23+
not isGE(defStart, useEnd) and
24+
result instanceof MayPartiallyOverlap
25+
)
2526
}
2627

2728
/**

cpp/ql/lib/semmle/code/cpp/security/BufferWrite.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ class GetsBW extends BufferWriteCall {
365365
/**
366366
* Gets the index of the parameter that is the maximum number of characters to be read.
367367
*/
368-
int getParamSize() { if exists(getArgument(1)) then result = 1 else none() }
368+
int getParamSize() { exists(getArgument(1)) and result = 1 }
369369

370370
override Type getBufferType() { result = this.getTarget().getParameter(0).getUnspecifiedType() }
371371

cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ where
5050
// If either of the operands is constant, then don't include it.
5151
(
5252
if cmp.getLeftOperand().isConstant()
53-
then
54-
if cmp.getRightOperand().isConstant()
55-
then none() // Both operands are constant so don't create a message.
56-
else reason = rightReason
53+
then not cmp.getRightOperand().isConstant() and reason = rightReason
5754
else
5855
if cmp.getRightOperand().isConstant()
5956
then reason = leftReason

cpp/ql/src/Security/CWE/CWE-468/IncorrectPointerScalingCommon.qll

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,14 @@ predicate exprSourceType(Expr use, Type sourceType, Location sourceLoc) {
121121
else
122122
if use instanceof CrementOperation
123123
then exprSourceType(use.(CrementOperation).getOperand(), sourceType, sourceLoc)
124-
else
124+
else (
125125
// Conversions are not in the AST, so ignore them.
126-
if use instanceof Conversion
127-
then none()
128-
else (
129-
// Source expressions
130-
sourceType = use.getUnspecifiedType() and
131-
isPointerType(sourceType) and
132-
sourceLoc = use.getLocation()
133-
)
126+
not use instanceof Conversion and
127+
// Source expressions
128+
sourceType = use.getUnspecifiedType() and
129+
isPointerType(sourceType) and
130+
sourceLoc = use.getLocation()
131+
)
134132
}
135133

136134
/**

csharp/ql/src/experimental/ir/internal/IntegerInterval.qll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ Overlap getOverlap(IntValue defStart, IntValue defEnd, IntValue useStart, IntVal
1818
else
1919
if isLE(defStart, useStart) and isGE(defEnd, useEnd)
2020
then result instanceof MustTotallyOverlap
21-
else
22-
if isLE(defEnd, useStart) or isGE(defStart, useEnd)
23-
then none()
24-
else result instanceof MayPartiallyOverlap
21+
else (
22+
not isLE(defEnd, useStart) and
23+
not isGE(defStart, useEnd) and
24+
result instanceof MayPartiallyOverlap
25+
)
2526
}
2627

2728
/**

csharp/ql/src/experimental/ir/rangeanalysis/RangeUtils.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ IntValue getArrayDim(Variable arr) {
5252
arr.getInitializer() = ac and
5353
if exists(ac.getLengthArgument(0))
5454
then result = ac.getLengthArgument(0).getValue().toInt()
55-
else
56-
if exists(ac.getInitializer())
57-
then result = ac.getInitializer().getNumberOfElements()
58-
else none()
55+
else result = ac.getInitializer().getNumberOfElements()
5956
)
6057
}
6158

javascript/ql/lib/semmle/javascript/RangeAnalysis.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,9 @@ module RangeAnalysis {
236236
) {
237237
if exists(r.getImmediatePredecessor())
238238
then linearDefinitionSum(r.getImmediatePredecessor(), xroot, xsign, yroot, ysign, bias)
239-
else
240-
if exists(r.asExpr().getIntValue())
241-
then none() // do not model constants as sums
242-
else (
239+
else (
240+
not exists(r.asExpr().getIntValue()) and // do not model constants as sums
241+
(
243242
exists(AddExpr add, int bias1, int bias2 | r.asExpr() = add |
244243
// r = r1 + r2
245244
linearDefinition(add.getLeftOperand().flow(), xroot, xsign, bias1) and
@@ -257,6 +256,7 @@ module RangeAnalysis {
257256
linearDefinitionSum(r.asExpr().(NegExpr).getOperand().flow(), xroot, -xsign, yroot, -ysign,
258257
-bias)
259258
)
259+
)
260260
}
261261

262262
/**

python/ql/src/Functions/SignatureSpecialMethods.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ predicate incorrect_special_method_defn(
132132
else
133133
if required < func.minParameters()
134134
then message = "Too many parameters" and show_counts = true
135-
else
136-
if func.minParameters() < required and not func.getScope().hasVarArg()
137-
then
138-
message = (required - func.minParameters()) + " default values(s) will never be used" and
139-
show_counts = false
140-
else none()
135+
else (
136+
func.minParameters() < required and
137+
not func.getScope().hasVarArg() and
138+
message = (required - func.minParameters()) + " default values(s) will never be used" and
139+
show_counts = false
140+
)
141141
)
142142
}
143143

0 commit comments

Comments
 (0)