-
Notifications
You must be signed in to change notification settings - Fork 76
Move M5-14-1 implementation into shared query with Rule 8.14.1, increase test coverage. #1050
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
mbaluda
merged 8 commits into
main
from
michaelrfairhurst/side-effects-5-rule-8-14-1-rhs-of-and-and-should-have-no-side-effects
Mar 3, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b0b74e
Move M5-14-1 implementation into shared query with Rule 8.14.1, incre…
MichaelRFairhurst df1a5f2
Format cpp tests
MichaelRFairhurst c3af72c
Commit rules.csv changes
MichaelRFairhurst 0822b06
Fix locally different clang-format output compared to CI/CD
MichaelRFairhurst 2f1e920
Fix metadata inconsistency
MichaelRFairhurst 823fbf1
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/sid…
MichaelRFairhurst 854ad4a
Update change_notes/2026-02-21-share-implementation-of-m5-14-1.md
MichaelRFairhurst 3ada8df
Copilot suggestion: rename testref
MichaelRFairhurst 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - `M5-14-1` - `RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql`: | ||
| - Implementation has been refactored to share logic with Rule 8.14.1. No observable changes to results expected. |
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
4 changes: 0 additions & 4 deletions
4
...osar/test/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.expected
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...autosar/test/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.qlref
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...tosar/test/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.testref
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 @@ | ||
| cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql | ||
This file was deleted.
Oops, something went wrong.
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
26 changes: 26 additions & 0 deletions
26
cpp/common/src/codingstandards/cpp/exclusions/cpp/SideEffects5.qll
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,26 @@ | ||
| //** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
| import cpp | ||
| import RuleMetadata | ||
| import codingstandards.cpp.exclusions.RuleMetadata | ||
|
|
||
| newtype SideEffects5Query = TShortCircuitedPersistentSideEffectQuery() | ||
|
|
||
| predicate isSideEffects5QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `shortCircuitedPersistentSideEffect` query | ||
| SideEffects5Package::shortCircuitedPersistentSideEffectQuery() and | ||
| queryId = | ||
| // `@id` for the `shortCircuitedPersistentSideEffect` query | ||
| "cpp/misra/short-circuited-persistent-side-effect" and | ||
| ruleId = "RULE-8-14-1" and | ||
| category = "advisory" | ||
| } | ||
|
|
||
| module SideEffects5Package { | ||
| Query shortCircuitedPersistentSideEffectQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `shortCircuitedPersistentSideEffect` query | ||
| TQueryCPP(TSideEffects5PackageQuery(TShortCircuitedPersistentSideEffectQuery())) | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...les/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.qll
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,32 @@ | ||
| /** | ||
| * Provides a configurable module ShortCircuitedPersistentSideEffectShared with a `problems` predicate | ||
| * for the following issue: | ||
| * The right-hand operand of a logical && or || operator should not contain persistent | ||
| * side effects, as this may violate developer intent and expectations. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.SideEffect | ||
| import codingstandards.cpp.sideeffect.DefaultEffects | ||
| import codingstandards.cpp.Expr | ||
|
|
||
| signature module ShortCircuitedPersistentSideEffectSharedConfigSig { | ||
| Query getQuery(); | ||
| } | ||
|
|
||
| module ShortCircuitedPersistentSideEffectShared< | ||
| ShortCircuitedPersistentSideEffectSharedConfigSig Config> | ||
| { | ||
| query predicate problems( | ||
| BinaryLogicalOperation op, string message, Expr rhs, string rhsDescription | ||
| ) { | ||
| not isExcluded(op, Config::getQuery()) and | ||
| rhs = op.getRightOperand() and | ||
| hasSideEffect(rhs) and | ||
| not rhs instanceof UnevaluatedExprExtension and | ||
| message = "The $@ may have a side effect that is not always evaluated." and | ||
| rhsDescription = "right-hand operand" | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
...hortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.expected
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,7 @@ | ||
| | test.cpp:20:7:20:14 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:20:12:20:14 | ... ++ | right-hand operand | | ||
| | test.cpp:23:7:23:21 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:23:13:23:20 | ... == ... | right-hand operand | | ||
| | test.cpp:26:7:26:15 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:26:12:26:13 | call to f1 | right-hand operand | | ||
| | test.cpp:29:7:29:16 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:29:12:29:13 | call to f3 | right-hand operand | | ||
| | test.cpp:45:7:45:41 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:45:26:45:26 | call to operator== | right-hand operand | | ||
| | test.cpp:56:7:56:15 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:56:12:56:13 | call to f8 | right-hand operand | | ||
| | test.cpp:66:7:66:16 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:66:12:66:14 | call to f10 | right-hand operand | |
8 changes: 8 additions & 0 deletions
8
...ules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql
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,8 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.shortcircuitedpersistentsideeffectshared.ShortCircuitedPersistentSideEffectShared | ||
|
|
||
| module TestFileConfig implements ShortCircuitedPersistentSideEffectSharedConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import ShortCircuitedPersistentSideEffectShared<TestFileConfig> |
68 changes: 68 additions & 0 deletions
68
cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/test.cpp
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,68 @@ | ||
| int i = 0; | ||
|
|
||
| bool f1() { | ||
| i++; | ||
| return i == 1; | ||
| } | ||
|
|
||
| bool f2() { | ||
| int i = 0; | ||
| return i++ == 1; | ||
| } | ||
|
|
||
| bool f3(int &i) { | ||
| i++; | ||
| return i == 1; | ||
| } | ||
|
|
||
| void f4(bool b) { | ||
| int j = 0; | ||
| if (b || i++) { // NON_COMPLIANT | ||
| } | ||
|
|
||
| if (b || (j == i++)) { // NON_COMPLIANT | ||
| } | ||
|
|
||
| if (b || f1()) { // NON_COMPLIANT, f1 has global side-effects | ||
| } | ||
|
|
||
| if (b || f3(j)) { // NON_COMPLIANT, f3 has local side-effects | ||
| } | ||
| } | ||
|
|
||
| int g1 = 0; | ||
| int f5() { return g1++; } | ||
| int f6() { return 1; } | ||
|
|
||
| #include <typeinfo> | ||
|
|
||
| void f7() { | ||
| if (1 && sizeof(f5())) { | ||
| } // COMPLIANT - sizeof operands not evaluated | ||
| if (1 &&noexcept(f5()) &&noexcept(f5())) { | ||
| } // COMPLIANT - noexcept operands not evaluated | ||
|
|
||
| if (1 || (typeid(f6()) == typeid(f5()))) { | ||
| } // NON_COMPLIANT - typeid operands not evaluated, but the ==operator is | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| bool f8() { | ||
| static int k = 0; | ||
| k++; | ||
| return k == 1; | ||
| } | ||
|
|
||
| void f9(bool b) { | ||
| if (b || f8()) { // NON_COMPLIANT, f8 has static side-effects | ||
| } | ||
| } | ||
|
|
||
| bool f10() { | ||
| volatile bool m = 0; | ||
| return m; | ||
| } | ||
|
|
||
| void f11(bool b) { | ||
| if (b || f10()) { // NON_COMPLIANT, f10 has volatile side-effects | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
cpp/misra/src/rules/RULE-8-14-1/ShortCircuitedPersistentSideEffect.ql
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,28 @@ | ||
| /** | ||
| * @id cpp/misra/short-circuited-persistent-side-effect | ||
| * @name RULE-8-14-1: The right-hand operand of a logical && or || operator should not contain persistent side effects | ||
| * @description The right-hand operand of a logical && or || operator should not contain persistent | ||
| * side effects, as such side effects will only conditionally occur, which may violate | ||
| * developer intent and/or expectations. | ||
| * @kind problem | ||
| * @precision high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-8-14-1 | ||
| * scope/system | ||
| * correctness | ||
| * readability | ||
| * external/misra/enforcement/undecidable | ||
| * external/misra/obligation/advisory | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.rules.shortcircuitedpersistentsideeffectshared.ShortCircuitedPersistentSideEffectShared | ||
|
|
||
| module ShortCircuitedPersistentSideEffectConfig implements | ||
| ShortCircuitedPersistentSideEffectSharedConfigSig | ||
| { | ||
| Query getQuery() { result = SideEffects5Package::shortCircuitedPersistentSideEffectQuery() } | ||
| } | ||
|
|
||
| import ShortCircuitedPersistentSideEffectShared<ShortCircuitedPersistentSideEffectConfig> |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-8-14-1/ShortCircuitedPersistentSideEffect.testref
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 @@ | ||
| cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql |
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,27 @@ | ||
| { | ||
| "MISRA-C++-2023": { | ||
| "RULE-8-14-1": { | ||
| "properties": { | ||
| "enforcement": "undecidable", | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "obligation": "advisory" | ||
| }, | ||
| "queries": [ | ||
| { | ||
| "description": "The right-hand operand of a logical && or || operator should not contain persistent side effects, as such side effects will only conditionally occur, which may violate developer intent and/or expectations.", | ||
| "kind": "problem", | ||
| "name": "The right-hand operand of a logical && or || operator should not contain persistent side effects", | ||
| "precision": "high", | ||
| "severity": "error", | ||
| "short_name": "ShortCircuitedPersistentSideEffect", | ||
| "shared_implementation_short_name": "ShortCircuitedPersistentSideEffectShared", | ||
| "tags": [ | ||
| "scope/system", | ||
| "correctness", | ||
| "readability" | ||
| ] | ||
| } | ||
| ], | ||
| "title": "The right-hand operand of a logical && or || operator should not contain persistent side effects" | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.