[PATCH] D67265: [clang-tidy] Magic number checker shouldn't warn on user defined string literals
Tibor Brunner via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 6 07:16:28 PDT 2019
bruntib updated this revision to Diff 219097.
bruntib added a comment.
Usually I'm not a big fan of `auto`, but in this case it makes sense not to duplicate the type name in the same expression. I'll use this rule of thumb in the future, thanks :)
Repository:
rCTE Clang Tools Extra
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D67265/new/
https://reviews.llvm.org/D67265
Files:
clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
clang-tools-extra/test/clang-tidy/readability-magic-numbers-userliteral.cpp
Index: clang-tools-extra/test/clang-tidy/readability-magic-numbers-userliteral.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/readability-magic-numbers-userliteral.cpp
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy -std=c++14-or-later %s readability-magic-numbers %t --
+
+namespace std {
+ class string {};
+ using size_t = decltype(sizeof(int));
+ string operator ""s(const char *, std::size_t);
+ int operator "" s(unsigned long long);
+}
+
+void UserDefinedLiteral() {
+ using std::operator ""s;
+ "Hello World"s;
+ const int i = 3600s;
+ int j = 3600s;
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3600s is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+}
Index: clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
@@ -112,10 +112,21 @@
return llvm::any_of(
Result.Context->getParents(ExprResult),
[&Result](const DynTypedNode &Parent) {
- return isUsedToInitializeAConstant(Result, Parent) ||
- // Ignore this instance, because this match reports the location
- // where the template is defined, not where it is instantiated.
- Parent.get<SubstNonTypeTemplateParmExpr>();
+ if (isUsedToInitializeAConstant(Result, Parent))
+ return true;
+
+ // Ignore this instance, because this match reports the location
+ // where the template is defined, not where it is instantiated.
+ if (Parent.get<SubstNonTypeTemplateParmExpr>())
+ return true;
+
+ // Don't warn on string user defined literals:
+ // std::string s = "Hello World"s;
+ if (const auto *UDL = Parent.get<UserDefinedLiteral>())
+ if (UDL->getLiteralOperatorKind() == UserDefinedLiteral::LOK_String)
+ return true;
+
+ return false;
});
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67265.219097.patch
Type: text/x-patch
Size: 2110 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190906/ab8ab9d9/attachment-0001.bin>
More information about the cfe-commits
mailing list