From eea6d3951b66f85df60574e10f19a81bfd7529cc Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 17 Aug 2017 17:21:53 +0200 Subject: loplugin:unnecessaryparen look for statements like return (function()); Change-Id: I906cf2183489f87225b99b987caca67e39b26cc3 Reviewed-on: https://gerrit.libreoffice.org/41260 Reviewed-by: Noel Grandin Tested-by: Noel Grandin --- compilerplugins/clang/unnecessaryparen.cxx | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/unnecessaryparen.cxx b/compilerplugins/clang/unnecessaryparen.cxx index 4abfd69e3b7c..1e01c41d5893 100644 --- a/compilerplugins/clang/unnecessaryparen.cxx +++ b/compilerplugins/clang/unnecessaryparen.cxx @@ -55,6 +55,7 @@ public: bool VisitDoStmt(const DoStmt *); bool VisitWhileStmt(const WhileStmt *); bool VisitSwitchStmt(const SwitchStmt *); + bool VisitReturnStmt(const ReturnStmt* ); bool VisitCallExpr(const CallExpr *); bool TraverseUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *); bool TraverseCaseStmt(CaseStmt *); @@ -162,6 +163,37 @@ bool UnnecessaryParen::VisitSwitchStmt(const SwitchStmt* switchStmt) return true; } +bool UnnecessaryParen::VisitReturnStmt(const ReturnStmt* returnStmt) +{ + if (ignoreLocation(returnStmt)) + return true; + if (returnStmt->getLocStart().isMacroID()) + return true; + + if (!returnStmt->getRetValue()) + return true; + auto parenExpr = dyn_cast(returnStmt->getRetValue()->IgnoreImpCasts()); + if (!parenExpr) + return true; + if (parenExpr->getLocStart().isMacroID()) + return true; + // assignments need extra parentheses or they generate a compiler warning + auto binaryOp = dyn_cast(parenExpr->getSubExpr()); + if (binaryOp && binaryOp->getOpcode() == BO_Assign) + return true; + + // only non-operator-calls for now + auto subExpr = parenExpr->getSubExpr(); + if (isa(subExpr) && !isa(subExpr)) + { + report( + DiagnosticsEngine::Warning, "parentheses immediately inside return statement", + parenExpr->getLocStart()) + << parenExpr->getSourceRange(); + } + return true; +} + void UnnecessaryParen::VisitSomeStmt(const Stmt *parent, const Expr* cond, StringRef stmtName) { if (ignoreLocation(parent)) -- cgit