Skip to content

Commit 2b5b81e

Browse files
committed
man
1 parent 69ee88e commit 2b5b81e

2 files changed

Lines changed: 11 additions & 64 deletions

File tree

man/checkers/duplicateExpressionTernary.md

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@
77

88
## Description
99

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`).
1511

1612
The warning is triggered when:
1713
- The second and third operands of a ternary operator are syntactically identical expressions
1814
- This includes cases where variables are referenced through aliases that resolve to the same expression
1915

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)
22-
- The expressions involve platform-dependent behavior
16+
## Why we warn
17+
18+
The same expressions indicates that there might be some logic error or copy paste mistake.
2319

2420
## Examples
2521

@@ -32,41 +28,13 @@ int result = condition ? x : x; // Warning: duplicateExpressionTernary
3228
// Same variable referenced through alias
3329
const int c = a;
3430
int result = condition ? a : c; // Warning: duplicateExpressionTernary
35-
36-
// Identical expressions with same syntax
37-
int result = condition ? 1 : 1; // Warning: duplicateExpressionTernary
38-
int result = condition ? (int)1 : (int)1; // Warning: duplicateExpressionTernary
39-
```
40-
41-
### Code that triggers duplicateValueTernary instead
42-
43-
```cpp
44-
// Different syntax, same value (triggers duplicateValueTernary, not duplicateExpressionTernary)
45-
int result = condition ? 1 : (int)1; // Warning: duplicateValueTernary
46-
int result = condition ? (int)42 : 42; // Warning: duplicateValueTernary
4731
```
4832

4933
### Fixed code
5034

5135
```cpp
5236
// Different expressions in branches
5337
int result = condition ? x : y; // OK
54-
55-
// Platform-dependent sizes are allowed
56-
int size = is_32bit ? sizeof(uint32_t) : sizeof(unsigned int); // OK - may differ on platforms
57-
```
58-
59-
### Exception: Platform-dependent expressions
60-
61-
```cpp
62-
// This does NOT generate a warning because sizeof may differ across platforms
63-
typedef float _Complex complex_f32;
64-
typedef struct {
65-
uint16_t real;
66-
uint16_t imag;
67-
} packed_complex;
68-
69-
int size = condition ? sizeof(packed_complex) : sizeof(complex_f32); // OK
7038
```
7139

7240
## How to fix
@@ -94,4 +62,4 @@ Or if the branches should differ:
9462
int getValue(bool flag) {
9563
return flag ? computeValue() : computeAlternativeValue(); // Different computations
9664
}
97-
```
65+
```

man/checkers/duplicateValueTernary.md

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77

88
## Description
99

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.
1511

1612
The warning is triggered when:
1713
- 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:
2319
- Variables are modified between evaluations
2420
- The expressions involve non-constant computations
2521

22+
## Why we warn
23+
24+
The same value indicates that there might be some logic error or copy paste mistake.
25+
2626
## Examples
2727

2828
### Problematic code
@@ -33,17 +33,6 @@ int result = condition ? (int)1 : 1; // Warning: duplicateValueTernary
3333

3434
// Different cast syntax, same value
3535
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
4736
```
4837

4938
### Fixed code
@@ -59,16 +48,6 @@ int result = 1; // OK - removed unnecessary ternary
5948
int size = is_64bit ? sizeof(long) : sizeof(int); // OK - may differ on platforms
6049
```
6150

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-
7251
## How to fix
7352

7453
1. **Check for logic errors**: Verify if both branches should actually have the same value
@@ -95,4 +74,4 @@ Or (if branches should differ):
9574
int getValue(bool flag) {
9675
return flag ? 42 : 0; // Different values for different conditions
9776
}
98-
```
77+
```

0 commit comments

Comments
 (0)