Kaydet (Commit) 4de9091c authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:flatten loosen condition

the description in the comment was right, but the code was not

Change-Id: I7c038e7453f4387d33ec6423c0c55446d6d0df47
Reviewed-on: https://gerrit.libreoffice.org/44680Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 9050854c
...@@ -45,7 +45,7 @@ private: ...@@ -45,7 +45,7 @@ private:
std::string invertCondition(Expr const * condExpr, SourceRange conditionRange); std::string invertCondition(Expr const * condExpr, SourceRange conditionRange);
bool checkOverlap(SourceRange); bool checkOverlap(SourceRange);
std::stack<bool> mLastStmtInParentStack; Stmt const * lastStmtInCompoundStmt = nullptr;
std::vector<std::pair<char const *, char const *>> mvModifiedRanges; std::vector<std::pair<char const *, char const *>> mvModifiedRanges;
Stmt const * mElseBranch = nullptr; Stmt const * mElseBranch = nullptr;
}; };
...@@ -107,9 +107,16 @@ bool Flatten::TraverseCompoundStmt(CompoundStmt * compoundStmt) ...@@ -107,9 +107,16 @@ bool Flatten::TraverseCompoundStmt(CompoundStmt * compoundStmt)
// var decls in its then block, we cannot de-indent the then block without // var decls in its then block, we cannot de-indent the then block without
// extending the lifetime of some variables, which may be problematic // extending the lifetime of some variables, which may be problematic
// ignore if we are part of an if/then/else/if chain // ignore if we are part of an if/then/else/if chain
mLastStmtInParentStack.push(compoundStmt->size() > 0 && isa<IfStmt>(*compoundStmt->body_back())); auto copy = lastStmtInCompoundStmt;
if (compoundStmt->size() > 0)
lastStmtInCompoundStmt = compoundStmt->body_back();
else
lastStmtInCompoundStmt = nullptr;
bool rv = RecursiveASTVisitor<Flatten>::TraverseCompoundStmt(compoundStmt); bool rv = RecursiveASTVisitor<Flatten>::TraverseCompoundStmt(compoundStmt);
mLastStmtInParentStack.pop();
lastStmtInCompoundStmt = copy;
return rv;
return rv; return rv;
} }
...@@ -142,7 +149,7 @@ bool Flatten::VisitIfStmt(IfStmt const * ifStmt) ...@@ -142,7 +149,7 @@ bool Flatten::VisitIfStmt(IfStmt const * ifStmt)
// if the "if" statement is not the last statement in its block, and it contains // if the "if" statement is not the last statement in its block, and it contains
// var decls in its then block, we cannot de-indent the then block without // var decls in its then block, we cannot de-indent the then block without
// extending the lifetime of some variables, which may be problematic // extending the lifetime of some variables, which may be problematic
if (!mLastStmtInParentStack.top() || containsVarDecl(ifStmt->getThen())) if (ifStmt != lastStmtInCompoundStmt && containsVarDecl(ifStmt->getThen()))
return true; return true;
if (!rewrite1(ifStmt)) if (!rewrite1(ifStmt))
...@@ -164,7 +171,7 @@ bool Flatten::VisitIfStmt(IfStmt const * ifStmt) ...@@ -164,7 +171,7 @@ bool Flatten::VisitIfStmt(IfStmt const * ifStmt)
// if the "if" statement is not the last statement in it's block, and it contains // if the "if" statement is not the last statement in it's block, and it contains
// var decls in it's else block, we cannot de-indent the else block without // var decls in it's else block, we cannot de-indent the else block without
// extending the lifetime of some variables, which may be problematic // extending the lifetime of some variables, which may be problematic
if (!mLastStmtInParentStack.top() || containsVarDecl(ifStmt->getElse())) if (ifStmt != lastStmtInCompoundStmt && containsVarDecl(ifStmt->getElse()))
return true; return true;
if (!rewrite2(ifStmt)) if (!rewrite2(ifStmt))
......
...@@ -19,12 +19,17 @@ void top1() { ...@@ -19,12 +19,17 @@ void top1() {
} else { } else {
throw std::exception(); // expected-error {{unconditional throw in else branch, rather invert the condition, throw early, and flatten the normal case [loplugin:flatten]}} throw std::exception(); // expected-error {{unconditional throw in else branch, rather invert the condition, throw early, and flatten the normal case [loplugin:flatten]}}
} }
// no warning expected
if (foo() == 1) { if (foo() == 1) {
Class aClass; Class aClass;
(void)aClass; (void)aClass;
} else { } else {
throw std::exception(); throw std::exception(); // no warning expected
}
if (foo() == 1) { // expected-note {{if condition here [loplugin:flatten]}}
Class aClass;
(void)aClass;
} else {
throw std::exception(); // expected-error {{unconditional throw in else branch, rather invert the condition, throw early, and flatten the normal case [loplugin:flatten]}}
} }
} }
...@@ -34,9 +39,14 @@ void top2() { ...@@ -34,9 +39,14 @@ void top2() {
} else { } else {
foo(); foo();
} }
// no warning expected
if (foo() == 2) { if (foo() == 2) {
throw std::exception(); throw std::exception(); // no warning expected
} else {
Class aClass;
(void)aClass;
}
if (foo() == 2) {
throw std::exception(); // expected-error {{unconditional throw in then branch, just flatten the else [loplugin:flatten]}}
} else { } else {
Class aClass; Class aClass;
(void)aClass; (void)aClass;
......
...@@ -74,10 +74,7 @@ OString getElement(OString const & docPath, ...@@ -74,10 +74,7 @@ OString getElement(OString const & docPath,
JFW_E_ERROR, JFW_E_ERROR,
"[Java framework] Error in function getElement (elements.cxx)"); "[Java framework] Error in function getElement (elements.cxx)");
} }
else sValue = reinterpret_cast<sal_Char*>(pathObj->nodesetval->nodeTab[0]->content);
{
sValue = reinterpret_cast<sal_Char*>(pathObj->nodesetval->nodeTab[0]->content);
}
return sValue; return sValue;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment