Skip to content
Snippets Groups Projects

Fix warnings unused but set variable

2 unresolved threads

I fixed all warnings "-Wunused-but-set-variable" by gcc.

I added the attribute "DUNE_UNUSED" to the variables to avoid the warnings. 

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
270 272 {
271 273 char buffer[VAR_ARG_BUFLEN];
272 274 va_list args;
273 int count;
275 DUNE_UNUSED int count;
  • But count is not unused. It is used in the assert a few lines below. Have a look at what assert actually is: count will be used when the preprocessor macro NDEBUG is not set. I suppose the correct fix here is to hide all uses of count behind #ifndef NDEBUG.

  • It seems that i misunderstood the meaning of DUNE_UNUSED. I used it to suppress compiler-warnings, that flag variables as "set but unused", but are not unused i.e. count in this case.

  • changed this line in version 2 of the diff

  • "unused" is a bit of a bad attribute name. It means "This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC does not produce a warning for this variable." (from GCC); note the "possibly" before "unused".

    The C++17 attribute has a better name: [[maybe_unused]]

    So it is fine to apply it to variables that are only used in some configurations (like when building without -DNDEBUG). Though often one can just create the variable in configurations where it is also used; I find this the better solution.

  • Please register or sign in to reply
  • No, you didn't misinterpret DNUE_UNUSED, but you misread the count variable. It is used, but only if you don't disable debugging using -DNDEBUG. Thus you can't mark it as unused, as it is used in certain configurations, but must hide it if NDEBUG is defined.

  • Now i see my mistake. That's the reason this warning only occurred in test "gcc-8-noassert". Thank you.

  • added 1 commit

    • 9f22df14 - Fixed the warning regarding "count"

    Compare with previous version

  • 270 270 {
    271 271 char buffer[VAR_ARG_BUFLEN];
    272 272 va_list args;
    273 int count;
    274 273
    275 274 /* initialize args */
    276 275 va_start(args,format);
    277
    276 #ifndef NDEBUG
    277 int count;
    278 278 count = vsprintf(buffer,format,args);
  • added 1 commit

    • e6069eda - Defined and initialized "count" in one line

    Compare with previous version

  • mentioned in commit 958ae1aa

  • Please register or sign in to reply
    Loading