Skip to content

Commit 3b7d92b

Browse files
committed
[clang-tidy] Preserve comments in readability-use-std-min-max
1 parent a83e09a commit 3b7d92b

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ static QualType getReplacementCastType(const Expr *CondLhs, const Expr *CondRhs,
9696
return GlobalImplicitCastType;
9797
}
9898

99-
static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs,
100-
const Expr *AssignLhs,
101-
const SourceManager &Source,
102-
const LangOptions &LO,
103-
StringRef FunctionName,
104-
const BinaryOperator *BO) {
99+
static std::string
100+
createReplacement(const Expr *CondLhs, const Expr *CondRhs,
101+
const Expr *AssignLhs, const SourceManager &Source,
102+
const LangOptions &LO, StringRef FunctionName,
103+
const BinaryOperator *BO, StringRef Comment = "") {
105104
const llvm::StringRef CondLhsStr = Lexer::getSourceText(
106105
Source.getExpansionRange(CondLhs->getSourceRange()), Source, LO);
107106
const llvm::StringRef CondRhsStr = Lexer::getSourceText(
@@ -116,7 +115,7 @@ static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs,
116115
(!GlobalImplicitCastType.isNull()
117116
? "<" + GlobalImplicitCastType.getAsString() + ">("
118117
: "(") +
119-
CondLhsStr + ", " + CondRhsStr + ");")
118+
CondLhsStr + ", " + CondRhsStr + ");" + Comment)
120119
.str();
121120
}
122121

@@ -172,13 +171,21 @@ void UseStdMinMaxCheck::check(const MatchFinder::MatchResult &Result) {
172171

173172
auto ReplaceAndDiagnose = [&](const llvm::StringRef FunctionName) {
174173
const SourceManager &Source = *Result.SourceManager;
174+
const std::string Comment =
175+
Lexer::getSourceText(
176+
CharSourceRange::getCharRange(
177+
Lexer::getLocForEndOfToken(If->getRParenLoc(), 0, Source, LO),
178+
If->getThen()->getBeginLoc()),
179+
Source, LO)
180+
.rtrim()
181+
.str();
175182
diag(IfLocation, "use `%0` instead of `%1`")
176183
<< FunctionName << BinaryOp->getOpcodeStr()
177184
<< FixItHint::CreateReplacement(
178185
SourceRange(IfLocation, Lexer::getLocForEndOfToken(
179186
ThenLocation, 0, Source, LO)),
180187
createReplacement(CondLhs, CondRhs, AssignLhs, Source, LO,
181-
FunctionName, BinaryOp))
188+
FunctionName, BinaryOp, Comment))
182189
<< IncludeInserter.createIncludeInsertion(
183190
Source.getFileID(If->getBeginLoc()), AlgorithmHeader);
184191
};

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ Changes in existing checks
555555
<clang-tidy/checks/readability/use-concise-preprocessor-directives>` check to
556556
generate correct fix-its for forms without a space after the directive.
557557

558+
- Improved :doc:`readability-use-std-min-max
559+
<clang-tidy/checks/readability/use-std-min-max>` check by ensuring that
560+
comments between the ``if`` condition and the ``then`` block are preserved
561+
when applying the fix.
562+
558563
Removed checks
559564
^^^^^^^^^^^^^^
560565

clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,14 @@ void useRight() {
273273
}
274274

275275
} // namespace gh121676
276+
277+
void testComment() {
278+
int box_depth = 10;
279+
int max_depth = 5;
280+
// CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::min` instead of `>` [readability-use-std-min-max]
281+
// CHECK-FIXES: box_depth = std::min(box_depth, max_depth); // limit the depth to max
282+
if (box_depth > max_depth) // limit the depth to max
283+
{
284+
box_depth = max_depth;
285+
}
286+
}

0 commit comments

Comments
 (0)