Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

clock999
Copy link
Contributor

https://trac.cppcheck.net/ticket/5591

Description:
int f2(int, int = 0) {....}

void f()
{
if(f2(0), 0) //actually mean f2(0, 0)
{
....
}
}

@clock999 clock999 force-pushed the wy_dev_5591 branch 9 times, most recently from 53083da to 1687100 Compare March 26, 2025 14:04
@chrchr-github
Copy link
Collaborator

Thanks for your contribution. Note that these issues seem to be diagnosed already by current compilers.

@clock999
Copy link
Contributor Author

clock999 commented Mar 27, 2025

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!

@chrchr-github
Copy link
Collaborator

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 \
Copy link
Owner

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.

Copy link
Owner

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);
Copy link
Owner

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);
Copy link
Owner

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.

@danmar
Copy link
Owner

danmar commented Apr 4, 2025

the PR description says that you want to detect:

if(f2(0), 0) //actually mean f2(0, 0)

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:

    if (x,y)

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.

@danmar
Copy link
Owner

danmar commented Apr 4, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants