Skip to content

Commit 98d252e

Browse files
committed
Addressing the part of Piotr's review
1 parent 892f5db commit 98d252e

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,26 @@ static SmallVector<FixItHint>
8080
generateReplacements(const MatchFinder::MatchResult &Match,
8181
const CallExpr *TopCall, const FindArgsResult &Result,
8282
const bool IgnoreNonTrivialTypes,
83-
const unsigned long IgnoreTrivialTypesOfSizeAbove) {
83+
const long IgnoreTrivialTypesOfSizeAbove) {
8484
SmallVector<FixItHint> FixItHints;
85+
const SourceManager &SourceMngr = *Match.SourceManager;
86+
const LangOptions &LanguageOpts = Match.Context->getLangOpts();
8587

8688
const QualType ResultType = TopCall->getDirectCallee()
8789
->getReturnType()
90+
.getCanonicalType()
8891
.getNonReferenceType()
89-
.getUnqualifiedType()
90-
.getCanonicalType();
92+
.getUnqualifiedType();
9193

9294
// check if the type is trivial
93-
const bool isResultTypeTrivial = Match.Context->getBaseElementType(ResultType)
94-
.isTrivialType(*Match.Context);
95-
const SourceManager &SourceMngr = *Match.SourceManager;
96-
const LangOptions &LanguageOpts = Match.Context->getLangOpts();
95+
const bool isResultTypeTrivial = ResultType.isTrivialType(*Match.Context);
9796

9897
if ((!isResultTypeTrivial && IgnoreNonTrivialTypes))
9998
return FixItHints;
10099

101100
if (isResultTypeTrivial &&
102101
// size in bits divided by 8 to get bytes
103-
Match.Context->getTypeSize(ResultType) / 8 >
102+
Match.Context->getTypeSizeInChars(ResultType).getQuantity() >
104103
IgnoreTrivialTypesOfSizeAbove)
105104
return FixItHints;
106105

@@ -112,8 +111,8 @@ generateReplacements(const MatchFinder::MatchResult &Match,
112111
// check if typecast is required
113112
const QualType ArgType = Arg->IgnoreParenImpCasts()
114113
->getType()
115-
.getUnqualifiedType()
116-
.getCanonicalType();
114+
.getCanonicalType()
115+
.getUnqualifiedType();
117116

118117
if (ArgType == ResultType)
119118
continue;
@@ -122,11 +121,11 @@ generateReplacements(const MatchFinder::MatchResult &Match,
122121
CharSourceRange::getTokenRange(Arg->getSourceRange()), SourceMngr,
123122
LanguageOpts);
124123

125-
Twine Replacement = llvm::Twine("static_cast<")
126-
.concat(ResultType.getAsString(LanguageOpts))
127-
.concat(">(")
128-
.concat(ArgText)
129-
.concat(")");
124+
const auto Replacement = Twine("static_cast<")
125+
.concat(ResultType.getAsString(LanguageOpts))
126+
.concat(">(")
127+
.concat(ArgText)
128+
.concat(")");
130129

131130
FixItHints.push_back(FixItHint::CreateReplacement(Arg->getSourceRange(),
132131
Replacement.str()));
@@ -200,7 +199,7 @@ MinMaxUseInitializerListCheck::MinMaxUseInitializerListCheck(
200199
: ClangTidyCheck(Name, Context),
201200
IgnoreNonTrivialTypes(Options.get("IgnoreNonTrivialTypes", true)),
202201
IgnoreTrivialTypesOfSizeAbove(
203-
Options.get("IgnoreTrivialTypesOfSizeAbove", 32UL)),
202+
Options.get("IgnoreTrivialTypesOfSizeAbove", 32L)),
204203
Inserter(Options.getLocalOrGlobal("IncludeStyle",
205204
utils::IncludeSorter::IS_LLVM),
206205
areDiagsSelfContained()) {}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MinMaxUseInitializerListCheck : public ClangTidyCheck {
4747

4848
private:
4949
bool IgnoreNonTrivialTypes;
50-
unsigned long IgnoreTrivialTypesOfSizeAbove;
50+
long IgnoreTrivialTypesOfSizeAbove;
5151
utils::IncludeInserter Inserter;
5252
};
5353

clang-tools-extra/docs/clang-tidy/checks/modernize/min-max-use-initializer-list.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
modernize-min-max-use-initializer-list
44
======================================
55

6-
Replaces nested ``std::min`` and ``std::max`` calls with an initializer list where applicable.
6+
Replaces nested ``std::min`` and ``std::max`` calls with an initializer list
7+
where applicable.
78

89
For instance, consider the following code:
910

@@ -20,9 +21,12 @@ The check will transform the above code to:
2021
Performance Considerations
2122
==========================
2223

23-
While this check simplifies the code and makes it more readable, it may cause performance degradation for non-trivial types due to the need to copy objects into the initializer list.
24+
While this check simplifies the code and makes it more readable, it may cause
25+
performance degradation for non-trivial types due to the need to copy objects
26+
into the initializer list.
2427

25-
To avoid this, it is recommended to use `std::ref` or `std::cref` for non-trivial types:
28+
To avoid this, it is recommended to use `std::ref` or `std::cref` for
29+
non-trivial types:
2630

2731
.. code-block:: cpp
2832
@@ -42,4 +46,5 @@ Options
4246

4347
.. option:: IgnoreTrivialTypesOfSizeAbove
4448

45-
An integer specifying the size (in bytes) above which trivial types are ignored. Default is `32`.
49+
An integer specifying the size (in bytes) above which trivial types are
50+
ignored. Default is `32`.

0 commit comments

Comments
 (0)