-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy] Add check hicpp-ignored-remove-result #73119
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91cf412
[clang-tidy] Add check hicpp-ignored-remove-result
bjosv 46179bd
fixup: missing double back-ticks in docs
bjosv c1e05d1
fixup: implement as own check (using add_new_check.py)
bjosv 696d465
fixup: review comments
bjosv 5f98947
fixup: update docs
bjosv 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
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
28 changes: 28 additions & 0 deletions
28
clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.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,28 @@ | ||
//===--- IgnoredRemoveResultCheck.cpp - clang-tidy ------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "IgnoredRemoveResultCheck.h" | ||
|
||
namespace clang::tidy::hicpp { | ||
|
||
IgnoredRemoveResultCheck::IgnoredRemoveResultCheck(llvm::StringRef Name, | ||
ClangTidyContext *Context) | ||
: UnusedReturnValueCheck(Name, Context, | ||
"::std::remove;" | ||
"::std::remove_if;" | ||
"::std::unique") { | ||
// The constructor for ClangTidyCheck needs to have been called | ||
// before we can access options via Options.get(). | ||
AllowCastToVoid = Options.get("AllowCastToVoid", true); | ||
} | ||
|
||
void IgnoredRemoveResultCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { | ||
Options.store(Opts, "AllowCastToVoid", AllowCastToVoid); | ||
} | ||
|
||
} // namespace clang::tidy::hicpp |
29 changes: 29 additions & 0 deletions
29
clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.h
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,29 @@ | ||
//===--- IgnoredRemoveResultCheck.h - clang-tidy ----------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_IGNOREDREMOVERESULTCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_IGNOREDREMOVERESULTCHECK_H | ||
|
||
#include "../bugprone/UnusedReturnValueCheck.h" | ||
|
||
namespace clang::tidy::hicpp { | ||
|
||
/// Ensure that the result of std::remove, std::remove_if and std::unique | ||
/// are not ignored according to rule 17.5.1. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/hicpp/ignored-remove-result.html | ||
class IgnoredRemoveResultCheck : public bugprone::UnusedReturnValueCheck { | ||
public: | ||
IgnoredRemoveResultCheck(StringRef Name, ClangTidyContext *Context); | ||
void storeOptions(ClangTidyOptions::OptionMap &Opts) override; | ||
}; | ||
|
||
} // namespace clang::tidy::hicpp | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_IGNOREDREMOVERESULTCHECK_H |
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
24 changes: 24 additions & 0 deletions
24
clang-tools-extra/docs/clang-tidy/checks/hicpp/ignored-remove-result.rst
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,24 @@ | ||
.. title:: clang-tidy - hicpp-ignored-remove-result | ||
|
||
hicpp-ignored-remove-result | ||
=========================== | ||
|
||
Ensure that the result of ``std::remove``, ``std::remove_if`` and ``std::unique`` | ||
are not ignored according to | ||
`rule 17.5.1 <https://www.perforce.com/resources/qac/high-integrity-cpp-coding-standard/standard-library>`_. | ||
|
||
The mutating algorithms ``std::remove``, ``std::remove_if`` and both overloads | ||
of ``std::unique`` operate by swapping or moving elements of the range they are | ||
operating over. On completion, they return an iterator to the last valid | ||
element. In the majority of cases the correct behavior is to use this result as | ||
the first operand in a call to ``std::erase``. | ||
|
||
PiotrZSL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This check is a subset of :doc:`bugprone-unused-return-value <../bugprone/unused-return-value>` | ||
and depending on used options it can be superfluous to enable both checks. | ||
|
||
Options | ||
------- | ||
|
||
.. option:: AllowCastToVoid | ||
|
||
Controls whether casting return values to ``void`` is permitted. Default: `true`. |
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
66 changes: 66 additions & 0 deletions
66
clang-tools-extra/test/clang-tidy/checkers/hicpp/ignored-remove-result.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,66 @@ | ||
// RUN: %check_clang_tidy %s hicpp-ignored-remove-result %t | ||
// RUN: %check_clang_tidy -check-suffixes=NOCAST %s hicpp-ignored-remove-result %t -- -config='{CheckOptions: {hicpp-ignored-remove-result.AllowCastToVoid: false}}' | ||
|
||
namespace std { | ||
|
||
template <typename ForwardIt, typename T> | ||
ForwardIt remove(ForwardIt, ForwardIt, const T &); | ||
|
||
template <typename ForwardIt, typename UnaryPredicate> | ||
ForwardIt remove_if(ForwardIt, ForwardIt, UnaryPredicate); | ||
|
||
template <typename ForwardIt> | ||
ForwardIt unique(ForwardIt, ForwardIt); | ||
|
||
template <class InputIt, class T> | ||
InputIt find(InputIt, InputIt, const T&); | ||
|
||
class error_code { | ||
}; | ||
|
||
} // namespace std | ||
|
||
std::error_code errorFunc() { | ||
return std::error_code(); | ||
} | ||
|
||
void warning() { | ||
std::remove(nullptr, nullptr, 1); | ||
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors | ||
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning | ||
// CHECK-MESSAGES-NOCAST: [[@LINE-3]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors | ||
|
||
std::remove_if(nullptr, nullptr, nullptr); | ||
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors | ||
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning | ||
// CHECK-MESSAGES-NOCAST: [[@LINE-3]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors | ||
|
||
std::unique(nullptr, nullptr); | ||
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors | ||
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning | ||
// CHECK-MESSAGES-NOCAST: [[@LINE-3]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors | ||
} | ||
|
||
void optionalWarning() { | ||
// No warning unless AllowCastToVoid=false | ||
(void)std::remove(nullptr, nullptr, 1); | ||
// CHECK-MESSAGES-NOCAST: [[@LINE-1]]:9: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors | ||
} | ||
|
||
void noWarning() { | ||
|
||
auto RemoveRetval = std::remove(nullptr, nullptr, 1); | ||
|
||
auto RemoveIfRetval = std::remove_if(nullptr, nullptr, nullptr); | ||
|
||
auto UniqueRetval = std::unique(nullptr, nullptr); | ||
|
||
// Verify that other checks in the baseclass are not used. | ||
// - no warning on std::find since the checker overrides | ||
// bugprone-unused-return-value's checked functions. | ||
std::find(nullptr, nullptr, 1); | ||
// - no warning on return types since the checker disable | ||
// bugprone-unused-return-value's checked return types. | ||
errorFunc(); | ||
(void) errorFunc(); | ||
} |
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.