Skip to content

Commit 10172af

Browse files
author
Robert Marsh
authored
Merge pull request #1557 from jbj/hiding-range-based-for
C++: Fix DeclarationHidesVariable FP for nested range-based for loops
2 parents 50e8034 + 8d3cb78 commit 10172af

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

cpp/ql/src/Best Practices/Hiding/DeclarationHidesVariable.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Best_Practices.Hiding.Shadowing
1515
from LocalVariable lv1, LocalVariable lv2
1616
where
1717
shadowing(lv1, lv2) and
18+
not lv1.isCompilerGenerated() and
19+
not lv2.isCompilerGenerated() and
1820
not lv1.getParentScope().(Block).isInMacroExpansion() and
1921
not lv2.getParentScope().(Block).isInMacroExpansion()
2022
select lv1, "Variable " + lv1.getName() + " hides another variable of the same name (on $@).", lv2,

cpp/ql/test/query-tests/Best Practices/Hiding/DeclarationHidesVariable/hiding.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ void f(void) {
33
if (1) {
44
int i;
55

6-
for(int i = 1; i < 10; i++) {
6+
for(int i = 1; i < 10; i++) { // BAD
77
;
88
}
99
}
@@ -15,7 +15,7 @@ namespace foo {
1515
int k;
1616
try {
1717
for (i = 0; i < 3; i++) {
18-
int k;
18+
int k; // BAD
1919
}
2020
}
2121
catch (int e) {
@@ -24,4 +24,9 @@ namespace foo {
2424
}
2525
}
2626

27-
27+
void nestedRangeBasedFor() {
28+
int xs[4], ys[4];
29+
for (auto x : xs)
30+
for (auto y : ys) // GOOD
31+
x = y = 0;
32+
}

0 commit comments

Comments
 (0)