Index: clang-tidy/readability/CMakeLists.txt =================================================================== --- clang-tidy/readability/CMakeLists.txt +++ clang-tidy/readability/CMakeLists.txt @@ -13,6 +13,7 @@ ReadabilityTidyModule.cpp RedundantStringCStrCheck.cpp RedundantSmartptrGetCheck.cpp + ReturnWithRedundantParensCheck.cpp SimplifyBooleanExprCheck.cpp UniqueptrDeleteReleaseCheck.cpp Index: clang-tidy/readability/ReadabilityTidyModule.cpp =================================================================== --- clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tidy/readability/ReadabilityTidyModule.cpp @@ -20,6 +20,7 @@ #include "NamedParameterCheck.h" #include "RedundantSmartptrGetCheck.h" #include "RedundantStringCStrCheck.h" +#include "ReturnWithRedundantParensCheck.h" #include "SimplifyBooleanExprCheck.h" #include "UniqueptrDeleteReleaseCheck.h" @@ -54,6 +55,8 @@ "readability-redundant-string-cstr"); CheckFactories.registerCheck( "readability-simplify-boolean-expr"); + CheckFactories.registerCheck( + "readability-return-with-redundant-parens"); } }; Index: clang-tidy/readability/ReturnWithRedundantParensCheck.h =================================================================== --- /dev/null +++ clang-tidy/readability/ReturnWithRedundantParensCheck.h @@ -0,0 +1,38 @@ +//===--- ReturnWithRedundantParensCheck.h - clang-tidy ----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_RETURNBRACKETSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_RETURNBRACKETSCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace readability { + +/// Find and remove redundant parenthesis surrounding returned expression. +/// +/// Examples: +/// \code +/// void f() { return (1); } ==> void f() { return 1; } +/// \endcode +class ReturnWithRedundantParensCheck : public ClangTidyCheck { +public: + ReturnWithRedundantParensCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace readability +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_RETURNBRACKETSCHECK_H Index: clang-tidy/readability/ReturnWithRedundantParensCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/readability/ReturnWithRedundantParensCheck.cpp @@ -0,0 +1,56 @@ +//===--- ReturnWithRedundantParensCheck.cpp - clang-tidy --------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ReturnWithRedundantParensCheck.h" + +#include + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace readability { + +namespace { + +// Ad-hoc measure of complexity of the expression. +bool isComplexExpression(const Expr *Expr) { + const int ChildrenCountThreshold = 1; + auto ChildrenNum = std::distance(Expr->child_begin(), Expr->child_end()); + return ChildrenNum > ChildrenCountThreshold; +} +} + +void ReturnWithRedundantParensCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(returnStmt(hasDescendant(parenExpr().bind("paren_expr"))), + this); +} + +void ReturnWithRedundantParensCheck::check( + const ast_matchers::MatchFinder::MatchResult &Result) { + + const ParenExpr *ParenExpression = + Result.Nodes.getNodeAs("paren_expr"); + + // Complex expression can be surrounded by parenthesis for readability + if (isComplexExpression(ParenExpression->getSubExpr())) { + return; + } + + const SourceLocation &LParenLoc = ParenExpression->getLParen(); + const SourceLocation &RParenLoc = ParenExpression->getRParen(); + diag(LParenLoc, "redundant '(' for expression in return statement") + << FixItHint::CreateReplacement(LParenLoc, ""); + diag(RParenLoc, "redundant ')' for expression in return statement") + << FixItHint::CreateReplacement(RParenLoc, ""); +} + +} // namespace readability +} // namespace tidy +} // namespace clang Index: test/clang-tidy/readability-return-with-redundant-parens.cpp =================================================================== --- /dev/null +++ test/clang-tidy/readability-return-with-redundant-parens.cpp @@ -0,0 +1,24 @@ +// RUN: %check_clang_tidy %s readability-return-with-redundant-parens %t + +int no_parens() { + return 1; +} + +int simple1() { + return (1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant '(' for expression in return statement [readability-return-with-redundant-parens] + // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: redundant ')' for expression in return statement [readability-return-with-redundant-parens] + // CHECK-FIXES: {{^ }}return 1;{{$}} +} + +int simple2() { + int a = 0; + return (a); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant '(' for expression in return statement [readability-return-with-redundant-parens] + // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: redundant ')' for expression in return statement [readability-return-with-redundant-parens] + // CHECK-FIXES: {{^ }}return a;{{$}} +} + +int complex1() { + return (1 + 2); +}