Removing check hidingInheritedPublic (#2482)

This commit is contained in:
Maksim Derbasov 2020-01-09 08:53:08 +03:00 committed by Daniel Marjamäki
parent 4e8a922e18
commit 872d531568
3 changed files with 1 additions and 65 deletions

View File

@ -2625,36 +2625,6 @@ void CheckClass::overrideError(const Function *funcInBase, const Function *funcI
false);
}
void CheckClass::checkAccessModifierVirtualFunctions()
{
// Motivation:
// isocpp.org/wiki/faq/proper-inheritance#hiding-inherited-public
// stackoverflow.com/questions/484592/overriding-public-virtual-functions-with-private-functions-in-c
if (!mSettings->isEnabled(Settings::STYLE))
return;
for (const Scope * classScope : mSymbolDatabase->classAndStructScopes) {
if (!classScope->definedType || classScope->definedType->derivedFrom.empty())
continue;
for (const Function &func : classScope->functionList) {
const Function *baseFunc = func.getOverriddenFunction();
if (baseFunc) {
if (AccessControl::Public == baseFunc->access && AccessControl::Public != func.access) {
checkAccessModifierVirtualFunctionsError(func.tokenDef, func.name());
}
}
}
}
}
void CheckClass::checkAccessModifierVirtualFunctionsError(const Token *tok, const std::string& func)
{
reportError(tok, Severity::style, "hidingInheritedPublic",
"$symbol:" + func + "\n"
"The virtual function '$symbol' is a public in a base class and became not-public in derived. It's violate a substitutability a principle in OOP.",
CWE(0U) /* Unknown CWE! */,
false);
}
void CheckClass::checkUnsafeClassRefMember()
{
if (!mSettings->safeChecks.classes || !mSettings->isEnabled(Settings::WARNING))

View File

@ -79,7 +79,6 @@ public:
checkClass.checkCopyCtorAndEqOperator();
checkClass.checkOverride();
checkClass.checkUnsafeClassRefMember();
checkClass.checkAccessModifierVirtualFunctions();
}
/** @brief %Check that all class constructors are ok */
@ -147,8 +146,6 @@ public:
/** @brief Unsafe class check - const reference member */
void checkUnsafeClassRefMember();
/** @brief Check that virtuial function has not least access in derived class */
void checkAccessModifierVirtualFunctions();
private:
const SymbolDatabase *mSymbolDatabase;
@ -186,7 +183,6 @@ private:
void copyCtorAndEqOperatorError(const Token *tok, const std::string &classname, bool isStruct, bool hasCopyCtor);
void overrideError(const Function *funcInBase, const Function *funcInDerived);
void unsafeClassRefMemberError(const Token *tok, const std::string &varname);
void checkAccessModifierVirtualFunctionsError(const Token *tok, const std::string& func);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const OVERRIDE {
CheckClass c(nullptr, settings, errorLogger);
@ -224,7 +220,6 @@ private:
c.virtualFunctionCallInConstructorError(nullptr, std::list<const Token *>(), "f");
c.overrideError(nullptr, nullptr);
c.unsafeClassRefMemberError(nullptr, "UnsafeClass::var");
c.checkAccessModifierVirtualFunctionsError(nullptr, "f");
}
static std::string myName() {
@ -253,8 +248,7 @@ private:
"- Duplicated inherited data members\n"
// disabled for now "- If 'copy constructor' defined, 'operator=' also should be defined and vice versa\n"
"- Check that arbitrary usage of public interface does not result in division by zero\n"
"- Check that the 'override' keyword is used when overriding virtual functions\n"
"- Check that virtual public function not became private in derived class\n";
"- Check that the 'override' keyword is used when overriding virtual functions\n";
}
// operatorEqRetRefThis helper functions

View File

@ -221,8 +221,6 @@ private:
TEST_CASE(overrideCVRefQualifiers);
TEST_CASE(unsafeClassRefMember);
TEST_CASE(accessModifierVirtualFunctions);
}
void checkCopyCtorAndEqOperator(const char code[]) {
@ -7148,32 +7146,6 @@ private:
ASSERT_EQUALS("", errout.str());
}
void checkAccessModifierVirtualFunctions(const char code[]) {
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("style");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
checkClass.checkAccessModifierVirtualFunctions();
}
void accessModifierVirtualFunctions() {
checkAccessModifierVirtualFunctions("struct Base { virtual void f(); };\n"
"struct Derived : Base { private: virtual void f(); };");
ASSERT_EQUALS("[test.cpp:2]: (style) The virtual function 'f' is a public in a base class and became not-public in derived. It's violate a substitutability a principle in OOP.\n", errout.str());
checkAccessModifierVirtualFunctions("struct Base { virtual void f(); };\n"
"struct Derived : Base { virtual void f(); };");
ASSERT_EQUALS("", errout.str());
}
void checkUnsafeClassRefMember(const char code[]) {
// Clear the error log
errout.str("");