Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpp/ql/src/Best Practices/Unused Entities/UnusedLocals.ql
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ predicate declarationHasSideEffects(Variable v) {
)
or
v.getType() instanceof TemplateDependentType // may have a constructor/destructor
or
v.getInitializer().getExpr().mayBeImpure()
}

from LocalVariable v, Function f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import cpp

predicate declarationHasSideEffects(Variable v) {
exists(Class c | c = v.getUnspecifiedType() | c.hasConstructor() or c.hasDestructor())
or
v.getInitializer().getExpr().mayBeImpure()
}

from Variable v
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `cpp/unused-local-variable` and `cpp/unused-static-variable` queries no longer report variables whose initializers may have side effects.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
| code2.cpp:26:16:26:17 | v1 | Variable v1 is not used. |
| code2.cpp:42:11:42:16 | myVar1 | Variable myVar1 is not used. |
| code2.cpp:64:7:64:8 | v3 | Variable v3 is not used. |
| code2.cpp:108:11:108:12 | v2 | Variable v2 is not used. |
| code2.cpp:128:9:128:9 | b | Variable b is not used. |
| code.c:10:18:10:18 | y | Variable y is not used. |
| code.c:11:18:11:18 | z | Variable z is not used. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void test_range_based_for()
output(v1);
}

for (int v2 : myContainer) // $ Alert // BAD: v2 is not used
for (int v2 : myContainer) // GOOD: iterator dereference may have side effects

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the relevant member functions in MyContainer are missing function bodies, so we don't known if there may be side-effects.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would hope that iterating through a container doesn't have side-effects in the vast majority of cases. But this is C++, people are bound to have come up with containers where they do.

{
}
}
Expand Down Expand Up @@ -181,3 +181,15 @@ void test_maybe_unused()
{
[[maybe_unused]] int unused; // GOOD: explicitly marked as maybe_unused
}

int globalVar;

int initialize()
{
return ++globalVar;
}

void test_initializer_side_effect()
{
int initialized = initialize(); // GOOD (initializer has a side effect)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@ class MyTemplateClass // never instantiated
// ...
}
};

int initialize()
{
return ++globalVar;
}

static int initialized = initialize(); // GOOD (initializer has a side effect)

struct WithConstructor
{
WithConstructor() { ++globalVar; }
};

struct WithDestructor
{
~WithDestructor() { ++globalVar; }
};

static WithConstructor constructed; // GOOD (constructor has a side effect)
static WithDestructor destructed; // GOOD (destructor has a side effect)
Loading