Skip to content

Commit 50e9956

Browse files
author
Mitchell Balan
committed
[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
1 parent ee0882b commit 50e9956

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
4646
// string bar("");
4747
Finder->addMatcher(
4848
namedDecl(
49-
varDecl(hasType(hasUnqualifiedDesugaredType(recordType(
50-
hasDeclaration(cxxRecordDecl(hasName("basic_string")))))),
51-
hasInitializer(expr(ignoringImplicit(anyOf(
52-
EmptyStringCtorExpr,
53-
EmptyStringCtorExprWithTemporaries)))
54-
.bind("expr"))),
55-
unless(parmVarDecl()))
56-
.bind("decl"),
49+
varDecl(
50+
hasType(hasUnqualifiedDesugaredType(recordType(
51+
hasDeclaration(cxxRecordDecl(hasName("basic_string")))))),
52+
hasInitializer(expr(ignoringImplicit(anyOf(
53+
EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries)))))
54+
.bind("vardecl"),
55+
unless(parmVarDecl())),
5756
this);
5857
}
5958

6059
void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) {
61-
const auto *CtorExpr = Result.Nodes.getNodeAs<Expr>("expr");
62-
const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl");
63-
diag(CtorExpr->getExprLoc(), "redundant string initialization")
64-
<< FixItHint::CreateReplacement(CtorExpr->getSourceRange(),
65-
Decl->getName());
60+
const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl");
61+
// VarDecl's getSourceRange() spans 'string foo = ""' or 'string bar("")'.
62+
// So start at getLocation() to span just 'foo = ""' or 'bar("")'.
63+
SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
64+
diag(VDecl->getLocation(), "redundant string initialization")
65+
<< FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
6666
}
6767

6868
} // namespace readability

clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t
2-
// FIXME: Fix the checker to work in C++17 mode.
1+
// RUN: %check_clang_tidy %s readability-redundant-string-init %t
32

43
namespace std {
54
template <typename T>

clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t
2-
// FIXME: Fix the checker to work in C++17 mode.
1+
// RUN: %check_clang_tidy %s readability-redundant-string-init %t
32

43
namespace std {
54
template <typename T>
@@ -131,6 +130,11 @@ void k() {
131130
// CHECK-FIXES: std::string a, b, c;
132131

133132
std::string d = "u", e = "u", f = "u";
133+
134+
std::string g = "u", h = "", i = "uuu", j = "", k;
135+
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: redundant string initialization
136+
// CHECK-MESSAGES: [[@LINE-2]]:43: warning: redundant string initialization
137+
// CHECK-FIXES: std::string g = "u", h, i = "uuu", j, k;
134138
}
135139

136140
// These cases should not generate warnings.

0 commit comments

Comments
 (0)