Skip to content

Commit 97ccd29

Browse files
committed
IdentifierHidden: add heuristic for hiding in lambda
1 parent 63cec52 commit 97ccd29

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

cpp/common/src/codingstandards/cpp/rules/identifierhidden/IdentifierHidden.qll

+20-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ abstract class IdentifierHiddenSharedQuery extends Query { }
1111

1212
Query getQuery() { result instanceof IdentifierHiddenSharedQuery }
1313

14+
/**
15+
* There is a lambda that contains a declaration
16+
* that hides something that is captured
17+
* and the lambda exists in the function where this lamda is enclosed
18+
*/
19+
predicate hiddenInLambda(UserDeclaration v2, UserDeclaration v1) {
20+
exists(Scope s, Closure le |
21+
s.getADeclaration() = v2 and
22+
s.getAnAncestor() = le and
23+
le.getEnclosingFunction().getBasicBlock().(Scope) = v1.getParentScope() and
24+
exists(LambdaCapture cap, Variable v |
25+
v.getAnAccess() = cap.getInitializer().(VariableAccess) and
26+
v = v1 and
27+
le.getLambdaExpression().getACapture() = cap
28+
) and
29+
v2.getName() = v1.getName()
30+
)
31+
}
32+
1433
query predicate problems(UserDeclaration v2, string message, UserDeclaration v1, string varName) {
1534
not isExcluded(v1, getQuery()) and
1635
not isExcluded(v2, getQuery()) and
@@ -20,7 +39,7 @@ query predicate problems(UserDeclaration v2, string message, UserDeclaration v1,
2039
//ignore types for this rule
2140
not v2 instanceof Type and
2241
not v1 instanceof Type and
23-
hidesStrict(v1, v2) and
42+
(hidesStrict(v1, v2) or hiddenInLambda(v2, v1)) and
2443
not excludedViaNestedNamespaces(v2, v1) and
2544
varName = v1.getName() and
2645
message = "Declaration is hiding declaration $@."

cpp/common/test/rules/identifierhidden/IdentifierHidden.expected

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
| test.cpp:70:12:70:12 | i | Declaration is hiding declaration $@. | test.cpp:61:7:61:7 | i | i |
1010
| test.cpp:75:16:75:16 | i | Declaration is hiding declaration $@. | test.cpp:61:7:61:7 | i | i |
1111
| test.cpp:81:5:81:5 | a | Declaration is hiding declaration $@. | test.cpp:79:5:79:5 | a | a |
12+
| test.cpp:102:9:102:9 | b | Declaration is hiding declaration $@. | test.cpp:96:11:96:11 | b | b |

cpp/common/test/rules/identifierhidden/test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void f4() {
9999
};
100100

101101
auto lambda2 = [b]() {
102-
int b = 10; // NON_COMPLIANT[FALSE_NEGATIVE] - not an exception - captured
102+
int b = 10; // NON_COMPLIANT - not an exception - captured
103103
// variable b
104104
};
105105
}

0 commit comments

Comments
 (0)