Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e01e40c

Browse files
committed
Fix FuncTypeExpr.getNumParameter
It actually counts the number of parameter declarations. We correct it to do what it says and introduce `FuncTypeExpr.getNumParameterDecls`, which we then use in `FuncTypeExpr.getUniquelyNumberedChild`.
1 parent 1b49bfe commit e01e40c

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The predicate `getNumParameter` on `FuncTypeExpr` has been changed to actually give the number of parameters. It previously gave the number of parameter declarations. `getNumParameterDecl` has been introduced to preserve this functionality.

go/ql/lib/semmle/go/Expr.qll

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -980,18 +980,36 @@ class StructTypeExpr extends @structtypeexpr, TypeExpr, FieldParent {
980980
* Examples:
981981
*
982982
* ```go
983-
* func(a, b int, c float32) (float32, bool)
983+
* func(a int, b, c float32) (float32, bool)
984984
* ```
985985
*/
986986
class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode, FieldParent {
987987
/** Gets the `i`th parameter of this function type (0-based). */
988988
ParameterDecl getParameterDecl(int i) { result = this.getField(i) and i >= 0 }
989989

990-
/** Gets a parameter of this function type. */
990+
/**
991+
* Gets a parameter declaration of this function type.
992+
*
993+
* For example, for `func(a int, b, c float32) (float32, bool)` the result is
994+
* `a int` or `b, c float32`.
995+
*/
991996
ParameterDecl getAParameterDecl() { result = this.getParameterDecl(_) }
992997

993-
/** Gets the number of parameters of this function type. */
994-
int getNumParameter() { result = count(this.getAParameterDecl()) }
998+
/**
999+
* Gets the number of parameter declarations of this function type.
1000+
*
1001+
* For example, for `func(a int, b, c float32) (float32, bool)` the result is 2:
1002+
* `a int` and `b, c float32`.
1003+
*/
1004+
int getNumParameterDecl() { result = count(this.getAParameterDecl()) }
1005+
1006+
/**
1007+
* Gets the number of parameters of this function type.
1008+
*
1009+
* For example, for `func(a int, b, c float32) (float32, bool)` the result is 3:
1010+
* `a`, `b` and `c`.
1011+
*/
1012+
int getNumParameter() { result = count(this.getAParameterDecl().getANameExpr()) }
9951013

9961014
/** Gets the `i`th result of this function type (0-based). */
9971015
ResultVariableDecl getResultDecl(int i) { result = this.getField(-(i + 1)) }
@@ -1011,9 +1029,9 @@ class FuncTypeExpr extends @functypeexpr, TypeExpr, ScopeNode, FieldParent {
10111029

10121030
/** Gets the `i`th child of this node, parameters first followed by results. */
10131031
override AstNode getUniquelyNumberedChild(int i) {
1014-
if i < this.getNumParameter()
1032+
if i < this.getNumParameterDecl()
10151033
then result = this.getParameterDecl(i)
1016-
else result = this.getResultDecl(i - this.getNumParameter())
1034+
else result = this.getResultDecl(i - this.getNumParameterDecl())
10171035
}
10181036
}
10191037

0 commit comments

Comments
 (0)