-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix #13773 (False positive: should not warn when different ternary 2nd and 3rd expressions happens to have same value) #7922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+182
−24
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1e21c8d
Fix #13773 (False positive: should not warn when different ternary 2n…
danmar 69ee88e
Add documentation
danmar 2b5b81e
man
danmar a83ca7a
Motivation [ci skip]
danmar a73f897
runformat
danmar 05c1ff6
Update man/checkers/duplicateExpressionTernary.md [ci skip]
danmar e8175e6
Update man/checkers/duplicateValueTernary.md [ci skip]
danmar 27f8b64
copilot review [ci skip]
danmar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # duplicateExpressionTernary | ||
|
|
||
| **Message**: Same expression in both branches of ternary operator<br/> | ||
| **Category**: Logic<br/> | ||
| **Severity**: Style<br/> | ||
| **Language**: C/C++ | ||
|
|
||
| ## Description | ||
|
|
||
| This checker detects when syntactically identical expressions appear in both the true and false branches of a ternary operator (`condition ? true_expr : false_expr`). | ||
|
|
||
| The warning is triggered when: | ||
| - The second and third operands of a ternary operator are syntactically identical expressions | ||
| - This includes cases where variables are referenced through aliases that resolve to the same expression | ||
|
|
||
| ## Motivation | ||
|
|
||
| The same expressions indicates that there might be some logic error or copy paste mistake. | ||
|
danmar marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Examples | ||
|
|
||
| ### Problematic code | ||
|
|
||
| ```cpp | ||
| // Same expression in both branches | ||
| int result = condition ? x : x; // Warning: duplicateExpressionTernary | ||
|
|
||
| // Same variable referenced through alias | ||
| const int c = a; | ||
| int result = condition ? a : c; // Warning: duplicateExpressionTernary | ||
| ``` | ||
|
|
||
| ### Fixed code | ||
|
|
||
| ```cpp | ||
| // Different expressions in branches | ||
| int result = condition ? x : y; // OK | ||
| ``` | ||
|
|
||
| ## How to fix | ||
|
|
||
| 1. **Check for copy-paste errors**: Verify that both branches are supposed to have the same expression | ||
| 2. **Use different expressions**: If the branches should differ, update one of them | ||
| 3. **Simplify the code**: If both branches are intentionally the same, remove the ternary operator entirely | ||
|
|
||
| Before: | ||
| ```cpp | ||
| int getValue(bool flag) { | ||
| return flag ? computeValue() : computeValue(); // Same computation in both branches | ||
| } | ||
| ``` | ||
|
|
||
| After: | ||
| ```cpp | ||
| int getValue(bool flag) { | ||
| return computeValue(); // Simplified - condition doesn't matter | ||
| } | ||
| ``` | ||
|
|
||
| Or if the branches should differ: | ||
| ```cpp | ||
| int getValue(bool flag) { | ||
| return flag ? computeValue() : computeAlternativeValue(); // Different computations | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # duplicateValueTernary | ||
|
|
||
| **Message**: Same value in both branches of ternary operator<br/> | ||
| **Category**: Logic<br/> | ||
| **Severity**: Style<br/> | ||
| **Language**: C/C++ | ||
|
|
||
| ## Description | ||
|
|
||
| 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: | ||
| - The second and third operands of a ternary operator evaluate to the same constant value | ||
| - The expressions are constant statements that don't change during evaluation | ||
| - The expressions have different syntax but equivalent values | ||
|
|
||
| However, no warning is generated when: | ||
| - The expressions have different values on different platforms | ||
| - Variables are modified between evaluations | ||
| - The expressions involve non-constant computations | ||
|
|
||
| ## Motivation | ||
|
|
||
| The same value indicates that there might be some logic error or copy paste mistake. | ||
|
danmar marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Examples | ||
|
|
||
| ### Problematic code | ||
|
|
||
| ```cpp | ||
| // 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 | ||
| ``` | ||
|
|
||
| ### Fixed code | ||
|
|
||
| ```cpp | ||
| // 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 | ||
| ``` | ||
|
|
||
| ## How to fix | ||
|
|
||
| 1. **Check for logic errors**: Verify if both branches should actually have the same value | ||
| 2. **Simplify the code**: If both branches always have the same value, remove the ternary operator | ||
| 3. **Use different values**: If the branches should differ, update one of them | ||
| 4. **Remove unnecessary casts**: If the difference is only in casting, consider if the cast is needed | ||
|
|
||
| Before: | ||
| ```cpp | ||
| int getValue(bool flag) { | ||
| return flag ? (int)42 : 42; // Same value, different expressions | ||
| } | ||
| ``` | ||
|
|
||
| After (simplified): | ||
| ```cpp | ||
| int getValue(bool flag) { | ||
| return 42; // Simplified - condition doesn't affect result | ||
| } | ||
| ``` | ||
|
|
||
| Or (if branches should differ): | ||
| ```cpp | ||
| int getValue(bool flag) { | ||
| return flag ? 42 : 0; // Different values for different conditions | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.