Skip to content

Commit 415acb2

Browse files
committed
Revert "[clang-itdy] Simplify virtual near-miss check"
This reverts commit 9a4b574.
1 parent 0145759 commit 415acb2

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,10 @@ bool VirtualNearMissCheck::isOverriddenByDerivedClass(
216216

217217
void VirtualNearMissCheck::registerMatchers(MatchFinder *Finder) {
218218
Finder->addMatcher(
219-
cxxMethodDecl(unless(anyOf(isOverride(), cxxConstructorDecl(),
220-
cxxDestructorDecl(), cxxConversionDecl(),
221-
isStatic(), isOverloadedOperator())))
219+
cxxMethodDecl(
220+
unless(anyOf(isOverride(), isImplicit(), cxxConstructorDecl(),
221+
cxxDestructorDecl(), cxxConversionDecl(), isStatic(),
222+
isOverloadedOperator())))
222223
.bind("method"),
223224
this);
224225
}
@@ -233,15 +234,7 @@ void VirtualNearMissCheck::check(const MatchFinder::MatchResult &Result) {
233234
assert(DerivedRD);
234235

235236
for (const auto &BaseSpec : DerivedRD->bases()) {
236-
const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl();
237-
if (const auto *TST =
238-
dyn_cast<TemplateSpecializationType>(BaseSpec.getType())) {
239-
auto TN = TST->getTemplateName();
240-
BaseRD =
241-
dyn_cast<CXXRecordDecl>(TN.getAsTemplateDecl()->getTemplatedDecl());
242-
}
243-
244-
if (BaseRD) {
237+
if (const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl()) {
245238
for (const auto *BaseMD : BaseRD->methods()) {
246239
if (!isPossibleToBeOverridden(BaseMD))
247240
continue;
@@ -257,12 +250,16 @@ void VirtualNearMissCheck::check(const MatchFinder::MatchResult &Result) {
257250
auto Range = CharSourceRange::getTokenRange(
258251
SourceRange(DerivedMD->getLocation()));
259252

260-
diag(DerivedMD->getBeginLoc(),
261-
"method '%0' has a similar name and the same signature as "
262-
"virtual method '%1'; did you mean to override it?")
253+
bool ApplyFix = !BaseMD->isTemplateInstantiation() &&
254+
!DerivedMD->isTemplateInstantiation();
255+
auto Diag =
256+
diag(DerivedMD->getBeginLoc(),
257+
"method '%0' has a similar name and the same signature as "
258+
"virtual method '%1'; did you mean to override it?")
263259
<< DerivedMD->getQualifiedNameAsString()
264-
<< BaseMD->getQualifiedNameAsString()
265-
<< FixItHint::CreateReplacement(Range, BaseMD->getName());
260+
<< BaseMD->getQualifiedNameAsString();
261+
if (ApplyFix)
262+
Diag << FixItHint::CreateReplacement(Range, BaseMD->getName());
266263
}
267264
}
268265
}

clang-tools-extra/clang-tidy/bugprone/VirtualNearMissCheck.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ class VirtualNearMissCheck : public ClangTidyCheck {
3232
}
3333
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
3434
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
35-
llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
36-
return TK_IgnoreUnlessSpelledInSource;
37-
}
3835

3936
private:
4037
/// Check if the given method is possible to be overridden by some other

clang-tools-extra/test/clang-tidy/checkers/bugprone-virtual-near-miss.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ template <typename T>
4444
struct TDerived : TBase<T> {
4545
virtual void tfunk(T t);
4646
// Should not apply fix for template.
47-
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: method 'TDerived::tfunk' has {{.*}} 'TBase::tfunc'
48-
// CHECK-FIXES: virtual void tfunc(T t);
47+
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: method 'TDerived<double>::tfunk' has {{.*}} 'TBase<double>::tfunc'
48+
// CHECK-MESSAGES: :[[@LINE-3]]:3: warning: method 'TDerived<int>::tfunk' has {{.*}} 'TBase<int>::tfunc'
49+
// CHECK-FIXES: virtual void tfunk(T t);
4950
};
5051

5152
TDerived<int> T1;

0 commit comments

Comments
 (0)