man/checkers/duplicateValueTernary.md
Message: Same value in both branches of ternary operator
Category: Logic
Severity: Style
Language: C/C++
This checker detects when both branches of a ternary operator (condition ? true_expr : false_expr) evaluate to the same value, even though the expressions themselves may be syntactically different.
The warning is triggered when:
However, no warning is generated when:
The same value indicates that there might be some logic error or copy-paste mistake.
// Different expressions, same value
int result = condition ? (int)1 : 1; // Warning: duplicateValueTernary
// Different cast syntax, same value
int result = condition ? 1 : (int)1; // Warning: duplicateValueTernary
// Different values in branches
int result = condition ? 1 : 2; // OK
// Simplified - condition doesn't matter
int result = 1; // OK - removed unnecessary ternary
// Platform-dependent values are allowed
int size = is_64bit ? sizeof(long) : sizeof(int); // OK - may differ on platforms
Before:
int getValue(bool flag) {
return flag ? (int)42 : 42; // Same value, different expressions
}
After (simplified):
int getValue(bool flag) {
return 42; // Simplified - condition doesn't affect result
}
Or (if branches should differ):
int getValue(bool flag) {
return flag ? 42 : 0; // Different values for different conditions
}