Menu

danglingTemporaryLifetime False positive

Julien
2022-01-04
2022-01-05
  • Julien

    Julien - 2022-01-04

    Hello,

    Thanks a lot for your awesome project. I think I've found a false positive for danglingTemporaryLifetime warning:

    #include <iostream>
    #include <vector>
    
    std::vector<int> ints = {0, 1, 2, 3};
    
    int main() {
      auto *ptr = &ints;
      if (rand() / 2 == 0) {
        ptr = &ints;
      }
      for (auto it = ptr->cbegin(); it != ptr->cend(); ++it) { // Using iterator to temporary.
        std::cout << *it << "\n";                               // Using iterator to temporary.
      }
      return 0;
    }
    

    Removing the if block removes the warning. Changing the condition to something more predictable (e.g. true) removes the warning. I'm not sure why such code should trigger a warning.

    Thanks again for your project.

     

    Last edit: Julien 2022-01-04
  • CHR

    CHR - 2022-01-05

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/10683

     

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.