-
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
base: main
Are you sure you want to change the base?
Conversation
53083da
to
1687100
Compare
Thanks for your contribution. Note that these issues seem to be diagnosed already by current compilers. |
Hi CHR, many thanks for your comments, and I have updated the commit. But there is a failed case tested on the github for the commit, that seems not the problem of the committed code. Seems the network traffic caused a overtime. But I am not sure. Do you know how to trigger the checks to run again? Thanks a lot! |
The macos runners can be very slow sometimes. I have restarted the failing run. |
@@ -204,6 +204,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \ | |||
$(libcppdir)/checkleakautovar.o \ | |||
$(libcppdir)/checkmemoryleak.o \ | |||
$(libcppdir)/checknullpointer.o \ | |||
$(libcppdir)/checkoperatorcomma.o \ |
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 want that we have files with just 1 checker. With this filename it's hard to see that more checkers would be added later. I suggest that your checker is added in checkother instead.
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.
or make the name more generic so more checkers can be added in it.
const Token * parent = tok->astParent(); | ||
if (parent && (Token::simpleMatch(parent->previous(), "if (") || | ||
Token::simpleMatch(parent->previous(), "while ("))) { | ||
assertWithSuspiciousCommaError(tok); |
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.
it looks strange to me that assertWithSuspiciousComma
is reported when there is no assert.
parent = parent->astParent(); | ||
if (Token::simpleMatch(parent->previous(), "for (")) { | ||
if (tok->index() < tok->astParent()->index()) { | ||
assertWithSuspiciousCommaError(tok); |
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 have the feeling this could be noisy. Maybe you want to increment two variables in the third expression and then a comma seems natural.
the PR description says that you want to detect:
well yes that looks like a possible bug. there seems to be misplaced parenthesis but I fear your checker is much more verbose. you warn whenever there is a comma in a if/while. no matter if there are function calls. If the condition says:
I agree the code looks weird.. but it's not a likely misplaced parenthesis and I am not convinced that the comma is a bug. It looks intentional. |
I also want to point out that misra has a rule that forbids comma operators. But I still feel sympathy for this checker if we make it more specific and it can detect actual bugs and not just warn about random comma operators. |
https://trac.cppcheck.net/ticket/5591
Description:
int f2(int, int = 0) {....}
void f()
{
if(f2(0), 0) //actually mean f2(0, 0)
{
....
}
}