-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Added options to readability-implicit-bool-conversion #120087
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
Changes from 11 commits
03f5368
16c7c95
0d6fae8
8f134a3
e13bf5a
a480460
5d1e7fc
391c870
9e83c0d
4b75aff
dc67c7f
c6078dc
0882713
d59f000
4520dd6
a9376ea
dc229ca
f6efcaa
ac54d56
74031e5
9b46c09
dd41e29
d0c0593
a56019f
ed6e001
4900886
e1eaf30
899683c
414768e
565bef8
dac44f4
fd1f7dc
df5e48e
56a4e09
75fe805
dff9123
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -319,10 +319,11 @@ Changes in existing checks | |
| diagnostic. | ||
|
|
||
| - Improved :doc:`readability-implicit-bool-conversion | ||
| <clang-tidy/checks/readability/implicit-bool-conversion>` check | ||
| by adding the option `UseUpperCaseLiteralSuffix` to select the | ||
| case of the literal suffix in fixes and fixing false positive for implicit | ||
| conversion of comparison result in C23. | ||
| <clang-tidy/checks/readability/implicit-bool-conversion>` | ||
| Check by adding the option | ||
|
||
| - `UseUpperCaseLiteralSuffix` : to select the case of the literal suffix in fixes and fixing false positive for implicit conversion of comparison result in C23. | ||
|
||
| - `CheckConversionsToBool`: to allow enabling or disabling warnings for implicit conversions to `bool` . | ||
| - `CheckConversionsFromBool`: to allow enabling or disabling warnings for implicit conversions from `bool`. | ||
|
|
||
| - Improved :doc:`readability-redundant-smartptr-get | ||
| <clang-tidy/checks/readability/redundant-smartptr-get>` check to | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have added // CHECK-MESSAGES-TO-BOOL-FALSEAlthough, instead of using the suffixes to say
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks ,I am working on it and also on clean PR build.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have wrote a basic-level test similar to other checks and also have done a clean PR build.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have written check file for all permutations of configuration of the options and also for the more comprehensive test cases as instructed with clean PR build. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t | ||
|
|
||
| // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t \ | ||
| // RUN: -- -config='{CheckOptions: [{key: readability-implicit-bool-conversion.CheckConversionsToBool, value: false}, {key: readability-implicit-bool-conversion.CheckConversionsFromBool, value: true}]}' | ||
|
||
|
|
||
| // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t \ | ||
| // RUN: -- -config='{CheckOptions: [{key: readability-implicit-bool-conversion.CheckConversionsToBool, value: true}, {key: readability-implicit-bool-conversion.CheckConversionsFromBool, value: false}]}' | ||
|
|
||
| // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t \ | ||
| // RUN: -- -config='{CheckOptions: [{key: readability-implicit-bool-conversion.CheckConversionsToBool, value: false}, {key: readability-implicit-bool-conversion.CheckConversionsFromBool, value: false}]}' | ||
|
|
||
| // ========================================================== | ||
| // Test Case: Conversions to bool (CheckConversionsToBool=true) | ||
| // ========================================================== | ||
| void TestConversionsToBool() { | ||
| int x = 42; | ||
| if (x) // CHECK-MESSAGES: :[[@LINE]]:8: warning: implicit conversion 'int' -> 'bool' | ||
| (void)0; | ||
|
|
||
| float f = 3.14; | ||
| if (f) // CHECK-MESSAGES: :[[@LINE]]:8: warning: implicit conversion 'float' -> 'bool' | ||
| (void)0; | ||
|
|
||
| int *p = nullptr; | ||
| if (p) // CHECK-MESSAGES: :[[@LINE]]:8: warning: implicit conversion 'int *' -> 'bool' | ||
| (void)0; | ||
|
|
||
| // Pointer-to-member | ||
| struct S { | ||
| int member; | ||
| }; | ||
| int S::*ptr = nullptr; | ||
| if (ptr) // CHECK-MESSAGES: :[[@LINE]]:8: warning: implicit conversion 'int S::*' -> 'bool' | ||
| (void)0; | ||
| } | ||
|
|
||
| // ========================================================== | ||
| // Test Case: Conversions from bool (CheckConversionsFromBool=true) | ||
| // ========================================================== | ||
| void TestConversionsFromBool() { | ||
| bool b = true; | ||
|
|
||
| int x = b; // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int' | ||
| float f = b; // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'float' | ||
| } | ||
|
|
||
| // ========================================================== | ||
| // Test Case: Mixed Configurations (ToBool=false, FromBool=true) | ||
| // ========================================================== | ||
| void TestMixedConfig() { | ||
| int x = 42; | ||
| if (x) // No warning: CheckConversionsToBool=false | ||
| (void)0; | ||
|
|
||
| bool b = true; | ||
| int y = b; // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int' | ||
| } | ||
|
|
||
| // ========================================================== | ||
| // Test Case: No Diagnostics (ToBool=false, FromBool=false) | ||
| // ========================================================== | ||
| void TestNoDiagnostics() { | ||
| int x = 42; | ||
| if (x) // No warning: CheckConversionsToBool=false | ||
| (void)0; | ||
|
|
||
| bool b = true; | ||
| int y = b; // No warning: CheckConversionsFromBool=false | ||
| } | ||
|
|
||
| // ========================================================== | ||
| // Test Case: Edge Cases and Complex Expressions | ||
| // ========================================================== | ||
| void TestEdgeCases() { | ||
| bool b = true; | ||
|
|
||
| // Nested implicit casts | ||
| int x = (b ? 1 : 0); // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int' | ||
|
|
||
| // Function returns implicit bool | ||
| auto ReturnBool = []() -> bool { return true; }; | ||
| int y = ReturnBool(); // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int' | ||
|
|
||
| // Explicit casts (no diagnostics) | ||
| int z = static_cast<int>(b); // No warning: explicit cast | ||
| } | ||
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.
Wrong, place these things should be actually put into matcher in registerMatchers method, to speed up.
You got 2 calls to addMatcher, one in line 300, other in line 339, put those (with dependences to avoid unused local variables) into if's.
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.
Okay Thanks I am Working on it.
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.
Can You please check this? I have done this now as instructed.