-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-tidy] Preserve comments in readability-use-std-min-max
#169908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: mitchell (zeyi2) ChangesCloses #121613 Full diff: https://github.com/llvm/llvm-project/pull/169908.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
index 5a7add88d6eeb..f78e62fbc3c27 100644
--- a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
@@ -96,12 +96,11 @@ static QualType getReplacementCastType(const Expr *CondLhs, const Expr *CondRhs,
return GlobalImplicitCastType;
}
-static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs,
- const Expr *AssignLhs,
- const SourceManager &Source,
- const LangOptions &LO,
- StringRef FunctionName,
- const BinaryOperator *BO) {
+static std::string
+createReplacement(const Expr *CondLhs, const Expr *CondRhs,
+ const Expr *AssignLhs, const SourceManager &Source,
+ const LangOptions &LO, StringRef FunctionName,
+ const BinaryOperator *BO, StringRef Comment = "") {
const llvm::StringRef CondLhsStr = Lexer::getSourceText(
Source.getExpansionRange(CondLhs->getSourceRange()), Source, LO);
const llvm::StringRef CondRhsStr = Lexer::getSourceText(
@@ -116,7 +115,7 @@ static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs,
(!GlobalImplicitCastType.isNull()
? "<" + GlobalImplicitCastType.getAsString() + ">("
: "(") +
- CondLhsStr + ", " + CondRhsStr + ");")
+ CondLhsStr + ", " + CondRhsStr + ");" + Comment)
.str();
}
@@ -172,13 +171,21 @@ void UseStdMinMaxCheck::check(const MatchFinder::MatchResult &Result) {
auto ReplaceAndDiagnose = [&](const llvm::StringRef FunctionName) {
const SourceManager &Source = *Result.SourceManager;
+ const std::string Comment =
+ Lexer::getSourceText(
+ CharSourceRange::getCharRange(
+ Lexer::getLocForEndOfToken(If->getRParenLoc(), 0, Source, LO),
+ If->getThen()->getBeginLoc()),
+ Source, LO)
+ .rtrim()
+ .str();
diag(IfLocation, "use `%0` instead of `%1`")
<< FunctionName << BinaryOp->getOpcodeStr()
<< FixItHint::CreateReplacement(
SourceRange(IfLocation, Lexer::getLocForEndOfToken(
ThenLocation, 0, Source, LO)),
createReplacement(CondLhs, CondRhs, AssignLhs, Source, LO,
- FunctionName, BinaryOp))
+ FunctionName, BinaryOp, Comment))
<< IncludeInserter.createIncludeInsertion(
Source.getFileID(If->getBeginLoc()), AlgorithmHeader);
};
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a6f80e3721db1..d2da19b61132c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -555,6 +555,11 @@ Changes in existing checks
<clang-tidy/checks/readability/use-concise-preprocessor-directives>` check to
generate correct fix-its for forms without a space after the directive.
+- Improved :doc:`readability-use-std-min-max
+ <clang-tidy/checks/readability/use-std-min-max>` check by ensuring that
+ comments between the ``if`` condition and the ``then`` block are preserved
+ when applying the fix.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
index 35ade8a7c6d37..f51bf1ae98ed1 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
@@ -273,3 +273,14 @@ void useRight() {
}
} // namespace gh121676
+
+void testComment() {
+ int box_depth = 10;
+ int max_depth = 5;
+ // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::min` instead of `>` [readability-use-std-min-max]
+ // CHECK-FIXES: box_depth = std::min(box_depth, max_depth); // limit the depth to max
+ if (box_depth > max_depth) // limit the depth to max
+ {
+ box_depth = max_depth;
+ }
+}
|
vbvictor
reviewed
Dec 2, 2025
clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
Outdated
Show resolved
Hide resolved
vbvictor
reviewed
Dec 2, 2025
zeyi2
commented
Dec 3, 2025
clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
Show resolved
Hide resolved
vbvictor
reviewed
Dec 3, 2025
clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
Outdated
Show resolved
Hide resolved
vbvictor
reviewed
Dec 3, 2025
HerrCai0907
reviewed
Dec 3, 2025
vbvictor
approved these changes
Dec 4, 2025
Contributor
vbvictor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
honeygoyal
pushed a commit
to honeygoyal/llvm-project
that referenced
this pull request
Dec 9, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #121613