Skip to content

Commit 9074c47

Browse files
authored
Fix #15153 FP constParameterPointer with unused attribute (#8976)
1 parent 1926dff commit 9074c47

4 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2560,7 +2560,7 @@ void Variable::evaluate(const Settings& settings)
25602560
bool isContainer = false;
25612561
if (mNameToken) {
25622562
setFlag(fIsArray, arrayDimensions(settings, isContainer));
2563-
setFlag(fIsMaybeUnused, mNameToken->isAttributeMaybeUnused());
2563+
setFlag(fIsMaybeUnused, mNameToken->isAttributeMaybeUnused() || mNameToken->isAttributeUnused());
25642564
}
25652565

25662566

‎lib/tokenize.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9718,8 +9718,8 @@ static Token* getVariableTokenAfterAttributes(Token* tok) {
97189718
Token *vartok = nullptr;
97199719
Token *after = getTokenAfterAttributes(tok, true);
97209720

9721-
// check if after variable name
9722-
if (Token::Match(after, "[;={]")) {
9721+
// check if after variable or parameter name
9722+
if (Token::Match(after, "[;={,)]")) {
97239723
Token *prev = tok->previous();
97249724
while (Token::simpleMatch(prev, "]"))
97259725
prev = prev->link()->previous();

‎test/testother.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5039,6 +5039,11 @@ class TestOther : public TestFixture {
50395039
"};\n");
50405040
ASSERT_EQUALS("[test.cpp:2:17]: (style) Either there is a missing override/final keyword, or the parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
50415041
errout_str());
5042+
5043+
check("void f(int *p __attribute__((unused))) {}\n"
5044+
"void g(__attribute__((unused)) int *p) {}\n"
5045+
"void h(int *p) {}\n");
5046+
ASSERT_EQUALS("[test.cpp:3:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str());
50425047
}
50435048

50445049
void constArray() {

‎test/testunusedvar.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,11 @@ class TestUnusedVar : public TestFixture {
20682068
" int i3 __attribute__((unused)) {};\n"
20692069
"};\n");
20702070
ASSERT_EQUALS("", errout_str());
2071+
2072+
checkStructMemberUsage("struct S {\n"
2073+
" int a __attribute__((unused)), b;\n"
2074+
"};\n");
2075+
ASSERT_EQUALS("[test.cpp:2:36]: (style) struct member 'S::b' is never used. [unusedStructMember]\n", errout_str());
20712076
}
20722077

20732078
void structmember31() { // #14130
@@ -6015,6 +6020,12 @@ class TestUnusedVar : public TestFixture {
60156020
" char b[1][2] __attribute__((unused));\n"
60166021
"}\n");
60176022
ASSERT_EQUALS("", errout_str());
6023+
6024+
functionVariableUsage("int foo()\n"
6025+
"{\n"
6026+
" int x __attribute__((unused)), y;\n"
6027+
"}\n");
6028+
ASSERT_EQUALS("[test.cpp:3:36]: (style) Unused variable: y [unusedVariable]\n", errout_str());
60186029
}
60196030

60206031
void localvarFunction() {

0 commit comments

Comments
 (0)