-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] Added warn-assignment-bool-context #115234
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
Open
PhilippRados
wants to merge
5
commits into
llvm:main
Choose a base branch
from
PhilippRados:implicit_bool_cast_assign
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e819360
[clang] Added warn-assignment-bool-context
PhilippRados 2a06e37
[clang] Store condition info in Expr; Passes all tests
PhilippRados 419fb2a
[clang] Changed IsInsideCondition field to private
PhilippRados 303a982
[clang] Formatted
PhilippRados b72f7a7
[clang] Merged diagnostic warn_condition_is_assignment into generic w…
PhilippRados File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // RUN: %clang_cc1 -x c -fsyntax-only -Wparentheses -verify %s | ||
|
|
||
| #define bool _Bool | ||
| #define true 1 | ||
| #define false 0 | ||
|
|
||
| // Do not emit the warning for compound-assignments. | ||
| bool f(int x) { return x = 0; } // expected-warning {{using the result of an assignment as a truth value without parentheses}}\ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
| bool f2(int x) { return x += 0; } | ||
|
|
||
| bool f3(bool x) { return x = 0; } | ||
|
|
||
| void test() { | ||
| int x; | ||
|
|
||
| // This should emit the `warn_assignment_bool_context` warning once, since | ||
| // C doesn't do implicit conversion booleans for conditions. | ||
| if (x = 0) {} // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}}\ | ||
| // expected-note{{use '==' to turn this assignment into an equality comparison}} | ||
| if (x = 4 && x){} // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}}\ | ||
| // expected-note{{use '==' to turn this assignment into an equality comparison}} | ||
|
|
||
| (void)(bool)(x = 1); | ||
| (void)(bool)(int)(x = 1); | ||
|
|
||
|
|
||
| bool _a = x = 3; // expected-warning {{using the result of an assignment as a truth value without parentheses}}\ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
|
|
||
| // Shouldn't warn for above cases if parentheses were provided. | ||
| if ((x = 0)) {} | ||
| bool _b = (x = 3); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // RUN: %clang_cc1 -x c++ -fsyntax-only -Wparentheses -verify %s | ||
|
|
||
| // Do not emit the warning for compound-assignments. | ||
| bool f(int x) { return x = 0; } // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
| bool f2(int x) { return x += 0; } | ||
|
|
||
| bool f3(bool x) { return x = 0; } | ||
|
|
||
| void test() { | ||
| int x; | ||
|
|
||
| // Assignments inside of conditions should still emit the more specific `==` fixits. | ||
| if (x = 0) {} // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{use '==' to turn this assignment into an equality comparison}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
| if (x = 4 && x){} // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{use '==' to turn this assignment into an equality comparison}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
|
|
||
|
|
||
| (void)bool(x = 1); // expected-warning {{using the result of an assignment as a truth value without parentheses}}\ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
| (void)(bool)(x = 1); | ||
|
|
||
| // This should still emit since the RHS is casted to `int` before being casted back to `bool`. | ||
| (void)bool(x = false); // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
|
|
||
| // Should only issue warning once, even if multiple implicit casts. | ||
| // FIXME: This only checks that warning occurs not how often. | ||
| (void)bool(bool(x = 1)); // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
| (void)bool(int(bool(x = 1))); // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
| (void)bool(int(x = 1)); | ||
|
|
||
| bool _a = x = 3; // expected-warning {{using the result of an assignment as a truth value without parentheses}} \ | ||
| // expected-note{{place parentheses around the assignment to silence this warning}} | ||
|
|
||
| // Shouldn't warn for above cases if parentheses were provided. | ||
| if ((x = 0)) {} | ||
| (void)bool((x = 1)); | ||
| bool _b= (x = 3); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Not sure what the question is here, can you clarify?
ALSO ALSO, if this ends up in C++ anywhere, you'll have some user-defined operators to mess with as well.
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.
Basically just asking if
ImplicitCastExpris the only valid expression that can be in between the outer cast and the assignment expression or if there is another implicit expression that would need to be matched here.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.
How can I check for that? Do you have a more robust approach?
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.
None I can think of? We might end up having a case where we don't properly warn if we miss anything here, which is perhaps acceptable.
Having this happen on the implicit cast makes this a little awkward since we can't record 'what is going on here' as we go. That said, I think punting /not warning in the case of user-defined-operators is probably OK, that will prevent us from diagnosing in the cases of some DSLs or something, which is the behavior we probably want?