Menu

throw and unusedscopedobject

T Tanner
2023-08-10
2023-08-14
  • T Tanner

    T Tanner - 2023-08-10

    This sort of code:

    throw mynamespace::exceptionobject("message");
    

    produces an unusedScopedObject message. Why? The language requires you to write code like this. The fact that the exception object is moved into somewhere magic and then deleted isn't really important.

    Could this message be switched off for objects constructed and promptly thrown?

     
  • CHR

    CHR - 2023-08-11

    Which version are you using? Can you post a minimal example?

     
  • Daniel Marjamäki

    usually we should not warn if you throw the object.

    a minimal example would be nice.

    wild guess: is there an override of throw in some header:

    #define throw
    
     
  • cb blauzahn

    cb blauzahn - 2023-08-13

    I also observe in our rather large codebase many unusedScopedObject messages. They started to occur months ago.

    I bisected it in cppcheck trunk. It occurs since git commit:
    sha1 id: 4c1c506d2153c8719cbccd0487bfecdc45e8167a
    '#11472 FP return TempReference with cast and scope operator (#4676)'
    chrchr-github
    2023-01-02 22:13:56 CET

    It has nothing to do with throw. Rather it looks like it is triggered when we return calling e.g. a ctor fully qualified with leading ::

    A minimal example triggering the message:

    // cppcheck --enable=all fp_unusedScopedObject.cpp
    
    struct Widget { int i_; };
    
    namespace {
        Widget widget() { return ::Widget{42}; }
          // NOTE: qualifying with leading :: triggers
          // style: Instance of 'return::Widget' 
          // object is destroyed immediately. [unusedScopedObject]
    }
    
    int main()
    {
        Widget w = widget();
        return w.i_;
    }
    

    Hope this helps. Thanks in advance. I like cppcheck a lot. Kudos to Daniel Marjamäki and the other maintainers.

     
  • T Tanner

    T Tanner - 2023-08-13

    Sorry, not been able to check for a couple of days. I'm using cppcheck 2.10.3 (as provided by codacy)

    Looks like the previous poster is having the same problems as I am, and I omitted to include the :: in my original example, so

    throw ::mynamespace::exceptionobject("message");
    

    would be a better example. Possibly

    if (cond) 
    {
           throw ::mynamespace::exceptionobject("message"); 
    }
    

    might be necessary.

    Although having seen the prior post, does look like I needed the :: at the start

     

    Last edit: T Tanner 2023-08-13
  • CHR

    CHR - 2023-08-13

    Thanks for reporting, fixed by https://github.com/danmar/cppcheck/pull/5321

     
  • T Tanner

    T Tanner - 2023-08-14

    Thank you!

     
  • cb blauzahn

    cb blauzahn - 2023-08-14

    I can confirm this fix. Thank you.

    Caveat: The git-commit
    sha1 id: 53714556066c7d9d83eff0af046e26fcf6da56c5
    'optimized pipe writing/reading..'
    Oliver Stöneberg
    2023-08-13 11:52 CEST

    renders my cppcheck unusable when called from the codeblocks ide. I have reverted just that for now.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.