Menu

[Suggest improvement] Improve the error id [nullPointerRedundantCheck]

Tuan
2024-08-16
2024-08-17
  • Tuan

    Tuan - 2024-08-16

    Dear dev teams,

    I understand the awesomeness that cppcheck hardly checks syntax, instead infers type and analysis
    Let's see following example:
    Use case: In both functions, it checks nullptr first, does other work and then accesses the pointer without checking nullptr
    Result: cppcheck succeeds in checking warnings in function f5, but FAILs in f4
    => seems like cppcheck cannot infer return type of func() as pointer from 2 expressions: compare vs nullptr and access operator -> later // <=== Can this be improved?

    cppcheck version: Cppcheck 2.14.1

    struct A {
        x;
        A() : x{1} {}
    }
    
    A* func() {
        return new A();
    }
    
    void f4() {
        if(func() != nullptr) {
            std::cout << "not nullptr";
        }
        // do others work
        std::cout << func()->x;
    }
    
    void f5(A *ptr) {
        if(ptr != nullptr) {
            std::cout << "not nullptr";
        }
        // do others work
        std::cout << ptr->x;
    }
    
     
  • CHR

    CHR - 2024-08-17

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

     

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.