-
Notifications
You must be signed in to change notification settings - Fork 74
Implement Rule 28-6-4, require use of std::remove_if and similar functions #1046
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
Open
MichaelRFairhurst
wants to merge
4
commits into
main
Choose a base branch
from
michaelrfairhurst/implement-deadcode-11-use-result-of-remove-algorithms-etc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
46cb4ff
Implement Rule 28-6-4, require use of std::remove_if and similar func…
MichaelRFairhurst 5d7db89
Regenerate ql query header
MichaelRFairhurst 54418e4
Copilot feedback
MichaelRFairhurst b44c675
Add default ctor to deque which was breaking tests.
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode11.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 DeadCode11Query = TPotentiallyErroneousContainerUsageQuery() | ||
|
|
||
| predicate isDeadCode11QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `potentiallyErroneousContainerUsage` query | ||
| DeadCode11Package::potentiallyErroneousContainerUsageQuery() and | ||
| queryId = | ||
| // `@id` for the `potentiallyErroneousContainerUsage` query | ||
| "cpp/misra/potentially-erroneous-container-usage" and | ||
| ruleId = "RULE-28-6-4" and | ||
| category = "required" | ||
| } | ||
|
|
||
| module DeadCode11Package { | ||
| Query potentiallyErroneousContainerUsageQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `potentiallyErroneousContainerUsage` query | ||
| TQueryCPP(TDeadCode11PackageQuery(TPotentiallyErroneousContainerUsageQuery())) | ||
| } | ||
| } |
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
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
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,21 @@ | ||
| #ifndef _GHLIBCPP_EMPTY | ||
| #define _GHLIBCPP_EMPTY | ||
| #include <initializer_list> | ||
| #include <stddef.h> | ||
| /** | ||
| * Not intended to be used via #include <empty.h>, and not part of the public | ||
| * API. | ||
| * | ||
| * However, multiple libraries such as <array>, <deque>, etc define std::empty, | ||
| * so this is the singular header to define it in the test suite. | ||
| */ | ||
|
|
||
| namespace std { | ||
| template <class C> constexpr auto empty(const C &c) -> decltype(c.empty()); | ||
|
|
||
| template <class T, size_t N> constexpr bool empty(const T (&)[N]) noexcept; | ||
|
|
||
| template <class T> constexpr bool empty(std::initializer_list<T>) noexcept; | ||
| } // namespace std | ||
|
|
||
| #endif // _GHLIBCPP_EMPTY |
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #include "iterator.h" | ||
| #include <bits/stl_function.h> | ||
| #include <empty.h> | ||
|
|
||
| namespace std { | ||
|
|
||
|
|
||
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
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 |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| #define _GHLIBCPP_STRING_VIEW | ||
|
|
||
| #include "stddef.h" | ||
| #include <empty.h> | ||
|
|
||
| namespace std { | ||
|
|
||
|
|
||
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
50 changes: 50 additions & 0 deletions
50
cpp/misra/src/rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.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,50 @@ | ||
| /** | ||
| * @id cpp/misra/potentially-erroneous-container-usage | ||
| * @name RULE-28-6-4: The result of std::remove, std::remove_if, std::unique and empty shall be used | ||
| * @description The `empty` method may be confused with `clear`, and the family of algorithms | ||
| * similar to `std::remove` require the resulting iterator to be used in order to | ||
| * properly handle the modifications performed. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-28-6-4 | ||
| * scope/single-translation-unit | ||
| * correctness | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.Iterators | ||
|
|
||
| predicate isRemoveOrUniqueCall(FunctionCall fc) { | ||
| exists(string name | name = fc.getTarget().getName() | | ||
| name = "remove" or | ||
| name = "remove_if" or | ||
| name = "unique" | ||
| ) and | ||
| fc.getTarget().hasQualifiedName("std", _) and | ||
| fc.getAnArgument().getUnderlyingType() instanceof IteratorType | ||
| } | ||
|
|
||
| predicate isEmptyCall(FunctionCall fc) { | ||
| fc.getTarget().getName() = "empty" and | ||
| ( | ||
| fc.getTarget().hasQualifiedName("std", "empty") or | ||
| fc.getTarget().(MemberFunction).getDeclaringType().hasQualifiedName("std", _) | ||
| ) | ||
| } | ||
|
|
||
| from FunctionCall fc, string message | ||
| where | ||
| not isExcluded(fc, DeadCode11Package::potentiallyErroneousContainerUsageQuery()) and | ||
| exists(ExprStmt es | es.getExpr() = fc) and | ||
| ( | ||
| isRemoveOrUniqueCall(fc) and | ||
| message = "Result of call to '" + fc.getTarget().getName() + "' is not used." | ||
| or | ||
| isEmptyCall(fc) and | ||
| message = "Result of call to 'empty' is not used." | ||
| ) | ||
| select fc, message | ||
10 changes: 10 additions & 0 deletions
10
cpp/misra/test/rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.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,10 @@ | ||
| | test.cpp:10:3:10:13 | call to remove | Result of call to 'remove' is not used. | | ||
| | test.cpp:27:3:27:16 | call to remove_if | Result of call to 'remove_if' is not used. | | ||
| | test.cpp:49:3:49:13 | call to unique | Result of call to 'unique' is not used. | | ||
| | test.cpp:65:3:65:13 | call to unique | Result of call to 'unique' is not used. | | ||
| | test.cpp:82:6:82:10 | call to empty | Result of call to 'empty' is not used. | | ||
| | test.cpp:100:3:100:12 | call to empty | Result of call to 'empty' is not used. | | ||
| | test.cpp:117:3:117:13 | call to remove | Result of call to 'remove' is not used. | | ||
| | test.cpp:122:6:122:10 | call to empty | Result of call to 'empty' is not used. | | ||
| | test.cpp:148:21:148:31 | call to remove | Result of call to 'remove' is not used. | | ||
| | test.cpp:149:9:149:19 | call to remove | Result of call to 'remove' is not used. | |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.qlref
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 @@ | ||
| rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.ql |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unused-result check only matches when the call expression is exactly the expression statement (
es.getExpr() = fc). This misses common “discard result” patterns likestatic_cast<void>(std::remove(...))or wrapping the call in a comma operator/parentheses. If the rule intends to catch deliberate discards, consider using a more general predicate for “return value is ignored” that also covers cast-to-void and similar wrappers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect