-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix #10305 & #10699: FP unusedStructMember with aggregate initialization #6797
base: main
Are you sure you want to change the base?
Conversation
10699 Can be added to title as it's pretty much the same. |
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.
.
lib/checkunusedvar.cpp
Outdated
@@ -1534,7 +1534,14 @@ void CheckUnusedVar::checkStructMemberUsage() | |||
if (bailout) | |||
continue; | |||
|
|||
// Keep track of number of non-static members encountered so far | |||
int nummembs = 0; |
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.
naming.. I like someName
use a capital letter for first letter in each word. And I prefer full name. "membs" is much harder to read than "members" and you only save 2 characters.
lib/checkunusedvar.cpp
Outdated
@@ -1544,6 +1551,22 @@ void CheckUnusedVar::checkStructMemberUsage() | |||
// Check if the struct member variable is used anywhere in the file | |||
bool use = false; | |||
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { | |||
if (Token::Match(tok, "%name% {") && !Token::Match(tok->previous(), "class|struct") && tok->str() == scope.className && tok->linkAt(1)) { |
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 appreciate that you take nothing for granted but it is redundant to check if a {
has a link. Cppcheck will output a syntax error if there is no corresponding }
in the source code.
test/testunusedvar.cpp
Outdated
checkStructMemberUsage("struct S {\n" | ||
" const int* p{};\n" | ||
"};\n" | ||
"struct C {\n" |
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 understand the point to have struct C. a function is less code.
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 think I tried to imititate the code in the ticket, but I agree it's unnecessary.
In my humble opinion here is a better test case. There should be no "unusedStructMember" warning here:
|
I realize know that this doesn't actually solve the main in example in #10305 (only the simpler one in the comments), since it relies on the name of the struct appearing before the braced initalizer. So this will require some more work. |
Compares number of non-static members with number of entries in braced initializer.