Author: Mitchell Balan Date: 2019-11-15T16:16:07-05:00 New Revision: 50e99563fb0459f5160572eef3c4e6062b8ad3f2
URL: https://github.com/llvm/llvm-project/commit/50e99563fb0459f5160572eef3c4e6062b8ad3f2 DIFF: https://github.com/llvm/llvm-project/commit/50e99563fb0459f5160572eef3c4e6062b8ad3f2.diff LOG: [clang-tidy] modernize-use-override new option AllowOverrideAndFinal Summary: In addition to adding `override` wherever possible, clang-tidy's `modernize-use-override` nicely removes `virtual` when `override` or `final` is specified, and further removes override when final is specified. While this is great default behavior, when code needs to be compiled with gcc at high warning levels that include `gcc -Wsuggest-override` or `gcc -Werror=suggest-override`, clang-tidy's removal of the redundant `override` keyword causes gcc to emit a warning or error. This discrepancy / conflict has been noted by others including a comment on Stack Overflow and by Mozilla's Firefox developers. This patch adds an AllowOverrideAndFinal option defaulting to 0 - thus preserving current behavior - that when enabled allows both `override` and `final` to co-exist, while still fixing all other issues. The patch includes a test file verifying all combinations of virtual/override/final, and mentions the new option in the release notes. Reviewers: alexfh, djasper, JonasToth Patch by: poelmanc Subscribers: JonasToth, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D70165 Added: Modified: clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp index 9a9a3830201b..a140e17fabfd 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp @@ -46,23 +46,23 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) { // string bar(""); Finder->addMatcher( namedDecl( - varDecl(hasType(hasUnqualifiedDesugaredType(recordType( - hasDeclaration(cxxRecordDecl(hasName("basic_string")))))), - hasInitializer(expr(ignoringImplicit(anyOf( - EmptyStringCtorExpr, - EmptyStringCtorExprWithTemporaries))) - .bind("expr"))), - unless(parmVarDecl())) - .bind("decl"), + varDecl( + hasType(hasUnqualifiedDesugaredType(recordType( + hasDeclaration(cxxRecordDecl(hasName("basic_string")))))), + hasInitializer(expr(ignoringImplicit(anyOf( + EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries))))) + .bind("vardecl"), + unless(parmVarDecl())), this); } void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) { - const auto *CtorExpr = Result.Nodes.getNodeAs<Expr>("expr"); - const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl"); - diag(CtorExpr->getExprLoc(), "redundant string initialization") - << FixItHint::CreateReplacement(CtorExpr->getSourceRange(), - Decl->getName()); + const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl"); + // VarDecl's getSourceRange() spans 'string foo = ""' or 'string bar("")'. + // So start at getLocation() to span just 'foo = ""' or 'bar("")'. + SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc()); + diag(VDecl->getLocation(), "redundant string initialization") + << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName()); } } // namespace readability diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp index fe05845db663..0299519a508e 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp @@ -1,5 +1,4 @@ -// RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t -// FIXME: Fix the checker to work in C++17 mode. +// RUN: %check_clang_tidy %s readability-redundant-string-init %t namespace std { template <typename T> diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp index b88e4a0f4d62..76d1fe97e1b9 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp @@ -1,5 +1,4 @@ -// RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t -// FIXME: Fix the checker to work in C++17 mode. +// RUN: %check_clang_tidy %s readability-redundant-string-init %t namespace std { template <typename T> @@ -131,6 +130,11 @@ void k() { // CHECK-FIXES: std::string a, b, c; std::string d = "u", e = "u", f = "u"; + + std::string g = "u", h = "", i = "uuu", j = "", k; + // CHECK-MESSAGES: [[@LINE-1]]:24: warning: redundant string initialization + // CHECK-MESSAGES: [[@LINE-2]]:43: warning: redundant string initialization + // CHECK-FIXES: std::string g = "u", h, i = "uuu", j, k; } // These cases should not generate warnings. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits