[PATCH] D71686: Fix false positive in magic number checker
Florin Iucha via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 20 20:10:27 PST 2019
0x8000-0000 updated this revision to Diff 234998.
0x8000-0000 added a comment.
Add a test file for expected failures
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D71686/new/
https://reviews.llvm.org/D71686
Files:
clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers-todo.cpp
clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp
@@ -59,7 +59,7 @@
const int anotherConstant;
};
-int ValueArray[] = {3, 5};
+int ValueArray[] = {3, 5, 0, 0, 0};
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
@@ -215,3 +215,14 @@
return Total;
}
+
+// prove that using enumerations values don't produce warnings
+enum class Letter : unsigned {
+ A, B, C, D, E, F, G, H, I, J
+};
+
+template<Letter x> struct holder { Letter letter = x; };
+template<Letter x> struct wrapper { using h_type = holder<x>; };
+
+template struct wrapper<Letter::A>;
+template struct wrapper<Letter::J>;
Index: clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers-todo.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers-todo.cpp
@@ -0,0 +1,16 @@
+// RUN: %check_clang_tidy %s readability-magic-numbers %t
+// RUN: --
+// XFAIL: *
+
+int ProcessSomething(int input);
+
+int DoWork()
+{
+ if (((int)4) > ProcessSomething(10))
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
+ return 0;
+
+ return 0;
+}
+
+
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -185,6 +185,10 @@
The check now supports the ``IgnoreBitFieldsWidths`` option to suppress
the warning for numbers used to specify bit field widths.
+ The check was updated to eliminate some false positives (such as using
+ class enumeration as non-type template parameters, or the synthetically
+ computed lengh of a static user string literal.)
+
- New :doc:`readability-make-member-function-const
<clang-tidy/checks/readability-make-member-function-const>` check.
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
@@ -34,7 +34,7 @@
return AsDecl->isImplicit();
}
- if (Node.get<EnumConstantDecl>() != nullptr)
+ if (Node.get<EnumConstantDecl>())
return true;
return llvm::any_of(Result.Context->getParents(Node),
@@ -125,8 +125,20 @@
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.
+ // Ignore this instance, because this matches an
+ // expanded class enumeration value.
+ if (Parent.get<CStyleCastExpr>() &&
+ llvm::any_of(
+ Result.Context->getParents(Parent),
+ [](const DynTypedNode &GrandParent) {
+ return GrandParent.get<SubstNonTypeTemplateParmExpr>() !=
+ nullptr;
+ }))
+ 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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71686.234998.patch
Type: text/x-patch
Size: 3828 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191221/39ae2279/attachment.bin>
More information about the cfe-commits
mailing list