[clang-tools-extra] [clang-tidy] readability-container-contains literal suffixes (PR #74215)
https://github.com/schenker created https://github.com/llvm/llvm-project/pull/74215 Before this PR, readability-container-contains fixits 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); ``` >From 117b45abf919b264662de32c2e4014c6cd716bdf Mon Sep 17 00:00:00 2001 From: Thomas Schenker Date: Sat, 2 Dec 2023 11:10:26 +0100 Subject: [PATCH] [clang-tidy] readability-container-contains literal suffixes Before this PR, readability-container-contains fixits 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); ``` --- .../readability/ContainerContainsCheck.cpp| 4 +- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++ .../readability/container-contains.cpp| 37 ++- 3 files changed, 42 insertions(+), 3 deletions(-) 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 6d5f49dc06254..bdc44d6e4c14e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -399,6 +399,10 @@ Changes in existing checks ` diagnositics to highlight the const location +- Improved :doc:`readability-container-contains + ` to correctly handle + interger literals with suffixes in fixits. + - Improved :doc:`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..8e5ec0089b21d 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 @@ -55,7 +55,11 @@ int testDifferentCheckTypes(std::map &MyMap) { auto C6 = MyMap.find(5) != MyMap.end(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use 'contains' to check for membership [readability-container-contains] // CHECK-FIXES: auto C6 = MyMap.contains(5); - return C1 + C2 + C3 + C4 + C5 + C6; + auto C7 = 0 != 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); + + return C1 + C2 + C3 + C4 + C5 + C6 + C7; } // Check that we detect various common ways to check for non-membership @@ -175,6 +179,37 @@ int nonRewrittenCount(std::multimap &MyMap) { return C1 + C2 + C3 + C4; } +// Check different integer literal suffixes +int testDifferentIntegerLiteralSuffixes(std::map &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); +
[clang-tools-extra] [clang-tidy] readability-container-contains literal suffixes (PR #74215)
https://github.com/schenker updated https://github.com/llvm/llvm-project/pull/74215 >From f93185fe2346e7184c17ff35fb82ba0873e040a1 Mon Sep 17 00:00:00 2001 From: Thomas Schenker Date: Sat, 2 Dec 2023 11:10:26 +0100 Subject: [PATCH] [clang-tidy] readability-container-contains literal suffixes Before this PR, readability-container-contains fixits 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); ``` --- .../readability/ContainerContainsCheck.cpp| 4 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../readability/container-contains.cpp| 32 +++ 3 files changed, 38 insertions(+), 2 deletions(-) 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 6d5f49dc06254..bdc44d6e4c14e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -399,6 +399,10 @@ Changes in existing checks ` diagnositics to highlight the const location +- Improved :doc:`readability-container-contains + ` to correctly handle + interger literals with suffixes in fixits. + - Improved :doc:`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..cecedbb2b3ea1 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 @@ -55,6 +55,7 @@ int testDifferentCheckTypes(std::map &MyMap) { auto C6 = MyMap.find(5) != MyMap.end(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use 'contains' to check for membership [readability-container-contains] // CHECK-FIXES: auto C6 = MyMap.contains(5); + return C1 + C2 + C3 + C4 + C5 + C6; } @@ -175,6 +176,37 @@ int nonRewrittenCount(std::multimap &MyMap) { return C1 + C2 + C3 + C4; } +// Check different integer literal suffixes +int testDifferentIntegerLiteralSuffixes(std::map &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_
[clang-tools-extra] [clang-tidy] readability-container-contains literal suffixes (PR #74215)
https://github.com/schenker updated https://github.com/llvm/llvm-project/pull/74215 >From e6afca50ae820ec2e8cc2d53fa68d09f5cd3b1ed Mon Sep 17 00:00:00 2001 From: Thomas Schenker Date: Sat, 2 Dec 2023 11:10:26 +0100 Subject: [PATCH] [clang-tidy] readability-container-contains literal suffixes Before this PR, readability-container-contains fixits 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); ``` --- .../readability/ContainerContainsCheck.cpp| 4 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../readability/container-contains.cpp| 31 +++ 3 files changed, 37 insertions(+), 2 deletions(-) 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 6d5f49dc06254..bdc44d6e4c14e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -399,6 +399,10 @@ Changes in existing checks ` diagnositics to highlight the const location +- Improved :doc:`readability-container-contains + ` to correctly handle + interger literals with suffixes in fixits. + - Improved :doc:`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 &MyMap) { return C1 + C2 + C3 + C4; } +// Check different integer literal suffixes +int testDifferentIntegerLiteralSuffixes(std::map &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 &MySet) { #define COUNT_ONES(SET) SET.count(1) ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] readability-container-contains literal suffixes (PR #74215)
https://github.com/schenker updated https://github.com/llvm/llvm-project/pull/74215 >From e6afca50ae820ec2e8cc2d53fa68d09f5cd3b1ed Mon Sep 17 00:00:00 2001 From: Thomas Schenker Date: Sat, 2 Dec 2023 11:10:26 +0100 Subject: [PATCH 1/2] [clang-tidy] readability-container-contains literal suffixes Before this PR, readability-container-contains fixits 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); ``` --- .../readability/ContainerContainsCheck.cpp| 4 +-- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../readability/container-contains.cpp| 31 +++ 3 files changed, 37 insertions(+), 2 deletions(-) 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 6d5f49dc06254..bdc44d6e4c14e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -399,6 +399,10 @@ Changes in existing checks ` diagnositics to highlight the const location +- Improved :doc:`readability-container-contains + ` to correctly handle + interger literals with suffixes in fixits. + - Improved :doc:`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 &MyMap) { return C1 + C2 + C3 + C4; } +// Check different integer literal suffixes +int testDifferentIntegerLiteralSuffixes(std::map &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 &MySet) { #define COUNT_ONES(SET) SET.count(1) >From 61af3ce59ced216bece6ed22ef764aa99a1362f3 Mon Sep 17 00:00:00 2001 From: Thomas Schenker Date: Sun, 3 Dec 2023 07:43:09 +0100 Subject: [PATCH 2/2] fix typo --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) di
[clang-tools-extra] [clang-tidy] readability-container-contains literal suffixes (PR #74215)
https://github.com/schenker edited https://github.com/llvm/llvm-project/pull/74215 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] add some missing Kinds to libclang python bindings (PR #85571)
schenker wrote: Thanks for the fix. Will these changes find their way into an 18.x release? https://github.com/llvm/llvm-project/pull/85571 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] fix misc-const-correctness to work with function-try-blocks (PR #99925)
schenker wrote: > Looks like missing -fexceptions thanks for fixing it https://github.com/llvm/llvm-project/pull/99925 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] fix misc-const-correctness to work with function-try-blocks (PR #99925)
https://github.com/schenker created https://github.com/llvm/llvm-project/pull/99925 Make the clang-tidy check misc-const-correctness work with function-try-blocks. Fixes #99860. >From f16be0b63d6cd9728354c5f71cf3831482b6ec25 Mon Sep 17 00:00:00 2001 From: Thomas Schenker Date: Mon, 22 Jul 2024 21:39:28 +0200 Subject: [PATCH] improve clang-tidy misc-const-correctness to work with function-try-blocks. --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 17 - .../clang-tidy/misc/ConstCorrectnessCheck.h | 4 ++-- clang-tools-extra/docs/ReleaseNotes.rst | 3 +++ .../checkers/misc/const-correctness-values.cpp | 9 + 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 8b500de0c028c..e20cf6fbcb55a 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -93,13 +93,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { // shall be run. const auto FunctionScope = functionDecl( - hasBody( - compoundStmt(forEachDescendant( - declStmt(containsAnyDeclaration( -LocalValDecl.bind("local-value")), -unless(has(decompositionDecl( - .bind("decl-stmt"))) - .bind("scope"))) + hasBody(stmt(forEachDescendant( + declStmt(containsAnyDeclaration( +LocalValDecl.bind("local-value")), +unless(has(decompositionDecl( + .bind("decl-stmt"))) + .bind("scope"))) .bind("function-decl"); Finder->addMatcher(FunctionScope, this); @@ -109,7 +108,7 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { enum class VariableCategory { Value, Reference, Pointer }; void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { - const auto *LocalScope = Result.Nodes.getNodeAs("scope"); + const auto *LocalScope = Result.Nodes.getNodeAs("scope"); const auto *Variable = Result.Nodes.getNodeAs("local-value"); const auto *Function = Result.Nodes.getNodeAs("function-decl"); @@ -198,7 +197,7 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { } } -void ConstCorrectnessCheck::registerScope(const CompoundStmt *LocalScope, +void ConstCorrectnessCheck::registerScope(const Stmt *LocalScope, ASTContext *Context) { auto &Analyzer = ScopesCache[LocalScope]; if (!Analyzer) diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index 08ffde524522a..bba060e555d00 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -32,10 +32,10 @@ class ConstCorrectnessCheck : public ClangTidyCheck { void check(const ast_matchers::MatchFinder::MatchResult &Result) override; private: - void registerScope(const CompoundStmt *LocalScope, ASTContext *Context); + void registerScope(const Stmt *LocalScope, ASTContext *Context); using MutationAnalyzer = std::unique_ptr; - llvm::DenseMap ScopesCache; + llvm::DenseMap ScopesCache; llvm::DenseSet TemplateDiagnosticsCache; const bool AnalyzeValues; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index a23483e6df6d2..ed005d8fa19ea 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -120,6 +120,9 @@ Improvements to clang-tidy - Improved :program:`check_clang_tidy.py` script. Added argument `-export-fixes` to aid in clang-tidy and test development. +- Improved :doc:`misc-const-correctness + ` to work with function-try-blocks. + - Fixed bug where big values for unsigned check options overflowed into negative values when being printed with `--dump-config`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp index cb6bfcc1dccba..2af4bfb0bd449 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp @@ -56,6 +56,15 @@ void some_function(double np_arg0, wchar_t np_arg1) { np_local6--; } +int function_try_block() try { + int p_local0 = 0; + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int' can be declared 'const' + // CHECK-FIXES: int const p_local0 + return p_local0; +} catch (...) { + return 0;
[clang-tools-extra] [clang-tidy] fix misc-const-correctness to work with function-try-blocks (PR #99925)
https://github.com/schenker updated https://github.com/llvm/llvm-project/pull/99925 >From f16be0b63d6cd9728354c5f71cf3831482b6ec25 Mon Sep 17 00:00:00 2001 From: Thomas Schenker Date: Mon, 22 Jul 2024 21:39:28 +0200 Subject: [PATCH 1/2] improve clang-tidy misc-const-correctness to work with function-try-blocks. --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 17 - .../clang-tidy/misc/ConstCorrectnessCheck.h | 4 ++-- clang-tools-extra/docs/ReleaseNotes.rst | 3 +++ .../checkers/misc/const-correctness-values.cpp | 9 + 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 8b500de0c028c..e20cf6fbcb55a 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -93,13 +93,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { // shall be run. const auto FunctionScope = functionDecl( - hasBody( - compoundStmt(forEachDescendant( - declStmt(containsAnyDeclaration( -LocalValDecl.bind("local-value")), -unless(has(decompositionDecl( - .bind("decl-stmt"))) - .bind("scope"))) + hasBody(stmt(forEachDescendant( + declStmt(containsAnyDeclaration( +LocalValDecl.bind("local-value")), +unless(has(decompositionDecl( + .bind("decl-stmt"))) + .bind("scope"))) .bind("function-decl"); Finder->addMatcher(FunctionScope, this); @@ -109,7 +108,7 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { enum class VariableCategory { Value, Reference, Pointer }; void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { - const auto *LocalScope = Result.Nodes.getNodeAs("scope"); + const auto *LocalScope = Result.Nodes.getNodeAs("scope"); const auto *Variable = Result.Nodes.getNodeAs("local-value"); const auto *Function = Result.Nodes.getNodeAs("function-decl"); @@ -198,7 +197,7 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { } } -void ConstCorrectnessCheck::registerScope(const CompoundStmt *LocalScope, +void ConstCorrectnessCheck::registerScope(const Stmt *LocalScope, ASTContext *Context) { auto &Analyzer = ScopesCache[LocalScope]; if (!Analyzer) diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index 08ffde524522a..bba060e555d00 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -32,10 +32,10 @@ class ConstCorrectnessCheck : public ClangTidyCheck { void check(const ast_matchers::MatchFinder::MatchResult &Result) override; private: - void registerScope(const CompoundStmt *LocalScope, ASTContext *Context); + void registerScope(const Stmt *LocalScope, ASTContext *Context); using MutationAnalyzer = std::unique_ptr; - llvm::DenseMap ScopesCache; + llvm::DenseMap ScopesCache; llvm::DenseSet TemplateDiagnosticsCache; const bool AnalyzeValues; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index a23483e6df6d2..ed005d8fa19ea 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -120,6 +120,9 @@ Improvements to clang-tidy - Improved :program:`check_clang_tidy.py` script. Added argument `-export-fixes` to aid in clang-tidy and test development. +- Improved :doc:`misc-const-correctness + ` to work with function-try-blocks. + - Fixed bug where big values for unsigned check options overflowed into negative values when being printed with `--dump-config`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp index cb6bfcc1dccba..2af4bfb0bd449 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp @@ -56,6 +56,15 @@ void some_function(double np_arg0, wchar_t np_arg1) { np_local6--; } +int function_try_block() try { + int p_local0 = 0; + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int' can be declared 'const' + // CHECK-FIXES: int const p_local0 + return p_local0; +} catch (...) { + return 0; +} + void nested_scopes() { int p_local0 = 2; // CHECK-MESSAGES: [[@LINE-1]]:3: warni
[clang-tools-extra] [clang-tidy] fix misc-const-correctness to work with function-try-blocks (PR #99925)
@@ -120,6 +120,9 @@ Improvements to clang-tidy - Improved :program:`check_clang_tidy.py` script. Added argument `-export-fixes` to aid in clang-tidy and test development. +- Improved :doc:`misc-const-correctness schenker wrote: done https://github.com/llvm/llvm-project/pull/99925 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits