| Summary: | Invalid "Unused declaration of variable" warning | ||
|---|---|---|---|
| Product: | [Tools] CDT | Reporter: | Sergey Prigogin <eclipse.sprigogin> |
| Component: | cdt-codan | Assignee: | Sergey Prigogin <eclipse.sprigogin> |
| Status: | RESOLVED FIXED | QA Contact: | Elena Laskavaia <elaskavaia.cdt> |
| Severity: | normal | ||
| Priority: | P3 | CC: | cdtdoug, malaperle |
| Version: | 8.0 | ||
| Target Milestone: | 8.0.1 | ||
| Hardware: | All | ||
| OS: | All | ||
| Whiteboard: | |||
A simpler example: extern const int test_var; const int test_var = 0; Fixed in master and cdt_8_0 > 20110828. *** cdt git genie on behalf of Sergey Prigogin ***
Bug 356040 - Invalid "Unused declaration of variable" warning.
[*] http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=e972ac9ea5fe05521931319398a752e95a7e2856
*** cdt git genie on behalf of Sergey Prigogin ***
Bug 356040 - Invalid "Unused declaration of variable" warning.
[*] http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=17f96f9eb3a4f54696e76b19bfb4cbe9ed0fe8fb
There is a difference in default linkage for const and non-const variables, i.e. default linkage for const is internal while for non-const it is extern. You are right that in your example the warning should not be there as the first declaration modifies default linkage: extern const int test_var; const int test_var = 0; However, following should produce the warning as the second declaration defines extern by default and so the first one is just a noise that can be omitted: extern int test_var; int test_var = 0; We should rather check for "const" to fix this bug properly. (In reply to comment #5) > However, following should produce the warning as the second declaration defines > extern by default and so the first one is just a noise that can be omitted: > extern int test_var; > int test_var = 0; Consider the following example: a.cc ---- int test_var = 1; b.cc ---- extern int test_var; void test() { printf("%d\n", test_var); } The definition of test_var in a.cc is used and should not produce a warning. (In reply to comment #6) > (In reply to comment #5) > > However, following should produce the warning as the second declaration > defines > > extern by default and so the first one is just a noise that can be omitted: > > extern int test_var; > > int test_var = 0; > > Consider the following example: > > a.cc > ---- > int test_var = 1; > > b.cc > ---- > extern int test_var; > > void test() { > printf("%d\n", test_var); > } > > The definition of test_var in a.cc is used and should not produce a warning. I agree. However it does not refute my point. In my example both statements are assumed to be in the same file. The checker only checks file scope. (In reply to comment #7) > I agree. However it does not refute my point. In my example both statements are > assumed to be in the same file. The checker only checks file scope. I see. Strictly speaking you are right. I interpreted the warning as "variable is declared but not used in the file". Should we have two separate warnings? (In reply to comment #8) > (In reply to comment #7) > > I agree. However it does not refute my point. In my example both statements > are > > assumed to be in the same file. The checker only checks file scope. > I see. Strictly speaking you are right. I interpreted the warning as "variable > is declared but not used in the file". Should we have two separate warnings? Well, it says "Unused declaration of variable". Technically it is correct, the declaration is unused. I agree that it is easy to confuse this message with "Unused variable" but I am kind of reluctant to add too many to the list of problems to "Code Analysis" preference page. It is going to be a big list over time. I think it is a good hint as is. You can just remove the declaration if you trust the checker or find the second one to see what's going on. |
extern const int test_var[2]; // Invalid warning const int test_var[] = { 0, 1 };