Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2026-02-21-share-implementation-of-m5-14-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `M5-14-1` - `RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql`:
- Implementation has been refactor to share logic with Rule 8.14.1. No observable changes to results expected.
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo/grammar: "has been refactor" should be "has been refactored".

Suggested change
- Implementation has been refactor to share logic with Rule 8.14.1. No observable changes to results expected.
- Implementation has been refactored to share logic with Rule 8.14.1. No observable changes to results expected.

Copilot uses AI. Check for mistakes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.SideEffect
import codingstandards.cpp.sideeffect.DefaultEffects
import codingstandards.cpp.Expr
import codingstandards.cpp.rules.shortcircuitedpersistentsideeffectshared.ShortCircuitedPersistentSideEffectShared

from BinaryLogicalOperation op, Expr rhs
where
not isExcluded(op,
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
rhs = op.getRightOperand() and
hasSideEffect(rhs) and
not rhs instanceof UnevaluatedExprExtension
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"
module RightHandOperandOfALogicalAndOperatorsContainSideEffectsConfig implements
ShortCircuitedPersistentSideEffectSharedConfigSig
{
Query getQuery() {
result = SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()
}
}

import ShortCircuitedPersistentSideEffectShared<RightHandOperandOfALogicalAndOperatorsContainSideEffectsConfig>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .testref filename is expected to be <query short_name>.testref (see scripts/generate_rules/generate_package_files.py), but this file is named RightHandOperandOfALogicalAndOperators.testref while the query short name for M5-14-1 is RightHandOperandOfALogicalAndOperatorsContainSideEffects. Rename the file to match the query short name so the test runner/generator can discover it correctly.

Copilot uses AI. Check for mistakes.

This file was deleted.

This file was deleted.

42 changes: 0 additions & 42 deletions cpp/autosar/test/rules/M5-14-1/test.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import Representation
import Scope
import SideEffects1
import SideEffects2
import SideEffects5
import SmartPointers1
import SmartPointers2
import Statements
Expand Down Expand Up @@ -134,6 +135,7 @@ newtype TCPPQuery =
TScopePackageQuery(ScopeQuery q) or
TSideEffects1PackageQuery(SideEffects1Query q) or
TSideEffects2PackageQuery(SideEffects2Query q) or
TSideEffects5PackageQuery(SideEffects5Query q) or
TSmartPointers1PackageQuery(SmartPointers1Query q) or
TSmartPointers2PackageQuery(SmartPointers2Query q) or
TStatementsPackageQuery(StatementsQuery q) or
Expand Down Expand Up @@ -207,6 +209,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
isScopeQueryMetadata(query, queryId, ruleId, category) or
isSideEffects1QueryMetadata(query, queryId, ruleId, category) or
isSideEffects2QueryMetadata(query, queryId, ruleId, category) or
isSideEffects5QueryMetadata(query, queryId, ruleId, category) or
isSmartPointers1QueryMetadata(query, queryId, ruleId, category) or
isSmartPointers2QueryMetadata(query, queryId, ruleId, category) or
isStatementsQueryMetadata(query, queryId, ruleId, category) or
Expand Down
26 changes: 26 additions & 0 deletions cpp/common/src/codingstandards/cpp/exclusions/cpp/SideEffects5.qll
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()))
}
}
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"
}
}
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 |
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>
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
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is incomplete (ends at "but the ==operator is"). Either finish the sentence or remove it to avoid confusion when reading the test case.

Suggested change
} // NON_COMPLIANT - typeid operands not evaluated, but the ==operator is
} // NON_COMPLIANT - typeid operands are not evaluated, but the == operator is, so its operands are evaluated

Copilot uses AI. Check for mistakes.
}

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
}
}
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql
1 change: 1 addition & 0 deletions rule_packages/cpp/SideEffects1.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"precision": "high",
"severity": "warning",
"short_name": "RightHandOperandOfALogicalAndOperatorsContainSideEffects",
"shared_implementation_short_name": "ShortCircuitedPersistentSideEffectShared",
"tags": [
"correctness"
]
Expand Down
27 changes: 27 additions & 0 deletions rule_packages/cpp/SideEffects5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"MISRA-C++-2023": {
"RULE-8-14-1": {
"properties": {
"enforcement": "undecidable",
Comment on lines +1 to +5
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RULE-8-14-1 is mapped to package SideEffects3 in rules.csv (line 901), but this PR introduces a new package file SideEffects5.json for the rule. This mismatch will likely break package generation / rule metadata resolution. Align the package name by updating rules.csv to SideEffects5 (or, if SideEffects3 is intended, rename/regenerate the new rule package/exclusion modules accordingly).

Copilot uses AI. Check for mistakes.
"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"
}
}
}
2 changes: 1 addition & 1 deletion rules.csv
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ cpp,MISRA-C++-2023,RULE-8-3-2,Yes,Advisory,Decidable,Single Translation Unit,The
cpp,MISRA-C++-2023,RULE-8-7-1,Yes,Required,Undecidable,System,Pointer arithmetic shall not form an invalid pointer,ARR30-C,Memory1,Easy,
cpp,MISRA-C++-2023,RULE-8-7-2,Yes,Required,Undecidable,System,Subtraction between pointers shall only be applied to pointers that address elements of the same array,ARR36-C,Memory2,Easy,
cpp,MISRA-C++-2023,RULE-8-9-1,Yes,Required,Undecidable,System,"The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type, except where they point to elements of the same array",ARR36-C,Memory3,Easy,
cpp,MISRA-C++-2023,RULE-8-14-1,Yes,Advisory,Undecidable,System,The right-hand operand of a logical && or operator should not contain persistent side effects,"M5-14-1, RULE-13-5",SideEffects3,Medium,
cpp,MISRA-C++-2023,RULE-8-14-1,Yes,Advisory,Undecidable,System,The right-hand operand of a logical && or operator should not contain persistent side effects,"M5-14-1, RULE-13-5",SideEffects5,Medium,
cpp,MISRA-C++-2023,RULE-8-18-1,Yes,Mandatory,Undecidable,System,An object or subobject must not be copied to an overlapping object,"M0-2-1, RULE-19-1",Memory4,Hard,
cpp,MISRA-C++-2023,RULE-8-18-2,Yes,Advisory,Decidable,Single Translation Unit,The result of an assignment operator should not be used,RULE-13-4,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-8-19-1,Yes,Advisory,Decidable,Single Translation Unit,The comma operator should not be used,M5-18-1,ImportMisra23,Import,
Expand Down
Loading