-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang-tidy] Ignore implicit functions in readability-implicit-bool-conversion #94512
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
PiotrZSL
merged 1 commit into
llvm:main
from
PiotrZSL:93409-c++-spaceship-operator-triggers-clang-tidy-warning
Jun 9, 2024
Merged
[clang-tidy] Ignore implicit functions in readability-implicit-bool-conversion #94512
PiotrZSL
merged 1 commit into
llvm:main
from
PiotrZSL:93409-c++-spaceship-operator-triggers-clang-tidy-warning
Jun 9, 2024
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
…onversion Ignore implicit declarations and defaulted functions. Helps with issues in generated code like, C++ spaceship operator. Closes llvm#93409
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: Piotr Zegar (PiotrZSL) ChangesIgnore implicit declarations and defaulted functions. Helps with issues in generated code like, C++ Closes #93409 Full diff: https://github.com/llvm/llvm-project/pull/94512.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index 28f5eada6d825..aa115cd450c4f 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -279,6 +279,9 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
hasParent(callExpr()),
hasSourceExpression(binaryOperator(hasAnyOperatorName("==", "!="))));
+ auto IsInCompilerGeneratedFunction = hasAncestor(namedDecl(anyOf(
+ isImplicit(), functionDecl(isDefaulted()), functionTemplateDecl())));
+
Finder->addMatcher(
traverse(TK_AsIs,
implicitCastExpr(
@@ -299,7 +302,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
// additional parens in replacement.
optionally(hasParent(stmt().bind("parentStmt"))),
unless(isInTemplateInstantiation()),
- unless(hasAncestor(functionTemplateDecl())))
+ unless(IsInCompilerGeneratedFunction))
.bind("implicitCastToBool")),
this);
@@ -331,7 +334,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
anything()),
unless(isInTemplateInstantiation()),
- unless(hasAncestor(functionTemplateDecl())))),
+ unless(IsInCompilerGeneratedFunction))),
this);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 33b65caf2b02c..6e2344de3e09f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -397,7 +397,8 @@ Changes in existing checks
valid fix suggestions for ``static_cast`` without a preceding space and
fixed problem with duplicate parentheses in double implicit casts. Corrected
the fix suggestions for C23 and later by using C-style casts instead of
- ``static_cast``.
+ ``static_cast``. Fixed false positives in C++20 spaceship operator by ignoring
+ casts in implicit and defaulted functions.
- Improved :doc:`readability-redundant-inline-specifier
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-cxx20.cpp
new file mode 100644
index 0000000000000..13aa5c5774b47
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-cxx20.cpp
@@ -0,0 +1,31 @@
+// RUN: %check_clang_tidy -std=c++20 %s readability-implicit-bool-conversion %t
+
+namespace std {
+struct strong_ordering {
+ int n;
+ constexpr operator int() const { return n; }
+ static const strong_ordering equal, greater, less;
+};
+constexpr strong_ordering strong_ordering::equal = {0};
+constexpr strong_ordering strong_ordering::greater = {1};
+constexpr strong_ordering strong_ordering::less = {-1};
+} // namespace std
+
+namespace PR93409 {
+ struct X
+ {
+ auto operator<=>(const X&) const = default;
+ bool m_b;
+ };
+
+ struct Y
+ {
+ auto operator<=>(const Y&) const = default;
+ X m_x;
+ };
+
+ bool compare(const Y& y1, const Y& y2)
+ {
+ return y1 == y2 || y1 < y2 || y1 > y2;
+ }
+}
|
5chmidti
approved these changes
Jun 8, 2024
nekoshirro
pushed a commit
to nekoshirro/Alchemist-LLVM
that referenced
this pull request
Jun 9, 2024
…onversion (llvm#94512) Ignore implicit declarations and defaulted functions. Helps with issues in generated code like, C++ spaceship operator. Closes llvm#93409 Signed-off-by: Hafidz Muzakky <[email protected]>
Closed
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.
Ignore implicit declarations and defaulted functions. Helps with issues in generated code like, C++
spaceship operator.
Closes #93409