For example, when distinct distance and speed constants both equal 1.0. Cppcheck flags their intentional division.
1.0
Save as equal-constants-minimal.cpp:
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
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.
equal-constants-minimal.cpp:3:48: warning: Same expression on both sides of '/' because 'DISTANCE_M' and 'SPEED_MPS' represent the same value. [duplicateExpression]
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15157
Log in to post a comment.
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:Run with Cppcheck 2.22.0:
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
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15157