[PATCH] D136399: [clang-tidy][modernize-use-equals-default] Avoid adding unnecessary semicolon
Alexander Shaposhnikov via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 20 17:19:40 PDT 2022
alexander-shaposhnikov created this revision.
alexander-shaposhnikov added reviewers: gribozavr2, ymandel, alexfh, LegalizeAdulthood.
alexander-shaposhnikov created this object with visibility "All Users".
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
alexander-shaposhnikov requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
Adjust the automatic to avoid adding superfluous semicolon.
Test plan: ninja check-all
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D136399
Files:
clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
@@ -91,7 +91,7 @@
// Private constructor/destructor.
class Priv {
Priv();
- ~Priv() {};
+ ~Priv() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: ~Priv() = default;
};
@@ -100,14 +100,28 @@
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use '= default'
// CHECK-FIXES: Priv::Priv() = default;
+struct SemiColon {
+ SemiColon() {};
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
+ // CHECK-FIXES: SemiColon() = default;{{$}}
+};
+
+struct SemiColonOutOfLine {
+ SemiColonOutOfLine();
+};
+
+SemiColonOutOfLine::SemiColonOutOfLine() {};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use '= default'
+// CHECK-FIXES: SemiColonOutOfLine::SemiColonOutOfLine() = default;{{$}}
+
// struct.
struct ST {
ST() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
- // CHECK-FIXES: ST() = default;
+ // CHECK-FIXES: ST() = default;{{$}}
~ST() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
- // CHECK-FIXES: ST() = default;
+ // CHECK-FIXES: ST() = default;{{$}}
};
// Deleted constructor/destructor.
@@ -200,7 +214,13 @@
DC() : KW() {}
~DC() override {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
- // CHECK-FIXES: ~DC() override = default;
+ // CHECK-FIXES: ~DC() override = default;{{$}}
+};
+
+struct OverrideWithSemiColon : KW {
+ ~OverrideWithSemiColon() override {};
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
+ // CHECK-FIXES: ~OverrideWithSemiColon() override = default;{{$}}
};
struct Comments {
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -159,6 +159,7 @@
with empty body is not equivalent to the explicitly defaulted one, variadic constructors
since they cannot be explicitly defaulted. The check also skips copy assignment operators
with nonstandard return types, private/protected default constructors for C++17 or earlier.
+ The automatic fixit has been adjusted to avoid adding superfluous semicolon.
The check is restricted to C++11 or later.
- Change the default behavior of :doc:`readability-avoid-const-params-in-decls
Index: clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
@@ -337,10 +337,13 @@
Diag << MemberType;
if (ApplyFix) {
+ SourceLocation UnifiedEnd = utils::lexer::getUnifiedEndLoc(
+ *Body, Result.Context->getSourceManager(),
+ Result.Context->getLangOpts());
// Skipping comments, check for a semicolon after Body->getSourceRange()
Optional<Token> Token = utils::lexer::findNextTokenSkippingComments(
- Body->getSourceRange().getEnd().getLocWithOffset(1),
- Result.Context->getSourceManager(), Result.Context->getLangOpts());
+ UnifiedEnd, Result.Context->getSourceManager(),
+ Result.Context->getLangOpts());
StringRef Replacement =
Token && Token->is(tok::semi) ? "= default" : "= default;";
Diag << FixItHint::CreateReplacement(Body->getSourceRange(), Replacement)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136399.469421.patch
Type: text/x-patch
Size: 3653 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221021/4074ab73/attachment.bin>
More information about the cfe-commits
mailing list