adek05 updated this revision to Diff 45228. adek05 added a comment. @LegalizeAdulthood, @Eugene.Zelenko and @omtcyf0 comments
http://reviews.llvm.org/D16286 Files: clang-tidy/readability/CMakeLists.txt clang-tidy/readability/ReadabilityTidyModule.cpp clang-tidy/readability/ReturnWithRedundantParensCheck.cpp clang-tidy/readability/ReturnWithRedundantParensCheck.h test/clang-tidy/readability-return-with-redundant-parens.cpp
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 parentheses for expression in return statement [readability-return-with-redundant-parens] + // CHECK-FIXES: {{^ }}return 1;{{$}} +} + +int complex1() { + int a = 0; + return (a + a * (a + a)); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant parentheses for expression in return statement [readability-return-with-redundant-parens] + // CHECK-FIXES: {{^ }}return a + a * (a + a);{{$}} +} + +int no_space() { + return(1); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant parentheses for expression in return statement [readability-return-with-redundant-parens] + // CHECK-FIXES: {{^ }}return 1;{{$}} +} 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,40 @@ +//===--- 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 <algorithm> + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace readability { + +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<ParenExpr>("paren_expr"); + + const SourceLocation &LParenLoc = ParenExpression->getLParen(); + const SourceLocation &RParenLoc = ParenExpression->getRParen(); + diag(LParenLoc, "redundant parentheses for expression in return statement") + << FixItHint::CreateRemoval(LParenLoc) + << FixItHint::CreateRemoval(RParenLoc); +} + +} // namespace readability +} // namespace tidy +} // namespace clang 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<SimplifyBooleanExprCheck>( "readability-simplify-boolean-expr"); + CheckFactories.registerCheck<ReturnWithRedundantParensCheck>( + "readability-return-with-redundant-parens"); } }; 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
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits