[clang-tools-extra] be7d633 - Magic number checker shouldn't warn on user defined string literals
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 9 10:13:35 PST 2019
Author: Tibor Brunner
Date: 2019-12-09T13:13:26-05:00
New Revision: be7d633a6fa6ddae6b7f455f5f336555d088c62d
URL: https://github.com/llvm/llvm-project/commit/be7d633a6fa6ddae6b7f455f5f336555d088c62d
DIFF: https://github.com/llvm/llvm-project/commit/be7d633a6fa6ddae6b7f455f5f336555d088c62d.diff
LOG: Magic number checker shouldn't warn on user defined string literals
Fixes a false positive brought up by PR40633.
Added:
clang-tools-extra/test/clang-tidy/readability-magic-numbers-userliteral.cpp
Modified:
clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
index 6f6366cab6f6..9114deb5c736 100644
--- a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
@@ -122,10 +122,21 @@ bool MagicNumbersCheck::isConstant(const MatchFinder::MatchResult &Result,
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;
});
}
diff --git a/clang-tools-extra/test/clang-tidy/readability-magic-numbers-userliteral.cpp b/clang-tools-extra/test/clang-tidy/readability-magic-numbers-userliteral.cpp
new file mode 100644
index 000000000000..82c4d7b0afce
--- /dev/null
+++ b/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]
+}
More information about the cfe-commits
mailing list