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
Copy file name to clipboardExpand all lines: man/checkers/duplicateExpressionTernary.md
+5-37Lines changed: 5 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,19 +7,15 @@
7
7
8
8
## Description
9
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`). When this occurs, the result of the ternary operator is always the same regardless of the condition, which is usually a logic error or copy-paste mistake.
11
-
12
-
The key difference from `duplicateValueTernary` is:
13
-
-`duplicateExpressionTernary`: Triggered when expressions are syntactically **identical** (e.g., `x` and `x`, or `(int)1` and `(int)1`)
14
-
-`duplicateValueTernary`: Triggered when expressions have the same **value** but different syntax (e.g., `1` and `(int)1`)
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`).
15
11
16
12
The warning is triggered when:
17
13
- The second and third operands of a ternary operator are syntactically identical expressions
18
14
- This includes cases where variables are referenced through aliases that resolve to the same expression
19
15
20
-
However, no warning is generated when:
21
-
- The expressions have different values on different platforms (e.g., `sizeof(uint32_t)` vs `sizeof(unsigned int)` which may differ across platforms)
Copy file name to clipboardExpand all lines: man/checkers/duplicateValueTernary.md
+6-27Lines changed: 6 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,7 @@
7
7
8
8
## Description
9
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. This usually indicates a logic error where the condition serves no purpose since the result is always the same.
11
-
12
-
The key difference from `duplicateExpressionTernary` is:
13
-
-`duplicateValueTernary`: Triggered when expressions have the same **value** (e.g., `1` and `(int)1`)
14
-
-`duplicateExpressionTernary`: Triggered when expressions are syntactically **identical** (e.g., `x` and `x`)
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.
15
11
16
12
The warning is triggered when:
17
13
- The second and third operands of a ternary operator evaluate to the same constant value
@@ -23,6 +19,10 @@ However, no warning is generated when:
23
19
- Variables are modified between evaluations
24
20
- The expressions involve non-constant computations
25
21
22
+
## Why we warn
23
+
24
+
The same value indicates that there might be some logic error or copy paste mistake.
25
+
26
26
## Examples
27
27
28
28
### Problematic code
@@ -33,17 +33,6 @@ int result = condition ? (int)1 : 1; // Warning: duplicateValueTernary
33
33
34
34
// Different cast syntax, same value
35
35
int result = condition ? 1 : (int)1; // Warning: duplicateValueTernary
36
-
37
-
// Same constant value with different representations
38
-
int result = condition ? (int)1 : 1; // Warning: duplicateValueTernary
39
-
```
40
-
41
-
### Code that correctly triggers duplicateExpressionTernary instead
42
-
43
-
```cpp
44
-
// Identical expressions (not just values)
45
-
int result = condition ? 1 : 1; // Warning: duplicateExpressionTernary
46
-
int result = condition ? (int)1 : (int)1; // Warning: duplicateExpressionTernary
47
36
```
48
37
49
38
### Fixed code
@@ -59,16 +48,6 @@ int result = 1; // OK - removed unnecessary ternary
59
48
int size = is_64bit ? sizeof(long) : sizeof(int); // OK - may differ on platforms
60
49
```
61
50
62
-
### Exception: Platform-dependent values
63
-
64
-
```cpp
65
-
// This may NOT generate a warning if values differ across platforms
66
-
int size = condition ? sizeof(long) : sizeof(int); // OK on some platforms
67
-
68
-
// Special case: +0.0 vs -0.0 are considered different
69
-
double result = condition ? 0.0 : -0.0; // OK - different floating-point values
70
-
```
71
-
72
51
## How to fix
73
52
74
53
1.**Check for logic errors**: Verify if both branches should actually have the same value
@@ -95,4 +74,4 @@ Or (if branches should differ):
95
74
intgetValue(bool flag) {
96
75
return flag ? 42 : 0; // Different values for different conditions
0 commit comments