void write_ref(int &ref) { ref = 1; } class MyClass { public: MyClass(); ~MyClass(); private: int val; }; MyClass :: MyClass() { int a, b, c, d, e; // BAD: 'e' is unused int &f = d; write_ref(a); val = b + f; throw c; } MyClass :: ~MyClass() { } void test() { MyClass mc; // GOOD: constructor and destructor may have side-effects MyClass *mc_ptr; // BAD: 'mc_ptr' is unused MyClass &mc_ref = mc; // BAD: 'mc_ref' is unused } // --- template class container { public: T t; }; // static int variable in non-instantiated template function template void *templateFunction() { static int my_static; // GOOD static void* my_ptr = &my_static; return my_ptr; } // static template parameter variable in non-instantiated template function template void *templateFunction2() { static T my_static; // GOOD static void* my_ptr = &my_static; return my_ptr; } // static template derived variable in non-instantiated template function template void *templateFunction3() { static container *my_static; // GOOD static void* my_ptr = &my_static; return my_ptr; } // static unused int variable in non-instantiated template function template void *templateFunction4() { static int my_static; // BAD static void* my_ptr = 0; return my_ptr; } // static int variable in twice instantiated template function template void *instantiatedTemplateFunction() { static int my_static; // GOOD static void* my_ptr = &my_static; return my_ptr; } // static template parameter variable in twice instantiated template function template void *instantiatedTemplateFunction2() { static T my_static; // GOOD static void* my_ptr = &my_static; return my_ptr; } // static template derived variable in twice instantiated template function template void *instantiatedTemplateFunction3() { static container *my_static; // GOOD static void* my_ptr = &my_static; return my_ptr; } // static unused int variable in twice instantiated template function template void *instantiatedTemplateFunction4() { static int my_static; // BAD static void* my_ptr = 0; return my_ptr; } void caller() { instantiatedTemplateFunction(); instantiatedTemplateFunction(); instantiatedTemplateFunction2(); instantiatedTemplateFunction2(); instantiatedTemplateFunction3(); instantiatedTemplateFunction3(); instantiatedTemplateFunction4(); instantiatedTemplateFunction4(); } // This is a non-template version of the above. void *nonTemplateFunction() { static int *my_static; // GOOD static void* my_ptr = &my_static; return my_ptr; } // This is a non-template version of the above. void *nonTemplateFunction2() { static int *my_static; // BAD static void* my_ptr = 0; return my_ptr; } // non-static int variable in non-instantiated template function template void *templateFunction5() { int my_local; // GOOD void* my_ptr = &my_local; return my_ptr; } // non-static template parameter variable in non-instantiated template function template void *templateFunction6() { T my_local; // GOOD void* my_ptr = &my_local; return my_ptr; } // non-static template derived variable in non-instantiated template function template void *templateFunction7() { container *my_local; // GOOD void* my_ptr = &my_local; return my_ptr; } // non-static unused int variable in non-instantiated template function template void *templateFunction8() { int my_local; // BAD void* my_ptr = 0; return my_ptr; } template class templateClass { public: // static int variable in class template method void *templateClassMethod() { static int my_static; // GOOD void* my_ptr = &my_static; return my_ptr; } // static template parameter variable in class template method void *templateClassMethod2() { static T my_static; // GOOD void* my_ptr = &my_static; return my_ptr; } // static template derived variable in class template method void *templateClassMethod3() { static container *my_static; // GOOD void* my_ptr = &my_static; return my_ptr; } // static unused int variable in class template method void *templateClassMethod4() { static int my_static; // BAD void* my_ptr = 0; return my_ptr; } }; templateClass tc_i; template class MyTemplateClass2 { public: void method() { static T *a; // BAD static T b; // GOOD - T could have a constructor / destructor static container *c; // BAD static container d; // BAD [NOT DETECTED - due to type container depending on type container, which *could* have a constructor, though as used here it can't] static container e; // GOOD - T could have a constructor / destructor } }; // --- int myGlobal; class MyMethodClass { public: void MyMethod() {myGlobal++;} // side-effect }; class MyConstructorClass { public: MyConstructorClass() {myGlobal++;} // side-effect }; class MyDerivedClass : public MyConstructorClass { }; class MyContainingClass { private: MyConstructorClass mcc; }; void testFunction() { MyMethodClass mmc; // BAD: unused MyConstructorClass mcc; // GOOD MyDerivedClass mdc; // GOOD MyContainingClass mcc2; // GOOD }