-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy] Fix invalid fixit from modernize-use-ranges for nullptr used with std::unique_ptr #127162
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: None (Andrewyuan34) ChangesThis PR fixes issue #124815 by correcting the handling of Changes:
Linked Issue:Fixes #12345. Testing:
Full diff: https://github.com/llvm/llvm-project/pull/127162.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp
index 604204e762c78..03b4321ceae99 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp
@@ -165,6 +165,7 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
Buff.assign({"::std::", Name});
Result.try_emplace(Buff, Replacer);
}
+ // auto Diag = diag("1111");
}
if (getLangOpts().CPlusPlus23)
Result.try_emplace(
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
index b022efebfdf4d..57ca038f64511 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
@@ -1,14 +1,24 @@
-// RUN: %check_clang_tidy -std=c++20 %s modernize-use-ranges %t -- -- -I %S/Inputs/use-ranges/
-// RUN: %check_clang_tidy -std=c++23 %s modernize-use-ranges %t -check-suffixes=,CPP23 -- -I %S/Inputs/use-ranges/
+// RUN: %check_clang_tidy -std=c++20 %s modernize-use-ranges %t -- -- -I %S/Inputs/
+// RUN: %check_clang_tidy -std=c++23 %s modernize-use-ranges %t -check-suffixes=,CPP23 -- -I %S/Inputs/
+// Example: ./check_clang_tidy.py -std=c++20 checkers/modernize/use-ranges.cpp modernize-use-ranges temp.txt -- -- -I ~/llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/Inputs/
// CHECK-FIXES: #include <algorithm>
// CHECK-FIXES-CPP23: #include <numeric>
// CHECK-FIXES: #include <ranges>
-#include "fake_std.h"
+#include "use-ranges/fake_std.h"
+#include "smart-ptr/unique_ptr.h"
void Positives() {
std::vector<int> I, J;
+
+ // Expect to have no check messages
+ std::find(I.begin(), I.end(), nullptr);
+
+ std::find(I.begin(), I.end(), std::unique_ptr<int>());
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+ // CHECK-FIXES: std::ranges::find(I, std::unique_ptr<int>());
+
std::find(I.begin(), I.end(), 0);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES: std::ranges::find(I, 0);
@@ -76,6 +86,14 @@ void Positives() {
void Reverse(){
std::vector<int> I, J;
+
+ // Expect to have no check messages
+ std::find(I.rbegin(), I.rend(), nullptr);
+
+ std::find(I.rbegin(), I.rend(), std::unique_ptr<int>());
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+ // CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(I), std::unique_ptr<int>());
+
std::find(I.rbegin(), I.rend(), 0);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES: std::ranges::find(std::ranges::reverse_view(I), 0);
@@ -112,3 +130,4 @@ void Negatives() {
// Pathological, but probably shouldn't diagnose this
std::rotate(I.begin(), I.end(), I.end() + 0);
}
+
|
a1dfd7d
to
7b73f5d
Compare
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.
Short release notes entry about fix would be nice.
|
||
void Positives() { | ||
std::vector<int> I, J; | ||
|
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.
I assume those tests pass due to "fake_std", would be good to use proper vector:
std::vector<std::unique_ptr<int>>
for these tests.
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.
I assume those tests pass due to "fake_std", would be good to use proper vector:
std::vector<std::unique_ptr<int>>
for these tests.
good point! Already applied.
e084f68
to
6d83f55
Compare
Done |
dc76c62
to
101ec5e
Compare
@@ -109,6 +109,9 @@ Changes in existing checks | |||
- Improved :doc:`misc-redundant-expression | |||
<clang-tidy/checks/misc/redundant-expression>` check by providing additional | |||
examples and fixing some macro related false positives. | |||
|
|||
- Improved :doc:`modernize-use-ranges | |||
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress warnings logic for ``nullptr`` in ``std::find``. |
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.
Please make lines no more than 80 characters long (rule applied to all docs).
a9b76a2
to
c6a732c
Compare
Thanks for addressing this. However I think a more general solution is required (instead of special-casing |
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. since it is just a patch instead of ideal solution for std::equality_comparable_with
stuff, could you add some FIXME in code?
deeb8cd
to
5bbd811
Compare
Thanks for the comment, I got confused by the following comments, should I just leave a FIXME or use a more general way to solve it. Or maybe we could create a new issue? |
Thanks for the comment! Already added. |
ping me if you want to merge it. |
Thanks! I'd like to merge it. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
a0cbdf6
to
879ef53
Compare
sorry about forgetting to use git-clang-format to check the differences. Now it should work. |
09b809d
to
cb684ab
Compare
…used with std::unique_ptr
cb684ab
to
f887433
Compare
I‘m sorry for requesting approval again. But I'm really confused that should I make sure my branch is alwasy up-to-date? |
Rebase from |
Thanks for the explanation! |
@HerrCai0907 Sorry for requesting your approval again, I really have no experience sending a PR. |
Thanks for you contributions! Some tips:
|
@Andrewyuan34 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
I really appreciate all of the suggestions! Will take those in mind. Looking forward to the day I have write access. |
This PR fixes issue #124815 by correcting the handling of
nullptr
withstd::unique_ptr
in themodernize-use-ranges
check.Changes:
Modified
clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp
:nullptr
instd::find
.Added unit tests:
nullptr
.std::unique_ptr<int>()
remains unchanged.Linked Issue:
Fixes #124815.
Testing:
Note:
This is my first PR to the LLVM project. If there are any issues or areas for improvement, please let me know—I’m happy to make adjustments and learn from your feedback! Thank you for your patience and guidance.