-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix #5591: Add check for meaningless comma operator in if statement, misplaced ')' #7406
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
clock999
wants to merge
1
commit into
danmar:main
Choose a base branch
from
clock999:wy_dev_5591
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.
+109
−1
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
# suspiciousComma | ||
|
||
**Message**: There is a suspicious comma expression directly after a function call in an if/while condition statement, is there a misplaced paranthesis?<br/> | ||
**Category**: ImproveCheck<br/> | ||
**Severity**: Style<br/> | ||
**Language**: C and C++ | ||
|
||
## Description | ||
|
||
When calling functions in an if or while condition statement, if the conditon is complex containing many parentheses, sometimes the parenthese might be misplaced if not carefull enough. If the function has a default argument, the compiler will not report errors, and the error may not be found and cause some unexpected result while the program running. | ||
``` | ||
int func(int a, int b = 100) { | ||
return a+b; | ||
} | ||
|
||
int call(int var) { | ||
if(func(func(100, 100)), var) {} //what actually wanted might be func(func(100, 100), var). | ||
} | ||
``` | ||
This is just to detect a most likely error, which could be some code wrote as intended with a bad style but not a real coding error. | ||
|
||
## How to fix | ||
|
||
The fix is simple. Just check the code carefully and make sure the function calls are as intended. |
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 |
---|---|---|
|
@@ -307,6 +307,8 @@ class TestOther : public TestFixture { | |
|
||
TEST_CASE(knownConditionFloating); | ||
TEST_CASE(knownConditionPrefixed); | ||
|
||
TEST_CASE(suspiciousComma); | ||
} | ||
|
||
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) | ||
|
@@ -13195,6 +13197,43 @@ class TestOther : public TestFixture { | |
"[test.cpp:2:13] -> [test.cpp:3:11]: (style) The comparison 'i > +1' is always false. [knownConditionTrueFalse]\n", | ||
errout_str()); | ||
} | ||
|
||
void suspiciousComma() | ||
{ | ||
check("void f(int a, int b = 0);\n" | ||
"void foo(int var) {\n" | ||
" if (f(100), var) {}\n" | ||
"}\n"); | ||
ASSERT_EQUALS( | ||
"[test.cpp:3:15]: (style) There is a suspicious comma expression directly after a function call " | ||
"in an if/while condition statement, is there a misplaced paranthesis? [suspiciousComma]\n", | ||
errout_str()); | ||
|
||
check("void f(int a, int b = 0);\n" | ||
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 tests that is good but it feels too little with just 2 tests. You could add a test that demonstrates that there must be a function call. i.e.:
And a test where there can't be additional parameters:
|
||
"void foo(int var) {\n" | ||
" while (f(100), var) {}\n" | ||
"}\n"); | ||
ASSERT_EQUALS( | ||
"[test.cpp:3:18]: (style) There is a suspicious comma expression directly after a function call " | ||
"in an if/while condition statement, is there a misplaced paranthesis? [suspiciousComma]\n", | ||
errout_str()); | ||
|
||
check("void f(int a, int b = 0);\n" | ||
"void foo(int * var) {\n" | ||
" if (f(100), var) {}\n" | ||
"}\n"); | ||
ASSERT_EQUALS( | ||
"", | ||
errout_str()); | ||
|
||
check("void f(int a);\n" | ||
"void foo(int var) {\n" | ||
" if (f(100), var) {}\n" | ||
"}\n"); | ||
ASSERT_EQUALS( | ||
"", | ||
errout_str()); | ||
} | ||
}; | ||
|
||
REGISTER_TEST(TestOther) |
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.
I don't really understand
&& !tok->astOperand2()->hasKnownValue()
.. is there any test that shows why that is needed?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.
Hi Danmar, if the if condition statement has a known value, maybe CheckCondition::alwaysTrueFalse should handle the case and pop up a corresponding warning. Seems we have discussed this previously in the discussed comments.