Skip to content

Commit 4990f16

Browse files
committed
Refactor struct field predicate to remove redundancy
1 parent 8dc0688 commit 4990f16

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

go/ql/lib/semmle/go/Types.qll

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -545,15 +545,7 @@ class StructType extends @structtype, CompositeType {
545545
predicate hasField(string name, Type tp) {
546546
exists(int mindepth |
547547
mindepth = min(int depth | this.hasFieldCand(name, _, depth, _)) and
548-
tp = unique(Field f | f = this.getFieldCand(name, mindepth, _)).getType()
549-
)
550-
}
551-
552-
private Field getFieldCand(string name, int depth, boolean isEmbedded) {
553-
result = this.getOwnField(name, isEmbedded) and depth = 0
554-
or
555-
exists(Type embedded | this.hasEmbeddedField(embedded, depth - 1) |
556-
result = embedded.getUnderlyingType().(StructType).getOwnField(name, isEmbedded)
548+
tp = unique(Field f | this.hasFieldCand(name, f, mindepth, _)).getType()
557549
)
558550
}
559551

@@ -568,9 +560,9 @@ class StructType extends @structtype, CompositeType {
568560
* The depth of a field `f` declared in this type is zero.
569561
*/
570562
Field getFieldAtDepth(string name, int depth) {
571-
depth = min(int depthCand | exists(this.getFieldCand(name, depthCand, _))) and
572-
result = this.getFieldCand(name, depth, _) and
573-
strictcount(this.getFieldCand(name, depth, _)) = 1
563+
depth = min(int depthCand | exists(Field f | this.hasFieldCand(name, f, depthCand, _))) and
564+
this.hasFieldCand(name, result, depth, _) and
565+
strictcount(Field f | this.hasFieldCand(name, f, depth, _)) = 1
574566
}
575567

576568
Method getMethodAtDepth(string name, int depth) {

0 commit comments

Comments
 (0)