Skip to content

Commit 647d4d0

Browse files
authored
Merge pull request github#7758 from jketema/unnamed-variable-fix
C++: Do not report "Declaration hides variable" for unnamed variables
2 parents baefd62 + ee78cc7 commit 647d4d0

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ where
1818
not lv1.isCompilerGenerated() and
1919
not lv2.isCompilerGenerated() and
2020
not lv1.getParentScope().(BlockStmt).isInMacroExpansion() and
21-
not lv2.getParentScope().(BlockStmt).isInMacroExpansion()
21+
not lv2.getParentScope().(BlockStmt).isInMacroExpansion() and
22+
not lv1.getName() = "(unnamed local variable)"
2223
select lv1, "Variable " + lv1.getName() + " hides another variable of the same name (on $@).", lv2,
2324
"line " + lv2.getLocation().getStartLine().toString()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fix an issue with the `cpp/declaration-hides-variable` query where it would report variables that are unnamed in a database.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
// semmle-extractor-options: -std=c++17
22
void f(void) {
33
if (1) {
44
int i;
@@ -30,3 +30,12 @@ void nestedRangeBasedFor() {
3030
for (auto y : ys) // GOOD
3131
x = y = 0;
3232
}
33+
34+
void structuredBinding() {
35+
int xs[1] = {1};
36+
auto [x] = xs;
37+
{
38+
auto [x] = xs; // BAD [NOT DETECTED]
39+
auto [y] = xs; // GOOD
40+
}
41+
}

0 commit comments

Comments
 (0)