Menu ▾ ▴

duplicateExpression false positive for equal-valued dimensional constants

Long Huang
2026-10-29
2026-10-30
  • Long Huang

    Long Huang - 2026-10-29

    For example, when distinct distance and speed constants both equal 1.0. Cppcheck flags their intentional division.

    Minimal reproduction

    Save as equal-constants-minimal.cpp:

    constexpr double DISTANCE_M = 1.0;
    constexpr double SPEED_MPS = 1.0;
    double TravelTimeSeconds() { return DISTANCE_M / SPEED_MPS; }
    

    Run with Cppcheck 2.22.0:

    cppcheck --enable=style --std=c++14 --template=gcc equal-constants-minimal.cpp
    

    Expected

    No duplicate-expression diagnostic solely because differently named constants have equal magnitudes. Distance divided by speed intentionally calculates time, the operands are not interchangeable physical quantities.

    Actual

    equal-constants-minimal.cpp:3:48: warning: Same expression on both sides of '/' because 'DISTANCE_M' and 'SPEED_MPS' represent the same value. [duplicateExpression]
    
     
  • CHR

    CHR - 2026-10-30

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

     

Log in to post a comment.