Fix #11126 FN: noExplicitConstructor with single default parameter (#4174)

This commit is contained in:
chrchr-github 2022-06-07 21:15:13 +02:00 committed by GitHub
parent b80d06b69e
commit 1d677c57a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 8 deletions

View File

@ -32,7 +32,7 @@ namespace Ui {
class HelpBrowser : public QTextBrowser { class HelpBrowser : public QTextBrowser {
public: public:
HelpBrowser(QWidget* parent = nullptr) : QTextBrowser(parent), mHelpEngine(nullptr) {} explicit HelpBrowser(QWidget* parent = nullptr) : QTextBrowser(parent), mHelpEngine(nullptr) {}
void setHelpEngine(QHelpEngine *helpEngine); void setHelpEngine(QHelpEngine *helpEngine);
QVariant loadResource(int type, const QUrl& name) override; QVariant loadResource(int type, const QUrl& name) override;
private: private:

View File

@ -130,7 +130,7 @@ struct Analyzer {
enum class Terminate { None, Bail, Escape, Modified, Inconclusive, Conditional }; enum class Terminate { None, Bail, Escape, Modified, Inconclusive, Conditional };
struct Result { struct Result {
Result(Action action = Action::None, Terminate terminate = Terminate::None) explicit Result(Action action = Action::None, Terminate terminate = Terminate::None)
: action(action), terminate(terminate) : action(action), terminate(terminate)
{} {}
Action action; Action action;

View File

@ -353,7 +353,8 @@ void CheckClass::checkExplicitConstructors()
continue; continue;
if (!func.isExplicit() && if (!func.isExplicit() &&
func.minArgCount() == 1 && func.argCount() > 0 && func.minArgCount() < 2 &&
func.argumentList.front().getTypeName() != "std::initializer_list" &&
func.type != Function::eCopyConstructor && func.type != Function::eCopyConstructor &&
func.type != Function::eMoveConstructor) { func.type != Function::eMoveConstructor) {
noExplicitConstructorError(func.tokenDef, scope->className, scope->type == Scope::eStruct); noExplicitConstructorError(func.tokenDef, scope->className, scope->type == Scope::eStruct);

View File

@ -49,7 +49,7 @@ public:
int type; int type;
int reallocedFromType = -1; int reallocedFromType = -1;
const Token * allocTok; const Token * allocTok;
AllocInfo(int type_ = 0, AllocStatus status_ = NOALLOC, const Token* allocTok_ = nullptr) : status(status_), type(type_), allocTok(allocTok_) {} explicit AllocInfo(int type_ = 0, AllocStatus status_ = NOALLOC, const Token* allocTok_ = nullptr) : status(status_), type(type_), allocTok(allocTok_) {}
bool managed() const { bool managed() const {
return status < 0; return status < 0;

View File

@ -68,7 +68,7 @@ struct ForwardTraversal {
} }
struct Branch { struct Branch {
Branch(Token* tok = nullptr) : endBlock(tok) {} explicit Branch(Token* tok = nullptr) : endBlock(tok) {}
Token* endBlock = nullptr; Token* endBlock = nullptr;
Analyzer::Action action = Analyzer::Action::None; Analyzer::Action action = Analyzer::Action::None;
bool check = false; bool check = false;
@ -857,12 +857,12 @@ Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const V
{ {
ForwardTraversal ft{a, settings}; ForwardTraversal ft{a, settings};
ft.updateRange(start, end); ft.updateRange(start, end);
return {ft.actions, ft.terminate}; return Analyzer::Result{ ft.actions, ft.terminate };
} }
Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr<Analyzer>& a, const Settings* settings) Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr<Analyzer>& a, const Settings* settings)
{ {
ForwardTraversal ft{a, settings}; ForwardTraversal ft{a, settings};
ft.updateRecursive(start); ft.updateRecursive(start);
return {ft.actions, ft.terminate}; return Analyzer::Result{ ft.actions, ft.terminate };
} }

View File

@ -107,7 +107,7 @@ public:
const Token * typeEnd; const Token * typeEnd;
MathLib::bigint sizeOf; MathLib::bigint sizeOf;
Type(const Token* classDef_ = nullptr, const Scope* classScope_ = nullptr, const Scope* enclosingScope_ = nullptr) : explicit Type(const Token* classDef_ = nullptr, const Scope* classScope_ = nullptr, const Scope* enclosingScope_ = nullptr) :
classDef(classDef_), classDef(classDef_),
classScope(classScope_), classScope(classScope_),
enclosingScope(enclosingScope_), enclosingScope(enclosingScope_),

View File

@ -480,6 +480,19 @@ private:
" explicit constexpr Baz(int) {}\n" " explicit constexpr Baz(int) {}\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkExplicitConstructors("class Token;\n" // #11126
"struct Branch {\n"
" Branch(Token* tok = nullptr) : endBlock(tok) {}\n"
" Token* endBlock = nullptr;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Struct 'Branch' has a constructor with 1 argument that is not explicit.\n", errout.str());
checkExplicitConstructors("struct S {\n"
" S(std::initializer_list<int> il) : v(il) {}\n"
" std::vector<int> v;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
} }
#define checkDuplInheritedMembers(code) checkDuplInheritedMembers_(code, __FILE__, __LINE__) #define checkDuplInheritedMembers(code) checkDuplInheritedMembers_(code, __FILE__, __LINE__)