[clang-tools-extra] 8ac84a9 - [clang-tidy] readability-container-contains literal suffixes (#74215)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 4 08:30:17 PST 2023
Author: Thomas Schenker
Date: 2023-12-04T17:30:12+01:00
New Revision: 8ac84a9555bc25ed9a53ea6a5a09432e18ee9042
URL: https://github.com/llvm/llvm-project/commit/8ac84a9555bc25ed9a53ea6a5a09432e18ee9042
DIFF: https://github.com/llvm/llvm-project/commit/8ac84a9555bc25ed9a53ea6a5a09432e18ee9042.diff
LOG: [clang-tidy] readability-container-contains literal suffixes (#74215)
Before this PR, readability-container-contains fix-its did not handle
integer literal suffixes correctly. It e.g. changed
```
MyMap.count(2) != 0U;
```
into
```
MyMap.contains(2)U;
```
With this PR, it correctly changes it to
```
MyMap.contains(2);
```
Added:
Modified:
clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
index 970ed8b83e0a7..dbb50a060e596 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
@@ -131,8 +131,8 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
Diag << FixItHint::CreateReplacement(
CharSourceRange::getCharRange(ComparisonBegin, CallBegin),
Negated ? "!" : "");
- Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
- CallEnd.getLocWithOffset(1), ComparisonEnd.getLocWithOffset(1)));
+ Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
+ CallEnd.getLocWithOffset(1), ComparisonEnd));
}
} // namespace clang::tidy::readability
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 81c8b686d1cc1..4ff4494cef562 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -406,6 +406,10 @@ Changes in existing checks
<clang-tidy/checks/readability/avoid-const-params-in-decls>` diagnositics to
highlight the const location
+- Improved :doc:`readability-container-contains
+ <clang-tidy/checks/readability/container-contains>` to correctly handle
+ interger literals with suffixes in fix-its.
+
- Improved :doc:`readability-container-size-empty
<clang-tidy/checks/readability/container-size-empty>` check to
detect comparison between string and empty string literals and support
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp
index 05a4ebc9c8a17..0ecb61b2e7df0 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp
@@ -175,6 +175,37 @@ int nonRewrittenCount(std::multimap<int, int> &MyMap) {
return C1 + C2 + C3 + C4;
}
+// Check
diff erent integer literal suffixes
+int testDifferentIntegerLiteralSuffixes(std::map<int, int> &MyMap) {
+
+ auto C1 = MyMap.count(2) != 0U;
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use 'contains' to check for membership [readability-container-contains]
+ // CHECK-FIXES: auto C1 = MyMap.contains(2);
+ auto C2 = MyMap.count(2) != 0UL;
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use 'contains' to check for membership [readability-container-contains]
+ // CHECK-FIXES: auto C2 = MyMap.contains(2);
+ auto C3 = 0U != MyMap.count(2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use 'contains' to check for membership [readability-container-contains]
+ // CHECK-FIXES: auto C3 = MyMap.contains(2);
+ auto C4 = 0UL != MyMap.count(2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: use 'contains' to check for membership [readability-container-contains]
+ // CHECK-FIXES: auto C4 = MyMap.contains(2);
+ auto C5 = MyMap.count(2) < 1U;
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use 'contains' to check for membership [readability-container-contains]
+ // CHECK-FIXES: auto C5 = !MyMap.contains(2);
+ auto C6 = MyMap.count(2) < 1UL;
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use 'contains' to check for membership [readability-container-contains]
+ // CHECK-FIXES: auto C6 = !MyMap.contains(2);
+ auto C7 = 1U > MyMap.count(2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use 'contains' to check for membership [readability-container-contains]
+ // CHECK-FIXES: auto C7 = !MyMap.contains(2);
+ auto C8 = 1UL > MyMap.count(2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use 'contains' to check for membership [readability-container-contains]
+ // CHECK-FIXES: auto C8 = !MyMap.contains(2);
+
+ return C1 + C2 + C3 + C4 + C5 + C6 + C7 + C8;
+}
+
// We don't want to rewrite if the `contains` call is from a macro expansion
int testMacroExpansion(std::unordered_set<int> &MySet) {
#define COUNT_ONES(SET) SET.count(1)
More information about the cfe-commits
mailing list