Skip to content

Commit fd020b5

Browse files
authored
Merge pull request #20232 from igfoo/igfoo/SloppyGlobal
C++: SloppyGlobal: Don't alert on template instantiations, only the template
2 parents 299ccb6 + 0870cc3 commit fd020b5

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

cpp/ql/src/Best Practices/SloppyGlobal.ql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import semmle.code.cpp.ConfigurationTestFile
1414
from GlobalVariable gv
1515
where
1616
gv.getName().length() <= 3 and
17+
// We will give an alert for the TemplateVariable, so we don't
18+
// need to also give one for each instantiation
19+
not gv instanceof VariableTemplateInstantiation and
1720
not gv.isStatic() and
1821
not gv.getFile() instanceof ConfigurationTestFile // variables in files generated during configuration are likely false positives
1922
select gv,
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 `cpp/short-global-name` query will no longer give alerts for instantiations of template variables, only for the template itself.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
| main.cpp:3:5:3:5 | x | Poor global variable name 'x'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). |
22
| main.cpp:4:5:4:6 | ys | Poor global variable name 'ys'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). |
3+
| main.cpp:9:5:9:6 | v1 | Poor global variable name 'v1'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). |
4+
| main.cpp:10:5:10:6 | v2 | Poor global variable name 'v2'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). |
5+
| main.cpp:12:5:12:5 | v3 | Poor global variable name 'v3'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). |
6+
| main.cpp:14:5:14:5 | v4 | Poor global variable name 'v4'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). |
7+
| main.cpp:16:5:16:5 | v5 | Poor global variable name 'v5'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). |

cpp/ql/test/query-tests/Best Practices/SloppyGlobal/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,19 @@ int ys[1000000]; // BAD: too short
55
int descriptive_name; // GOOD: sufficient
66

77
static int z; // GOOD: not a global
8+
9+
int v1; // BAD: too short
10+
int v2; // BAD: too short
11+
template <typename T>
12+
T v3; // BAD: too short
13+
template <typename T>
14+
T v4; // BAD: too short
15+
template <typename T>
16+
T v5; // BAD: too short
17+
18+
void use_some_fs() {
19+
v2 = 100;
20+
v4<int> = 200;
21+
v5<int> = 300;
22+
v5<const char *> = "string";
23+
}

0 commit comments

Comments
 (0)