diff --git a/cpp/ql/src/Best Practices/SloppyGlobal.ql b/cpp/ql/src/Best Practices/SloppyGlobal.ql index 4c1935627d52..b20e0271db87 100644 --- a/cpp/ql/src/Best Practices/SloppyGlobal.ql +++ b/cpp/ql/src/Best Practices/SloppyGlobal.ql @@ -14,6 +14,9 @@ import semmle.code.cpp.ConfigurationTestFile from GlobalVariable gv where gv.getName().length() <= 3 and + // We will give an alert for the TemplateVariable, so we don't + // need to also give one for each instantiation + not gv instanceof VariableTemplateInstantiation and not gv.isStatic() and not gv.getFile() instanceof ConfigurationTestFile // variables in files generated during configuration are likely false positives select gv, diff --git a/cpp/ql/src/change-notes/2025-08-15-short-global-name-template-instantiations.md b/cpp/ql/src/change-notes/2025-08-15-short-global-name-template-instantiations.md new file mode 100644 index 000000000000..8a8f54c73372 --- /dev/null +++ b/cpp/ql/src/change-notes/2025-08-15-short-global-name-template-instantiations.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The `cpp/short-global-name` query will no longer give alerts for instantiations of template variables, only for the template itself. diff --git a/cpp/ql/test/query-tests/Best Practices/SloppyGlobal/SloppyGlobal.expected b/cpp/ql/test/query-tests/Best Practices/SloppyGlobal/SloppyGlobal.expected index 692f7d81cd6c..ceccd95ea3c4 100644 --- a/cpp/ql/test/query-tests/Best Practices/SloppyGlobal/SloppyGlobal.expected +++ b/cpp/ql/test/query-tests/Best Practices/SloppyGlobal/SloppyGlobal.expected @@ -1,2 +1,7 @@ | main.cpp:3:5:3:5 | x | Poor global variable name 'x'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). | | main.cpp:4:5:4:6 | ys | Poor global variable name 'ys'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). | +| main.cpp:9:5:9:6 | v1 | Poor global variable name 'v1'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). | +| main.cpp:10:5:10:6 | v2 | Poor global variable name 'v2'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). | +| main.cpp:12:5:12:5 | v3 | Poor global variable name 'v3'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). | +| main.cpp:14:5:14:5 | v4 | Poor global variable name 'v4'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). | +| main.cpp:16:5:16:5 | v5 | Poor global variable name 'v5'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo). | diff --git a/cpp/ql/test/query-tests/Best Practices/SloppyGlobal/main.cpp b/cpp/ql/test/query-tests/Best Practices/SloppyGlobal/main.cpp index 1b1b7ee0280b..e279fbf02579 100644 --- a/cpp/ql/test/query-tests/Best Practices/SloppyGlobal/main.cpp +++ b/cpp/ql/test/query-tests/Best Practices/SloppyGlobal/main.cpp @@ -5,3 +5,19 @@ int ys[1000000]; // BAD: too short int descriptive_name; // GOOD: sufficient static int z; // GOOD: not a global + +int v1; // BAD: too short +int v2; // BAD: too short +template +T v3; // BAD: too short +template +T v4; // BAD: too short +template +T v5; // BAD: too short + +void use_some_fs() { + v2 = 100; + v4 = 200; + v5 = 300; + v5 = "string"; +}