Skip to content

Commit 1a6a3c4

Browse files
committed
refactor implement
1 parent ba1a73e commit 1a6a3c4

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/SmallVector.h"
2020

2121
#include <cstdint>
22+
#include <optional>
2223

2324
using namespace clang::ast_matchers;
2425

@@ -48,7 +49,7 @@ NarrowingConversionsCheck::NarrowingConversionsCheck(StringRef Name,
4849
ClangTidyContext *Context)
4950
: ClangTidyCheck(Name, Context),
5051
WarnOnIntegerNarrowingConversion(
51-
Options.get("WarnOnIntegerNarrowingConversion", true)),
52+
Options.get<bool>("WarnOnIntegerNarrowingConversion")),
5253
WarnOnIntegerToFloatingPointNarrowingConversion(
5354
Options.get("WarnOnIntegerToFloatingPointNarrowingConversion", true)),
5455
WarnOnFloatingPointNarrowingConversion(
@@ -61,8 +62,9 @@ NarrowingConversionsCheck::NarrowingConversionsCheck(StringRef Name,
6162

6263
void NarrowingConversionsCheck::storeOptions(
6364
ClangTidyOptions::OptionMap &Opts) {
64-
Options.store(Opts, "WarnOnIntegerNarrowingConversion",
65-
WarnOnIntegerNarrowingConversion);
65+
if (WarnOnIntegerNarrowingConversion.has_value())
66+
Options.store(Opts, "WarnOnIntegerNarrowingConversion",
67+
WarnOnIntegerNarrowingConversion.value());
6668
Options.store(Opts, "WarnOnIntegerToFloatingPointNarrowingConversion",
6769
WarnOnIntegerToFloatingPointNarrowingConversion);
6870
Options.store(Opts, "WarnOnFloatingPointNarrowingConversion",
@@ -392,13 +394,13 @@ void NarrowingConversionsCheck::handleIntegralCast(const ASTContext &Context,
392394
SourceLocation SourceLoc,
393395
const Expr &Lhs,
394396
const Expr &Rhs) {
395-
if (WarnOnIntegerNarrowingConversion) {
396-
// From [conv.integral] since C++20
397-
// The result is the unique value of the destination type that is congruent
398-
// to the source integer modulo 2^N, where N is the width of the destination
399-
// type.
400-
if (getLangOpts().CPlusPlus20)
401-
return;
397+
// From [conv.integral] since C++20
398+
// The result is the unique value of the destination type that is congruent
399+
// to the source integer modulo 2^N, where N is the width of the destination
400+
// type.
401+
const bool ActualWarnOnIntegerNarrowingConversion =
402+
WarnOnIntegerNarrowingConversion.value_or(!getLangOpts().CPlusPlus20);
403+
if (ActualWarnOnIntegerNarrowingConversion) {
402404
const BuiltinType *ToType = getBuiltinType(Lhs);
403405
// From [conv.integral] before C++20:
404406
// Conversions to unsigned integer is well defined so no warning is issued.

clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_NARROWING_CONVERSIONS_H
1111

1212
#include "../ClangTidyCheck.h"
13+
#include <optional>
1314

1415
namespace clang::tidy::cppcoreguidelines {
1516

@@ -95,7 +96,7 @@ class NarrowingConversionsCheck : public ClangTidyCheck {
9596
const BuiltinType &FromType,
9697
const BuiltinType &ToType) const;
9798

98-
const bool WarnOnIntegerNarrowingConversion;
99+
const std::optional<bool> WarnOnIntegerNarrowingConversion;
99100
const bool WarnOnIntegerToFloatingPointNarrowingConversion;
100101
const bool WarnOnFloatingPointNarrowingConversion;
101102
const bool WarnWithinTemplateInstantiation;

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This check implements `ES.46
1212
from the C++ Core Guidelines.
1313

1414
We enforce only part of the guideline, more specifically, we flag narrowing conversions from:
15-
- an integer to a narrower integer before C++20 (e.g. ``char`` to ``unsigned char``)
15+
- an integer to a narrower integer (e.g. ``char`` to ``unsigned char``)
1616
if WarnOnIntegerNarrowingConversion Option is set,
1717
- an integer to a narrower floating-point (e.g. ``uint64_t`` to ``float``)
1818
if WarnOnIntegerToFloatingPointNarrowingConversion Option is set,
@@ -34,7 +34,9 @@ Options
3434
.. option:: WarnOnIntegerNarrowingConversion
3535

3636
When `true`, the check will warn on narrowing integer conversion
37-
(e.g. ``int`` to ``size_t``). `true` by default.
37+
(e.g. ``int`` to ``size_t``).
38+
Before C++20 `true` by default.
39+
Since C++20 `false` by default.
3840

3941
.. option:: WarnOnIntegerToFloatingPointNarrowingConversion
4042

0 commit comments

Comments
 (0)