diff options
| author | Mitchell Balan <mitchell@stellarscience.com> | 2019-11-15 16:13:48 -0500 |
|---|---|---|
| committer | Mitchell Balan <mitchell@stellarscience.com> | 2019-11-15 16:16:07 -0500 |
| commit | 50e99563fb0459f5160572eef3c4e6062b8ad3f2 (patch) | |
| tree | 18783c7823e202be79f8af34c453e33a97d04905 /clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp | |
| parent | ee0882bdf866ad8877dfda3820a822c851d0733a (diff) | |
| download | bcm5719-llvm-50e99563fb0459f5160572eef3c4e6062b8ad3f2.tar.gz bcm5719-llvm-50e99563fb0459f5160572eef3c4e6062b8ad3f2.zip | |
[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
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp index 9a9a3830201..a140e17fabf 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 |

