Skip to content

Commit 87e4bb2

Browse files
committed
fix literal in template problem in exchange for not resolving implicit casts
1 parent 2e0318f commit 87e4bb2

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,14 @@ void UseStdNumbersCheck::registerMatchers(MatchFinder *const Finder) {
315315
Matches.matchPhi(),
316316
};
317317

318-
// Using 'TK_IgnoreUnlessSpelledInSource' here instead of at the check level
319-
// to figure out what the type is that the matched constants are used as.
320318
Finder->addMatcher(
321-
expr(traverse(TK_IgnoreUnlessSpelledInSource,
322-
expr(anyOfExhaustive(ConstantMatchers))),
323-
unless(hasParent(expr(
324-
anyOf(implicitCastExpr(hasImplicitDestinationType(isFloating())),
325-
explicitCastExpr(hasDestinationType(isFloating())))))),
326-
hasType(qualType(hasCanonicalTypeUnqualified(
327-
anyOf(qualType(asString("float")).bind("float"),
328-
qualType(asString("double")),
329-
qualType(asString("long double")).bind("long double")))))),
319+
expr(
320+
anyOfExhaustive(ConstantMatchers),
321+
unless(hasParent(explicitCastExpr(hasDestinationType(isFloating())))),
322+
hasType(qualType(hasCanonicalTypeUnqualified(
323+
anyOf(qualType(asString("float")).bind("float"),
324+
qualType(asString("double")),
325+
qualType(asString("long double")).bind("long double")))))),
330326
this);
331327
}
332328

clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class UseStdNumbersCheck : public ClangTidyCheck {
3737
Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
3838
Options.store(Opts, "DiffThreshold", DiffThresholdString);
3939
}
40+
std::optional<TraversalKind> getCheckTraversalKind() const override {
41+
return TK_IgnoreUnlessSpelledInSource;
42+
}
4043

4144
private:
4245
utils::IncludeInserter IncludeInserter;

clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@ The following list of constants from the ``numbers`` header are supported:
3131

3232
The list currently includes all constants as of C++20.
3333

34-
The replacements try to match the type of the inserted constant by how the
35-
removed expression was used, e.g., switching between ``std::numbers::e`` to
36-
``std::numbers::e_v<float>`` or ``std::numbers::e_v<long double>``
34+
The replacements use the type of the matched constant and can remove explicit casts,
35+
i.e., switching between ``std::numbers::e``, ``std::numbers::e_v<float>`` and ``std::numbers::e_v<long double>``
3736
where appropriate.
3837

3938
.. code-block:: c++
4039

4140
double sqrt(double);
42-
double log(double);
41+
double log2(double);
4342
void sink(auto&&) {}
4443
void floatSink(float);
4544

4645
#define MY_PI 3.1415926
4746

4847
void foo() {
49-
const double Pi = 3.141592653589; // const double Pi = std::numbers::pi
50-
const auto Use = Pi / 2; // no match for Pi
51-
static constexpr double Euler = 2.7182818; // static constexpr double Euler = std::numbers::e;
52-
53-
log2(exp(1)); // std::numbers::log2e;
54-
log2(Euler); // std::numbers::log2e;
55-
1 / sqrt(MY_PI); // std::numbers::inv_sqrtpi;
56-
sink(MY_PI); // sink(std::numbers::pi);
57-
floatSink(MY_PI); // floatSink(std::numbers::pi_v<float>);
48+
const double Pi = 3.141592653589; // const double Pi = std::numbers::pi
49+
const auto Use = Pi / 2; // no match for Pi
50+
static constexpr double Euler = 2.7182818; // static constexpr double Euler = std::numbers::e;
51+
52+
log2(exp(1)); // std::numbers::log2e;
53+
log2(Euler); // std::numbers::log2e;
54+
1 / sqrt(MY_PI); // std::numbers::inv_sqrtpi;
55+
sink(MY_PI); // sink(std::numbers::pi);
56+
floatSink(MY_PI); // floatSink(std::numbers::pi);
57+
floatSink(static_cast<float>(MY_PI)); // floatSink(std::numbers::pi_v<float>);
5858
}

clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ void foo(){
6666
// CHECK-FIXES-IMPRECISE: static constexpr double Phi2 = std::numbers::phi;
6767

6868
static constexpr double Pi3 = 3.1415926L;
69-
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:35: warning: prefer 'std::numbers::pi' to this literal, differs by '5.36e-08' [modernize-use-std-numbers]
70-
// CHECK-FIXES-ALL: static constexpr double Pi3 = std::numbers::pi;
69+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:35: warning: prefer 'std::numbers::pi_v<long double>' to this literal, differs by '5.36e-08' [modernize-use-std-numbers]
70+
// CHECK-FIXES-ALL: static constexpr double Pi3 = std::numbers::pi_v<long double>;
7171

7272
static constexpr double Euler3 = 2.7182818L;
73-
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:38: warning: prefer 'std::numbers::e' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
74-
// CHECK-FIXES-ALL: static constexpr double Euler3 = std::numbers::e;
73+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:38: warning: prefer 'std::numbers::e_v<long double>' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
74+
// CHECK-FIXES-ALL: static constexpr double Euler3 = std::numbers::e_v<long double>;
7575

7676
static constexpr double Phi3 = 1.6180339L;
77-
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:36: warning: prefer 'std::numbers::phi' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
78-
// CHECK-FIXES-ALL: static constexpr double Phi3 = std::numbers::phi;
77+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:36: warning: prefer 'std::numbers::phi_v<long double>' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
78+
// CHECK-FIXES-ALL: static constexpr double Phi3 = std::numbers::phi_v<long double>;
7979

8080
static constexpr long double Pi4 = 3.1415926L;
8181
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:40: warning: prefer 'std::numbers::pi_v<long double>' to this literal, differs by '5.36e-08' [modernize-use-std-numbers]
@@ -94,8 +94,8 @@ void foo(){
9494
// CHECK-FIXES-ALL: static constexpr my_double Euler5 = std::numbers::e;
9595

9696
static constexpr my_float Euler6 = 2.7182818;
97-
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:40: warning: prefer 'std::numbers::e_v<float>' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
98-
// CHECK-FIXES-ALL: static constexpr my_float Euler6 = std::numbers::e_v<float>;
97+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:40: warning: prefer 'std::numbers::e' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
98+
// CHECK-FIXES-ALL: static constexpr my_float Euler6 = std::numbers::e;
9999

100100
static constexpr int NotEuler7 = 2.7182818;
101101
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:38: warning: prefer 'std::numbers::e' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
@@ -183,16 +183,16 @@ void foo(){
183183
// CHECK-FIXES-ALL: auto log2e = std::numbers::log2e;
184184

185185
floatSink(log2(Euler));
186-
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this formula [modernize-use-std-numbers]
187-
// CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
186+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
187+
// CHECK-FIXES-ALL: floatSink(std::numbers::log2e);
188188

189189
floatSink(static_cast<float>(log2(Euler)));
190190
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this formula [modernize-use-std-numbers]
191191
// CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
192192

193193
floatSink(1.4426950);
194-
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this literal, differs by '4.09e-08' [modernize-use-std-numbers]
195-
// CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
194+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e' to this literal, differs by '4.09e-08' [modernize-use-std-numbers]
195+
// CHECK-FIXES-ALL: floatSink(std::numbers::log2e);
196196

197197
floatSink(static_cast<float>(1.4426950));
198198
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this literal, differs by '4.09e-08' [modernize-use-std-numbers]
@@ -217,8 +217,8 @@ void foo(){
217217
// CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
218218

219219
floatSink(static_cast<double>(1.4426950F));
220-
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this literal, differs by '1.93e-08' [modernize-use-std-numbers]
221-
// CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
220+
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e' to this literal, differs by '1.93e-08' [modernize-use-std-numbers]
221+
// CHECK-FIXES-ALL: floatSink(std::numbers::log2e);
222222

223223
floatSink(static_cast<int>(1.4426950F));
224224
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:32: warning: prefer 'std::numbers::log2e_v<float>' to this literal, differs by '1.93e-08' [modernize-use-std-numbers]

0 commit comments

Comments
 (0)