You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Message**: Same expression in both branches of ternary operator<br/>
4
+
**Category**: Logic<br/>
5
+
**Severity**: Style<br/>
6
+
**Language**: C/C++
7
+
8
+
## Description
9
+
10
+
This checker detects when syntactically identical expressions appear in both the true and false branches of a ternary operator (`condition ? true_expr : false_expr`).
11
+
12
+
The warning is triggered when:
13
+
- The second and third operands of a ternary operator are syntactically identical expressions
14
+
- This includes cases where variables are referenced through aliases that resolve to the same expression
15
+
16
+
## Motivation
17
+
18
+
The same expression indicates that there might be some logic error or copy-paste mistake.
19
+
20
+
## Examples
21
+
22
+
### Problematic code
23
+
24
+
```cpp
25
+
// Same expression in both branches
26
+
int result = condition ? x : x; // Warning: duplicateExpressionTernary
27
+
28
+
// Same variable referenced through alias
29
+
constint c = a;
30
+
int result = condition ? a : c; // Warning: duplicateExpressionTernary
31
+
```
32
+
33
+
### Fixed code
34
+
35
+
```cpp
36
+
// Different expressions in branches
37
+
int result = condition ? x : y; // OK
38
+
```
39
+
40
+
## How to fix
41
+
42
+
1.**Check for copy-paste errors**: Verify that both branches are supposed to have the same expression
43
+
2.**Use different expressions**: If the branches should differ, update one of them
44
+
3.**Simplify the code**: If both branches are intentionally the same, remove the ternary operator entirely
45
+
46
+
Before:
47
+
```cpp
48
+
intgetValue(bool flag) {
49
+
return flag ? computeValue() : computeValue(); // Same computation in both branches
**Message**: Same value in both branches of ternary operator<br/>
4
+
**Category**: Logic<br/>
5
+
**Severity**: Style<br/>
6
+
**Language**: C/C++
7
+
8
+
## Description
9
+
10
+
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.
11
+
12
+
The warning is triggered when:
13
+
- The second and third operands of a ternary operator evaluate to the same constant value
14
+
- The expressions are constant statements that don't change during evaluation
15
+
- The expressions have different syntax but equivalent values
16
+
17
+
However, no warning is generated when:
18
+
- The expressions have different values on different platforms
19
+
- Variables are modified between evaluations
20
+
- The expressions involve non-constant computations
21
+
22
+
## Motivation
23
+
24
+
The same value indicates that there might be some logic error or copy-paste mistake.
25
+
26
+
## Examples
27
+
28
+
### Problematic code
29
+
30
+
```cpp
31
+
// Different expressions, same value
32
+
int result = condition ? (int)1 : 1; // Warning: duplicateValueTernary
33
+
34
+
// Different cast syntax, same value
35
+
int result = condition ? 1 : (int)1; // Warning: duplicateValueTernary
36
+
```
37
+
38
+
### Fixed code
39
+
40
+
```cpp
41
+
// Different values in branches
42
+
int result = condition ? 1 : 2; // OK
43
+
44
+
// Simplified - condition doesn't matter
45
+
int result = 1; // OK - removed unnecessary ternary
46
+
47
+
// Platform-dependent values are allowed
48
+
int size = is_64bit ? sizeof(long) : sizeof(int); // OK - may differ on platforms
49
+
```
50
+
51
+
## How to fix
52
+
53
+
1.**Check for logic errors**: Verify if both branches should actually have the same value
54
+
2.**Simplify the code**: If both branches always have the same value, remove the ternary operator
55
+
3.**Use different values**: If the branches should differ, update one of them
56
+
4.**Remove unnecessary casts**: If the difference is only in casting, consider if the cast is needed
57
+
58
+
Before:
59
+
```cpp
60
+
intgetValue(bool flag) {
61
+
return flag ? (int)42 : 42; // Same value, different expressions
62
+
}
63
+
```
64
+
65
+
After (simplified):
66
+
```cpp
67
+
int getValue(bool flag) {
68
+
return 42; // Simplified - condition doesn't affect result
69
+
}
70
+
```
71
+
72
+
Or (if branches should differ):
73
+
```cpp
74
+
intgetValue(bool flag) {
75
+
return flag ? 42 : 0; // Different values for different conditions
0 commit comments