aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-06-17[clang-tidy][performance-unnecessary-value-param] Avoid in coroutines (#140912)Dmitry Polukhin1-7/+11
Summary: Replacing by-value parameters with passing by-reference is not safe for coroutines because the caller may be executed in parallel with the callee, which increases the chances of resulting in dangling references and hard-to-find crashes. See for the reference [cppcoreguidelines-avoid-reference-coroutine-parameters](https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters.html). Test Plan: check-clang-tools
2025-02-26[clang-tidy]improve performance-unnecessary-value-param performance (#128383)Congcong Cai1-13/+1
Tolerate fix-it breaking compilation when functions is used as pointers. `isReferencedOutsideOfCallExpr` will visit the whole translate unit for each matched function decls. It will waste lots of cpu time in some big cpp files. But the benefits of this validation are limited. Lots of function usage are out of current translation unit. After removing this validation step, the check profiling changes from 5.7 to 1.1 in SemaExprCXX.cpp, which is similar to version 18.
2024-10-22[clang-tidy][NFC] Replace usages of `DeclSpec::TQ` with `Qualifiers::TQ` ↵Vlad Serebrennikov1-1/+1
(#113295) This patch improves, but doens't fully resolve the layering violation, which stems from relying on Sema. There's one function that needs to convert enumerator to a string (`buildQualifier` in `FixItHintUtils.cpp`), but `Qualifiers::TQ` doesn't offer such function. Even more, the set of enumerators is not complete compared to `DeclSpec::TQ`, so I'm afraid that this would be a functional change.
2024-07-23[clang-tidy][performance-unnecessary-value-param] Make `handleMoveFix` ↵Clement Courbet1-31/+38
virtual (#99867) ... so that downstream checks can override behaviour to do additional processing. Refactor the rest of the logic to `handleConstRefFix` (which is also `virtual`). This is otherwise and NFC. This is similar to https://github.com/llvm/llvm-project/pull/73921 but for `performance-unnecessary-value-param`.
2024-07-15[clang-tidy] Allow unnecessary-value-param to match templated functions ↵Vitaly Goldshteyn1-4/+3
including lambdas with auto. (#97767) Clang-Tidy unnecessary-value-param value param will be triggered for templated functions if at least one instantiontion with expensive to copy type is present in translation unit. It is relatively common mistake to write lambda functions with auto arguments for expensive to copy types.
2024-04-17[clang analysis] ExprMutationAnalyzer support recursive forwarding reference ↵Congcong Cai1-5/+5
(#88843) Reapply for #88765. Partially fixes: #60895.
2023-01-14[clang-tidy][NFC] Use C++17 nested namespaces in the clang-tidy folderCarlos Galvez1-6/+2
Fix applied by running: run-clang-tidy.py -checks=-*,modernize-concat-nested-namespaces Differential Revision: https://reviews.llvm.org/D141770
2023-01-07[clang-tools-extra] Use std::optional instead of llvm::Optional (NFC)Kazu Hirata1-1/+1
This patch replaces (llvm::|)Optional< with std::optional<. I'll post a separate patch to clean up the "using" declarations, #include "llvm/ADT/Optional.h", etc. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07[clang-tools-extra] Add #include <optional> (NFC)Kazu Hirata1-0/+1
This patch adds #include <optional> to those files containing llvm::Optional<...> or Optional<...>. I'll post a separate patch to actually replace llvm::Optional with std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-10-30[clang-tools-extra] Use llvm::find (NFC)Kazu Hirata1-2/+1
2022-05-30Fix `performance-unnecessary-value-param` for template specializationSockke1-14/+3
The checker missed a check for parameter type of primary template of specialization template and this could cause build breakages. Reviewed By: aaron.ballman, flx Differential Revision: https://reviews.llvm.org/D116593
2022-04-16[clang-tidy] Add a Standalone diagnostics mode to clang-tidyNathan James1-1/+2
Adds a flag to `ClangTidyContext` that is used to indicate to checks that fixes will only be applied one at a time. This is to indicate to checks that each fix emitted should not depend on any other fixes emitted across the translation unit. I've currently implemented the `IncludeInserter`, `LoopConvertCheck` and `PreferMemberInitializerCheck` to use these support these modes. Reasoning behind this is in use cases like `clangd` it's only possible to apply one fix at a time. For include inserter checks, the include is only added once for the first diagnostic that requires it, this will result in subsequent fixes not having the included needed. A similar issue is seen in the `PreferMemberInitializerCheck` where the `:` will only be added for the first member that needs fixing. Fixes emitted in `StandaloneDiagsMode` will likely result in malformed code if they are applied all together, conversely fixes currently emitted may result in malformed code if they are applied one at a time. For this reason invoking `clang-tidy` from the binary will always with `StandaloneDiagsMode` disabled, However using it as a library its possible to select the mode you wish to use, `clangd` always selects `StandaloneDiagsMode`. This is an example of the current behaviour failing ```lang=c++ struct Foo { int A, B; Foo(int D, int E) { A = D; B = E; // Fix Here } }; ``` Incorrectly transformed to: ```lang=c++ struct Foo { int A, B; Foo(int D, int E), B(E) { A = D; // Fix Here } }; ``` In `StandaloneDiagsMode`, it gets transformed to: ```lang=c++ struct Foo { int A, B; Foo(int D, int E) : B(E) { A = D; // Fix Here } }; ``` Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D97121
2021-02-26[clang-tidy][NFC] Tweak some generation of diag messagesNathan James1-7/+4
Fix up cases where diag is called by piecing together a string in favour of placeholders. Fix up cases where select could be used instead of duplicating the message for sake of 1 word difference. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D97488
2020-12-11Remove references to the ast_type_traits namespaceAlexander Kornienko1-3/+3
Follow up to cd62511496938e33c061c90796dd23a5288ff843 / https://reviews.llvm.org/D74499 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D92994
2020-09-28[clang-tidy] IncludeInserter: allow <> in header nameAlexander Kornienko1-2/+1
This adds a pair of overloads for create(MainFile)?IncludeInsertion methods that use the presence of the <> in the file name to control whether the #include directive will use angle brackets or quotes. Motivating examples: https://reviews.llvm.org/D82089#inline-789412 and https://github.com/llvm/llvm-project/blob/master/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp#L433 The overloads with the IsAngled parameter can be removed after the users are updated. Update usages of createIncludeInsertion. Update (almost all) usages of createMainFileIncludeInsertion. Reviewed By: hokein Differential Revision: https://reviews.llvm.org/D85666
2020-07-27[clang-tidy] Refactor IncludeInserterNathan James1-7/+5
Simplified how `IncludeInserter` is used in Checks by abstracting away the SourceManager and PPCallbacks inside the method `registerPreprocessor`. Changed checks that use `IncludeInserter` to no longer use a `std::unique_ptr`, instead the IncludeInserter is just a member of the class thats initialized with an `IncludeStyle`. Saving an unnecessary allocation. This results in the removal of the field `IncludeSorter::IncludeStyle` from the checks, as its wrapped in the `IncludeInserter`. No longer need to create an instance of the `IncludeInserter` in the registerPPCallbacks, now that method only needs to contain: ``` Inserter.registerPreprocessor(PP); ``` Also added a helper method to `IncludeInserter` called `createMainFileInclusionInsertion`, purely sugar but does better express intentions. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D83680
2020-07-11[clang-tidy] Reworked enum options handling(again)Nathan James1-3/+1
Reland b9306fd after fixing the issue causing mac builds to fail unittests. Following on from D77085, I was never happy with the passing a mapping to the option get/store functions. This patch addresses this by using explicit specializations to handle the serializing and deserializing of enum options. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D82188
2020-06-29Revert "[clang-tidy] relanding b9306fd"Nathan James1-1/+3
This reverts commit 37cc4fa2eaa3d03ca8cd4947eb0d4c60e3c9b45c. More investigation needed
2020-06-29[clang-tidy] relanding b9306fdNathan James1-3/+1
Added some sanity checks to figure out the cause of a (seemingly unrelated) test failure on mac. These can be removed should no issues arise on that platform again.
2020-06-28Revert "[clang-tidy] Reworked enum options handling(again)"Nico Weber1-1/+3
This reverts commit b9306fd042ce1c11d84f05d2124dfdc65b8331fe and follow-up 42a51587c79a673045aec3586f4070630e5e7af3. It seems to build check-clang-tools on macOS, see comments on https://reviews.llvm.org/D82188
2020-06-28[clang-tidy] Reworked enum options handling(again)Nathan James1-3/+1
Following on from D77085, I was never happy with the passing a mapping to the option get/store functions. This patch addresses this by using explicit specializations to handle the serializing and deserializing of enum options. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D82188
2020-06-19[NFC] Simplify IncludeInsertions appending to diagnosticsNathan James1-5/+4
2020-05-21Set traversal explicitly where needed in clang-tidyStephen Kelly1-10/+15
Reviewers: aaron.ballman Subscribers: nemanjai, kbarton, arphaman, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72530
2020-04-07[clang-tidy] Change checks that take enum configurations to use a new access ↵Nathan James1-4/+5
method. Summary: Change all checks that take enums as configuration to use enum specific methods in `ClangTidyCheck::OptionsView`. Reviewers: aaron.ballman, alexfh Reviewed By: aaron.ballman Subscribers: wuzish, nemanjai, kbarton, arphaman, xazax.hun, cfe-commits Tags: #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D76606
2020-03-03[clang-tidy] Change checks to use new isLanguageVersionSupported restrictionNathan James1-4/+0
Summary: Modifies all checks that are language version dependent to use `isLanguageVersionSupported` Reviewers: jdoerfert, lebedev.ri, aaron.ballman, gribozavr2, Eugene.Zelenko Reviewed By: gribozavr2 Subscribers: wuzish, nemanjai, xazax.hun, hiraditya, kbarton, steven_wu, dexonsmith, arphaman, lebedev.ri, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D75340
2020-01-03[clang-tidy] implement utility-function to add 'const' to variablesJonas Toth1-3/+6
Summary: This patch extends the already existing facility to add 'const' to variables to be more flexible and correct. The previous version did not consider pointers as value AND pointee. For future automatic introduction for const-correctness this shortcoming needs to be fixed. It always allows configuration where the 'const' token is inserted, either on the left side (if possible) or the right side. It adds many unit-tests to the utility-function that did not exist before, as the function was implicitly tested through clang-tidy checks. These tests were not changed, as the API is still compatible. Reviewers: aaron.ballman, hokein, alexfh, shuaiwang, lebedev.ri Reviewed By: aaron.ballman Subscribers: jdoerfert, mgorny, xazax.hun, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D54395
2019-08-14[clang-tools-extra] Migrate llvm::make_unique to std::make_uniqueJonas Devlieghere1-1/+1
Now that we've moved to C++14, we no longer need the llvm::make_unique implementation from STLExtras.h. This patch is a mechanical replacement of (hopefully) all the llvm::make_unique instances across the monorepo. Differential revision: https://reviews.llvm.org/D66259 llvm-svn: 368944
2019-03-22[clang-tidy] Move all checks to the new registerPPCallbacks APIAlexander Kornienko1-4/+4
llvm-svn: 356796
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-11-25A bit of AST matcher cleanup, NFC.Alexander Kornienko1-2/+2
Removed the uses of the allOf() matcher inside node matchers that are implicit allOf(). Replaced uses of allOf() with the explicit node matcher where it makes matchers more readable. Replace anyOf(hasName(), hasName(), ...) with the more efficient and readable hasAnyName(). llvm-svn: 347520
2018-10-12[clang-tidy] White List Option for performance-unnecessary-value-param, ↵Adam Balogh1-5/+13
performance-unnecessary-copy-initialization and performance-for-range-copy New option added to these three checks to be able to silence false positives on types that are intentionally passed by value or copied. Such types are e.g. intrusive reference counting pointer types like llvm::IntrusiveRefCntPtr. The new option is named WhiteListTypes and can contain a semicolon-separated list of names of these types. Regular expressions are allowed. Default is empty. Differential Revision: https://reviews.llvm.org/D52727 llvm-svn: 344340
2018-09-17[clang-tidy] Remove duplicated logic in UnnecessaryValueParamCheck and use ↵Shuai Wang1-15/+8
FunctionParmMutationAnalyzer instead. Reviewers: alexfh, JonasToth, george.karpenkov Subscribers: xazax.hun, kristof.beyls, chrib, a.sidorin, Szelethus, cfe-commits Differential Revision: https://reviews.llvm.org/D52158 llvm-svn: 342403
2018-09-11[clangtidy] Remove old copy of ExprMutationAnalyzerShuai Wang1-3/+3
Summary: This is 2/2 of moving ExprMutationAnalyzer from clangtidy to clang/Analysis. ExprMutationAnalyzer is moved to clang/Analysis in D51948. This diff migrates existing usages within clangtidy to point to the new location and remove the old copy of ExprMutationAnalyzer. Reviewers: george.karpenkov, JonasToth Reviewed By: george.karpenkov Subscribers: mgorny, a.sidorin, Szelethus, cfe-commits Differential Revision: https://reviews.llvm.org/D51950 llvm-svn: 342006
2018-09-10[clang-tidy] ExprMutationAnalyzer: construct from references. Fixes PR38888Roman Lebedev1-2/+2
Summary: I have hit this the rough way, while trying to use this in D51870. There is no particular point in storing the pointers, and moreover the pointers are assumed to be non-null, and that assumption is not enforced. If they are null, it won't be able to do anything good with them anyway. Initially i thought about simply adding asserts() that they are not null, but taking/storing references looks like even cleaner solution? Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=38888 | PR38888 ]] Reviewers: JonasToth, shuaiwang, alexfh, george.karpenkov Reviewed By: shuaiwang Subscribers: xazax.hun, a.sidorin, Szelethus, cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D51884 llvm-svn: 341854
2018-08-09Port getLocStart -> getBeginLocStephen Kelly1-5/+5
Reviewers: javed.absar Subscribers: nemanjai, kbarton, ilya-biryukov, ioeric, jkorous, arphaman, jfb, cfe-commits Differential Revision: https://reviews.llvm.org/D50354 llvm-svn: 339400
2018-08-03Use ExprMutationAnalyzer in performance-unnecessary-value-paramShuai Wang1-34/+39
Summary: This yields better recall as ExprMutationAnalyzer is more accurate. One common pattern this check is now able to catch is: ``` void foo(std::vector<X> v) { for (const auto& elm : v) { // ... } } ``` Reviewers: george.karpenkov Subscribers: a.sidorin, cfe-commits Differential Revision: https://reviews.llvm.org/D50102 llvm-svn: 338903
2018-02-02[clang-tidy] ObjC ARC objects should not trigger ↵Ben Hamilton1-0/+4
performance-unnecessary-value-param Summary: The following Objective-C code currently incorrectly triggers clang-tidy's performance-unnecessary-value-param check: ``` % cat /tmp/performance-unnecessary-value-param-arc.m void foo(id object) { } clang-tidy /tmp/performance-unnecessary-value-param-arc.m -checks=-\*,performance-unnecessary-value-param -- -xobjective-c -fobjc-abi-version=2 -fobjc-arc 1 warning generated. /src/llvm/tools/clang/tools/extra/test/clang-tidy/performance-unnecessary-value-param-arc.m:10:13: warning: the parameter 'object' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param] void foo(id object) { } ~~ ^ const & ``` This is wrong for a few reasons: 1) Objective-C doesn't have references, so `const &` is not going to help 2) ARC heavily optimizes the "expensive" copy which triggers the warning This fixes the issue by disabling the warning for non-C++, as well as disabling it for objects under ARC memory management for Objective-C++. Fixes https://bugs.llvm.org/show_bug.cgi?id=32075 Test Plan: New tests added. Ran tests with `make -j12 check-clang-tools`. Reviewers: alexfh, hokein Reviewed By: hokein Subscribers: stephanemoore, klimek, xazax.hun, cfe-commits, Wizard Differential Revision: https://reviews.llvm.org/D42812 llvm-svn: 324097
2017-07-26[clang-tidy] Do not issue fixit for explicit template specializationsFelix Berger1-1/+15
Summary: Do not issue fixit in UnnecessaryValueParamCheck if the function is an explicit template specialization as this could cause build breakages. Reviewers: alexfh Subscribers: JDevlieghere, xazax.hun, cfe-commits Differential Revision: https://reviews.llvm.org/D35718 llvm-svn: 309067
2017-07-20[clang-tidy] Unify the way IncludeStyle and HeaderFileExtesions options are usedAlexander Kornienko1-1/+1
llvm-svn: 308605
2017-05-16[clang-tidy] Speed up performance-unnecessary-value-param checkAlexander Kornienko1-5/+4
Moved slower matchers closer to the end. The total speed up on a large file I was interested in is not huge, just about 10%, since the check seems to be doing a lot in the check() method. llvm-svn: 303191
2017-01-23[clang-tidy] Ignore implicit functions in performance-unnecessary-value-paramMalcolm Parsons1-1/+1
Summary: The performance-unnecessary-value-param check mangled inherited constructors, as the constructors' parameters do not have useful source locations. Fix this by ignoring implicit functions. Fixes PR31684. Reviewers: flx, alexfh, aaron.ballman Subscribers: madsravn, JDevlieghere, cfe-commits Differential Revision: https://reviews.llvm.org/D29018 llvm-svn: 292786
2017-01-03[clang-tidy] Handle constructors in performance-unnecessary-value-paramMalcolm Parsons1-28/+17
Summary: modernize-pass-by-value doesn't warn about value parameters that cannot be moved, so performance-unnecessary-value-param should. Reviewers: aaron.ballman, flx, alexfh Subscribers: JDevlieghere, cfe-commits Differential Revision: https://reviews.llvm.org/D28022 llvm-svn: 290883
2016-12-17[clang-tidy] Remove duplicated check from move-constructor-initMalcolm Parsons1-1/+1
Summary: An addition to the move-constructor-init check was duplicating the modernize-pass-by-value check. Remove the additional check and UseCERTSemantics option. Run the move-constructor-init test with both checks enabled. Fix modernize-pass-by-value false-positive when initializing a base class. Add option to modernize-pass-by-value to only warn about parameters that are already values. Reviewers: alexfh, flx, aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D26453 llvm-svn: 290051
2016-12-16[clang-tidy] Do not move parameter if only DeclRefExpr occurs inside of a loopFelix Berger1-0/+13
Summary: This fixes a bug where the performance-unnecessary-value-param check suggests a fix to move the parameter inside of a loop which could be invoked multiple times. Reviewers: sbenza, aaron.ballman, alexfh Subscribers: JDevlieghere, cfe-commits Differential Revision: https://reviews.llvm.org/D27187 llvm-svn: 289912
2016-12-02[clang-tidy] Do not trigger unnecessary-value-param check on methods marked ↵Felix Berger1-1/+2
as final Summary: Virtual method overrides of dependent types cannot be recognized unless they are marked as override or final. Exclude methods marked as final from check and add test. Reviewers: sbenza, hokein, alexfh Subscribers: malcolm.parsons, JDevlieghere, cfe-commits Differential Revision: https://reviews.llvm.org/D27248 llvm-svn: 288502
2016-11-10[clang-tidy] Do not issue fix for functions that are referenced outside of ↵Felix Berger1-3/+15
callExpr Summary: Suppress fixes for functions that are referenced within the compilation unit outside of a call expression as the signature change could break the code referencing the function. We still issue a warning in this case so that users can decide to manually change the function signature. Reviewers: alexfh, sbenza, aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D26203 llvm-svn: 286424
2016-11-08[clang-tools-extra] Format sources with clang-format. NFC.Mandeep Singh Grang1-1/+1
Summary: Ran clang-format on all .c/.cpp/.h files in clang-tools-extra. Excluded the test, unittests, clang-reorder-fields, include-fixer, modularize and pptrace directories. Reviewers: klimek, alexfh Subscribers: nemanjai Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D26329 llvm-svn: 286221
2016-11-04[ClangTidy - performance-unnecessary-value-param] Only add "const" when ↵Felix Berger1-1/+4
current parameter is not already const qualified Reviewers: alexfh, sbenza, aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D26207 llvm-svn: 286010
2016-08-01[clang-tidy] remove trailing whitespaces and retabKirill Bobyrev1-2/+2
llvm-svn: 277340
2016-07-05[clang-tidy] UnnecessaryValueParamCheck - only warn for virtual methodsFelix Berger1-2/+4
Summary: As changing virtual methods could break method overrides disable applying the fix and just warn. Reviewers: alexfh, sbenza Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D21936 llvm-svn: 274552