Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] performance-unnecessary-copy-initialization: Create opti…
…on to exclude container types from triggering the check. Add string list option of type names analagous to `AllowedTypes` which lets users specify a list of ExcludedContainerTypes. Types matching this list will not trigger the check when an expensive variable is copy initialized from a const accessor method they provide, i.e.: ``` ExcludedContainerTypes = 'ExcludedType' void foo() { ExcludedType<ExpensiveToCopy> Container; const ExpensiveToCopy NecessaryCopy = Container.get(); } ``` Even though an expensive to copy variable is copy initialized the check does not trigger because the container type is excluded. This is useful for container types that don't own their data, such as view types where modification of the returned references in other places cannot be reliably tracked, or const incorrect types. Differential Revision: https://reviews.llvm.org/D106173 Reviewed-by: ymandel
- Loading branch information
Felix Berger
committed
Jul 22, 2021
1 parent
46667a1
commit cb4c12b
Showing
4 changed files
with
105 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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
60 changes: 60 additions & 0 deletions
60
...ng-tidy/checkers/performance-unnecessary-copy-initialization-excluded-container-types.cpp
This file contains 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,60 @@ | ||
// RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t -- -config="{CheckOptions: [{key: performance-unnecessary-copy-initialization.ExcludedContainerTypes, value: 'ns::ViewType$;::ConstInCorrectType$'}]}" -- | ||
|
||
namespace ns { | ||
template <typename T> | ||
struct ViewType { | ||
ViewType(const T &); | ||
const T &view() const; | ||
}; | ||
} // namespace ns | ||
|
||
template <typename T> | ||
struct ViewType { | ||
ViewType(const T &); | ||
const T &view() const; | ||
}; | ||
|
||
struct ExpensiveToCopy { | ||
~ExpensiveToCopy(); | ||
void constMethod() const; | ||
}; | ||
|
||
struct ConstInCorrectType { | ||
const ExpensiveToCopy &secretlyMutates() const; | ||
}; | ||
|
||
using NSVTE = ns::ViewType<ExpensiveToCopy>; | ||
typedef ns::ViewType<ExpensiveToCopy> FewType; | ||
|
||
void positiveViewType() { | ||
ExpensiveToCopy E; | ||
ViewType<ExpensiveToCopy> V(E); | ||
const auto O = V.view(); | ||
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'O' is copy-constructed | ||
// CHECK-FIXES: const auto& O = V.view(); | ||
O.constMethod(); | ||
} | ||
|
||
void excludedViewTypeInNamespace() { | ||
ExpensiveToCopy E; | ||
ns::ViewType<ExpensiveToCopy> V(E); | ||
const auto O = V.view(); | ||
O.constMethod(); | ||
} | ||
|
||
void excludedViewTypeAliased() { | ||
ExpensiveToCopy E; | ||
NSVTE V(E); | ||
const auto O = V.view(); | ||
O.constMethod(); | ||
|
||
FewType F(E); | ||
const auto P = F.view(); | ||
P.constMethod(); | ||
} | ||
|
||
void excludedConstIncorrectType() { | ||
ConstInCorrectType C; | ||
const auto E = C.secretlyMutates(); | ||
E.constMethod(); | ||
} |