Skip to content

Commit b5a2487

Browse files
authored
Merge pull request #1050 from github/michaelrfairhurst/side-effects-5-rule-8-14-1-rhs-of-and-and-should-have-no-side-effects
Move M5-14-1 implementation into shared query with Rule 8.14.1, increase test coverage.
2 parents 68892e6 + 3ada8df commit b5a2487

17 files changed

+215
-59
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M5-14-1` - `RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql`:
2+
- Implementation has been refactored to share logic with Rule 8.14.1. No observable changes to results expected.

cpp/autosar/src/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616

1717
import cpp
1818
import codingstandards.cpp.autosar
19-
import codingstandards.cpp.SideEffect
20-
import codingstandards.cpp.sideeffect.DefaultEffects
21-
import codingstandards.cpp.Expr
19+
import codingstandards.cpp.rules.shortcircuitedpersistentsideeffectshared.ShortCircuitedPersistentSideEffectShared
2220

23-
from BinaryLogicalOperation op, Expr rhs
24-
where
25-
not isExcluded(op,
26-
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
27-
rhs = op.getRightOperand() and
28-
hasSideEffect(rhs) and
29-
not rhs instanceof UnevaluatedExprExtension
30-
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"
21+
module RightHandOperandOfALogicalAndOperatorsContainSideEffectsConfig implements
22+
ShortCircuitedPersistentSideEffectSharedConfigSig
23+
{
24+
Query getQuery() {
25+
result = SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()
26+
}
27+
}
28+
29+
import ShortCircuitedPersistentSideEffectShared<RightHandOperandOfALogicalAndOperatorsContainSideEffectsConfig>

cpp/autosar/test/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.expected

Lines changed: 0 additions & 4 deletions
This file was deleted.

cpp/autosar/test/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql

cpp/autosar/test/rules/M5-14-1/test.cpp

Lines changed: 0 additions & 42 deletions
This file was deleted.

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import Scope
7272
import SideEffects1
7373
import SideEffects2
7474
import SideEffects4
75+
import SideEffects5
7576
import SmartPointers1
7677
import SmartPointers2
7778
import Statements
@@ -156,6 +157,7 @@ newtype TCPPQuery =
156157
TSideEffects1PackageQuery(SideEffects1Query q) or
157158
TSideEffects2PackageQuery(SideEffects2Query q) or
158159
TSideEffects4PackageQuery(SideEffects4Query q) or
160+
TSideEffects5PackageQuery(SideEffects5Query q) or
159161
TSmartPointers1PackageQuery(SmartPointers1Query q) or
160162
TSmartPointers2PackageQuery(SmartPointers2Query q) or
161163
TStatementsPackageQuery(StatementsQuery q) or
@@ -240,6 +242,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
240242
isSideEffects1QueryMetadata(query, queryId, ruleId, category) or
241243
isSideEffects2QueryMetadata(query, queryId, ruleId, category) or
242244
isSideEffects4QueryMetadata(query, queryId, ruleId, category) or
245+
isSideEffects5QueryMetadata(query, queryId, ruleId, category) or
243246
isSmartPointers1QueryMetadata(query, queryId, ruleId, category) or
244247
isSmartPointers2QueryMetadata(query, queryId, ruleId, category) or
245248
isStatementsQueryMetadata(query, queryId, ruleId, category) or
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype SideEffects5Query = TShortCircuitedPersistentSideEffectQuery()
7+
8+
predicate isSideEffects5QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `shortCircuitedPersistentSideEffect` query
11+
SideEffects5Package::shortCircuitedPersistentSideEffectQuery() and
12+
queryId =
13+
// `@id` for the `shortCircuitedPersistentSideEffect` query
14+
"cpp/misra/short-circuited-persistent-side-effect" and
15+
ruleId = "RULE-8-14-1" and
16+
category = "advisory"
17+
}
18+
19+
module SideEffects5Package {
20+
Query shortCircuitedPersistentSideEffectQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `shortCircuitedPersistentSideEffect` query
24+
TQueryCPP(TSideEffects5PackageQuery(TShortCircuitedPersistentSideEffectQuery()))
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Provides a configurable module ShortCircuitedPersistentSideEffectShared with a `problems` predicate
3+
* for the following issue:
4+
* The right-hand operand of a logical && or || operator should not contain persistent
5+
* side effects, as this may violate developer intent and expectations.
6+
*/
7+
8+
import cpp
9+
import codingstandards.cpp.Customizations
10+
import codingstandards.cpp.Exclusions
11+
import codingstandards.cpp.SideEffect
12+
import codingstandards.cpp.sideeffect.DefaultEffects
13+
import codingstandards.cpp.Expr
14+
15+
signature module ShortCircuitedPersistentSideEffectSharedConfigSig {
16+
Query getQuery();
17+
}
18+
19+
module ShortCircuitedPersistentSideEffectShared<
20+
ShortCircuitedPersistentSideEffectSharedConfigSig Config>
21+
{
22+
query predicate problems(
23+
BinaryLogicalOperation op, string message, Expr rhs, string rhsDescription
24+
) {
25+
not isExcluded(op, Config::getQuery()) and
26+
rhs = op.getRightOperand() and
27+
hasSideEffect(rhs) and
28+
not rhs instanceof UnevaluatedExprExtension and
29+
message = "The $@ may have a side effect that is not always evaluated." and
30+
rhsDescription = "right-hand operand"
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| 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 |
2+
| 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 |
3+
| 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 |
4+
| 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 |
5+
| 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 |
6+
| 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 |
7+
| 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 |

0 commit comments

Comments
 (0)