Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 356040 - Invalid "Unused declaration of variable" warning
Summary: Invalid "Unused declaration of variable" warning
Status: RESOLVED FIXED
Alias: None
Product: CDT
Classification: Tools
Component: cdt-codan (show other bugs)
Version: 8.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 8.0.1   Edit
Assignee: Sergey Prigogin CLA
QA Contact: Elena Laskavaia CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-28 22:57 EDT by Sergey Prigogin CLA
Modified: 2011-08-31 10:18 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergey Prigogin CLA 2011-08-28 22:57:32 EDT
extern const int test_var[2]; // Invalid warning
const int test_var[] = { 0, 1 };
Comment 1 Sergey Prigogin CLA 2011-08-28 23:21:33 EDT
A simpler example:

extern const int test_var;
const int test_var = 0;
Comment 2 Sergey Prigogin CLA 2011-08-28 23:46:48 EDT
Fixed in master and cdt_8_0 > 20110828.
Comment 3 CDT Genie CLA 2011-08-29 00:23:01 EDT
*** 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
Comment 4 CDT Genie CLA 2011-08-29 00:23:06 EDT
*** 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
Comment 5 Andrew Gvozdev CLA 2011-08-30 23:46:32 EDT
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.
Comment 6 Sergey Prigogin CLA 2011-08-31 00:40:20 EDT
(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.
Comment 7 Andrew Gvozdev CLA 2011-08-31 01:08:45 EDT
(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.
Comment 8 Sergey Prigogin CLA 2011-08-31 01:55:44 EDT
(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?
Comment 9 Andrew Gvozdev CLA 2011-08-31 10:18:54 EDT
(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.