#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
+#include "../utils/LexerUtils.h"
using namespace clang::ast_matchers;
auto Diag = diag(Location, "use '= default' to define a trivial " +
SpecialFunctionName);
- if (ApplyFix)
- Diag << FixItHint::CreateReplacement(Body->getSourceRange(), "= default;")
+ if (ApplyFix) {
+ // 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());
+ StringRef Replacement =
+ Token && Token->is(tok::semi) ? "= default" : "= default;";
+ Diag << FixItHint::CreateReplacement(Body->getSourceRange(), Replacement)
<< RemoveInitializers;
+ }
}
} // namespace modernize
return findNextAnyTokenKind(Start, SM, LangOpts, tok::comma, tok::semi);
}
+Optional<Token> findNextTokenSkippingComments(SourceLocation Start,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ Optional<Token> CurrentToken;
+ do {
+ CurrentToken = Lexer::findNextToken(Start, SM, LangOpts);
+ } while (CurrentToken && CurrentToken->is(tok::comment));
+ return CurrentToken;
+}
+
bool rangeContainsExpansionsOrDirectives(SourceRange Range,
const SourceManager &SM,
const LangOptions &LangOpts) {
}
}
+// Finds next token that's not a comment.
+Optional<Token> findNextTokenSkippingComments(SourceLocation Start,
+ const SourceManager &SM,
+ const LangOptions &LangOpts);
+
/// Re-lex the provide \p Range and return \c false if either a macro spans
/// multiple tokens, a pre-processor directive or failure to retrieve the
/// next token is found, otherwise \c true.
- The 'objc-avoid-spinlock' check was renamed to :doc:`darwin-avoid-spinlock
<clang-tidy/checks/darwin-avoid-spinlock>`
+- The :doc:`modernize-use-equals-default
+ <clang-tidy/checks/modernize-use-equals-default>` fix no longer adds
+ semicolons where they would be redundant.
+
- New :doc:`readability-redundant-access-specifiers
<clang-tidy/checks/readability-redundant-access-specifiers>` check.
struct BF {
BF() = default;
BF(const BF &Other) : Field1(Other.Field1), Field2(Other.Field2), Field3(Other.Field3),
- Field4(Other.Field4) {}
+ Field4(Other.Field4) {};
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use '= default'
// CHECK-FIXES: BF(const BF &Other) {{$}}
// CHECK-FIXES: = default;
~OL();
};
-OL::OL() {}
+OL::OL() {};
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use '= default' to define a trivial default constructor [modernize-use-equals-default]
// CHECK-FIXES: OL::OL() = default;
OL::~OL() {}
// Inline definitions.
class IL {
public:
- IL() {}
+ IL() {} ; // Note embedded tab on this line
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
- // CHECK-FIXES: IL() = default;
+ // CHECK-FIXES: IL() = default ; // Note embedded tab on this line
~IL() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: ~IL() = default;
// Default member initializer
class DMI {
public:
- DMI() {}
- // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
- // CHECK-FIXES: DMI() = default;
+ DMI() {} // Comment before semi-colon on next line
+ ;
+ // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use '= default'
+ // CHECK-FIXES: DMI() = default // Comment before semi-colon on next line
+ // CHECK-FIXES-NEXT: ;
int Field = 5;
};
// Class member
class CM {
public:
- CM() {}
+ CM() {} /* Comments */ /* before */ /* semicolon */;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
- // CHECK-FIXES: CM() = default;
+ // CHECK-FIXES: CM() = default /* Comments */ /* before */ /* semicolon */;
OL o;
};
Priv() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: Priv() = default;
- ~Priv() {}
+ ~Priv() {};
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: ~Priv() = default;
};