diff --git a/cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode.qll b/cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode.qll index 40b8795e5e..a779eaadfa 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode.qll @@ -19,7 +19,8 @@ newtype DeadCodeQuery = TSingleUseLocalPODVariableQuery() or TSingleUseGlobalOrNamespacePODVariableQuery() or TSingleUseMemberPODVariableQuery() or - TDeadCodeQuery() + TDeadCodeQuery() or + TFunctionContainUnreachableStatementsQuery() predicate isDeadCodeQueryMetadata(Query query, string queryId, string ruleId, string category) { query = @@ -165,6 +166,15 @@ predicate isDeadCodeQueryMetadata(Query query, string queryId, string ruleId, st "cpp/autosar/dead-code" and ruleId = "M0-1-9" and category = "required" + or + query = + // `Query` instance for the `functionContainUnreachableStatements` query + DeadCodePackage::functionContainUnreachableStatementsQuery() and + queryId = + // `@id` for the `functionContainUnreachableStatements` query + "cpp/misra/function-contain-unreachable-statements" and + ruleId = "RULE-0-0-1" and + category = "required" } module DeadCodePackage { @@ -279,4 +289,11 @@ module DeadCodePackage { // `Query` type for `deadCode` query TQueryCPP(TDeadCodePackageQuery(TDeadCodeQuery())) } + + Query functionContainUnreachableStatementsQuery() { + //autogenerate `Query` type + result = + // `Query` type for `functionContainUnreachableStatements` query + TQueryCPP(TDeadCodePackageQuery(TFunctionContainUnreachableStatementsQuery())) + } } diff --git a/cpp/misra/src/rules/RULE-0-0-1/FunctionContainUnreachableStatements.ql b/cpp/misra/src/rules/RULE-0-0-1/FunctionContainUnreachableStatements.ql new file mode 100644 index 0000000000..077945e939 --- /dev/null +++ b/cpp/misra/src/rules/RULE-0-0-1/FunctionContainUnreachableStatements.ql @@ -0,0 +1,21 @@ +/** + * @id cpp/misra/function-contain-unreachable-statements + * @name RULE-0-0-1: A function shall not contain unreachable statements + * @description Unreachable statements can indicate a mistake on the part of the programmer. + * @kind problem + * @precision high + * @problem.severity error + * @tags external/misra/id/rule-0-0-1 + * scope/single-translation-unit + * readability + * maintainability + * external/misra/enforcement/decidable + * external/misra/obligation/required + */ + +import cpp +import codingstandards.cpp.misra + +from Expr x +where not isExcluded(x, DeadCodePackage::functionContainUnreachableStatementsQuery()) +select x, "none" diff --git a/cpp/misra/test/rules/RULE-0-0-1/FunctionContainUnreachableStatements.expected b/cpp/misra/test/rules/RULE-0-0-1/FunctionContainUnreachableStatements.expected new file mode 100644 index 0000000000..3b7631d687 --- /dev/null +++ b/cpp/misra/test/rules/RULE-0-0-1/FunctionContainUnreachableStatements.expected @@ -0,0 +1 @@ +| test.cpp:1:26:1:28 | arg | none | diff --git a/cpp/misra/test/rules/RULE-0-0-1/FunctionContainUnreachableStatements.qlref b/cpp/misra/test/rules/RULE-0-0-1/FunctionContainUnreachableStatements.qlref new file mode 100644 index 0000000000..f1d07e9f1b --- /dev/null +++ b/cpp/misra/test/rules/RULE-0-0-1/FunctionContainUnreachableStatements.qlref @@ -0,0 +1 @@ +rules/RULE-0-0-1/FunctionContainUnreachableStatements.ql \ No newline at end of file diff --git a/cpp/misra/test/rules/RULE-0-0-1/test.cpp b/cpp/misra/test/rules/RULE-0-0-1/test.cpp new file mode 100644 index 0000000000..89841c4a31 --- /dev/null +++ b/cpp/misra/test/rules/RULE-0-0-1/test.cpp @@ -0,0 +1 @@ +int f1(int arg) { return arg; } diff --git a/rule_packages/cpp/DeadCode.json b/rule_packages/cpp/DeadCode.json index 7eb5c9f6f9..f55228bf83 100644 --- a/rule_packages/cpp/DeadCode.json +++ b/rule_packages/cpp/DeadCode.json @@ -349,5 +349,29 @@ ], "title": "There shall be no dead code." } + }, + "MISRA-C++-2023": { + "RULE-0-0-1": { + "properties": { + "enforcement": "decidable", + "obligation": "required" + }, + "queries": [ + { + "description": "Unreachable statements can indicate a mistake on the part of the programmer.", + "kind": "problem", + "name": "A function shall not contain unreachable statements", + "precision": "high", + "severity": "error", + "short_name": "FunctionContainUnreachableStatements", + "tags": [ + "scope/single-translation-unit", + "readability", + "maintainability" + ] + } + ], + "title": "A function shall not contain unreachable statements" + } } -} +} \ No newline at end of file diff --git a/rules.csv b/rules.csv index 913aa27282..2fcf32c915 100644 --- a/rules.csv +++ b/rules.csv @@ -774,7 +774,7 @@ c,MISRA-C-2012,RULE-22-7,Yes,Required,,,The macro EOF shall only be compared wit c,MISRA-C-2012,RULE-22-8,Yes,Required,,,The value of errno shall be set to zero prior to a call to an errno-setting-function,ERR30-C,Contracts3,Medium, c,MISRA-C-2012,RULE-22-9,Yes,Required,,,The value of errno shall be tested against zero after calling an errno-setting-function,,Contracts3,Medium, c,MISRA-C-2012,RULE-22-10,Yes,Required,,,The value of errno shall only be tested when the last function to be called was an errno-setting-function,,Contracts3,Medium, -cpp,MISRA-C++-2023,RULE-0-0-1,Yes,Required,Decidable,Single Translation Unit,A function shall not contain unreachable statements,,,Medium, +cpp,MISRA-C++-2023,RULE-0-0-1,Yes,Required,Decidable,Single Translation Unit,A function shall not contain unreachable statements,,DeadCode,Medium, cpp,MISRA-C++-2023,RULE-0-0-2,Yes,Advisory,Undecidable,System,Controlling expressions should not be invariant,,,Easy, cpp,MISRA-C++-2023,RULE-0-1-1,Yes,Advisory,Undecidable,System,A value should not be unnecessarily written to a local object,,,Medium, cpp,MISRA-C++-2023,RULE-0-1-2,Yes,Required,Decidable,Single Translation Unit,The value returned by a function shall be used,,,Easy,