Author: mitchell Date: 2026-03-09T20:13:57+08:00 New Revision: ff232d67b534f41617effa20abdab0933fe127c6
URL: https://github.com/llvm/llvm-project/commit/ff232d67b534f41617effa20abdab0933fe127c6 DIFF: https://github.com/llvm/llvm-project/commit/ff232d67b534f41617effa20abdab0933fe127c6.diff LOG: [clang-tidy][NFC] Add missing Option tests in cppcoreguidelines and performance [3/N] (#185210) This PR adds testcases for untested Options in `cppcoreguidelines` and `performance` modules for better test coverage, specifically: - `cppcoreguidelines-init-variables`: `IncludeStyle`, `MathHeader`. - `cppcoreguidelines-pro-bounds-constant-array-index`: `IncludeStyle`. - `performance-inefficient-string-concatenation`: `StrictMode`. - `performance-no-automatic-move`: `AllowedTypes`. - `performance-type-promotion-in-math-fn`: `IncludeStyle`. - `performance-unnecessary-value-param`: `IncludeStyle`. As of AI Usage: Assisted by Gemini 3 and Claude (Writing part of the testcases and pre-commit reviewing). Added: clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/utility clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-mathheader.cpp clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index-includestyle.cpp clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-string-concatenation-strict.cpp clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move-allowed-types.cpp clang-tools-extra/test/clang-tidy/checkers/performance/type-promotion-in-math-fn-includestyle.cpp clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-includestyle.cpp Modified: clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/utility b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/utility new file mode 100644 index 0000000000000..30e170b5decc1 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/utility @@ -0,0 +1,18 @@ +#ifndef _UTILITY_ +#define _UTILITY_ + +namespace std { +template <typename T> +struct remove_reference { typedef T type; }; +template <typename T> +struct remove_reference<T &> { typedef T type; }; +template <typename T> +struct remove_reference<T &&> { typedef T type; }; + +template <typename _Tp> +constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) { + return static_cast<typename std::remove_reference<_Tp>::type &&>(__t); +} +} // namespace std + +#endif // _UTILITY_ diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-mathheader.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-mathheader.cpp new file mode 100644 index 0000000000000..f5e236b895202 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables-mathheader.cpp @@ -0,0 +1,13 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-init-variables %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: cppcoreguidelines-init-variables.IncludeStyle: 'google', \ +// RUN: cppcoreguidelines-init-variables.MathHeader: '<cmath>' \ +// RUN: }}" -- -fexceptions + +// CHECK-FIXES: #include <cmath> + +void init_unit_tests() { + float f; + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'f' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: float f = NAN; +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index-includestyle.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index-includestyle.cpp new file mode 100644 index 0000000000000..8f277f74befb7 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index-includestyle.cpp @@ -0,0 +1,28 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-constant-array-index %t -- \ +// RUN: -config="{CheckOptions: { \ +// RUN: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader: 'dir1/gslheader.h', \ +// RUN: cppcoreguidelines-pro-bounds-constant-array-index.IncludeStyle: 'google' \ +// RUN: }}" + +// CHECK-FIXES: #include "dir1/gslheader.h" + +typedef __SIZE_TYPE__ size_t; + +namespace std { + template<typename T, size_t N> + struct array { + T& operator[](size_t n); + T& at(size_t n); + }; +} + +namespace gsl { + template<class T, size_t N> + T& at( std::array<T, N> &a, size_t index ); +} + +void f(std::array<int, 10> a, int pos) { + a [ pos / 2 ] = 1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index] + // CHECK-FIXES: gsl::at(a, pos / 2 ) = 1; +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-string-concatenation-strict.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-string-concatenation-strict.cpp new file mode 100644 index 0000000000000..9b3dc11c585bf --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-string-concatenation-strict.cpp @@ -0,0 +1,18 @@ +// RUN: %check_clang_tidy %s performance-inefficient-string-concatenation %t -- \ +// RUN: -config="{CheckOptions: {performance-inefficient-string-concatenation.StrictMode: true}}" -- \ +// RUN: -isystem %clang_tidy_headers + +#include <string> + +void f(std::string) {} + +int main() { + std::string mystr1, mystr2; + mystr1 = mystr1 + mystr2; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: string concatenation results in allocation of unnecessary temporary strings; consider using 'operator+=' or 'string::append()' instead [performance-inefficient-string-concatenation] + + f(mystr1 + mystr2 + mystr1); + // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: string concatenation results in allocation of unnecessary temporary strings; consider using 'operator+=' or 'string::append()' instead + + return 0; +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move-allowed-types.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move-allowed-types.cpp new file mode 100644 index 0000000000000..f0a3e5f319d95 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/no-automatic-move-allowed-types.cpp @@ -0,0 +1,40 @@ +// RUN: %check_clang_tidy -std=c++11-or-later %s performance-no-automatic-move %t -- \ +// RUN: -config="{CheckOptions: {performance-no-automatic-move.AllowedTypes: '::Obj'}}" + +struct Obj { + Obj(); + Obj(const Obj &); + Obj(Obj &&); + virtual ~Obj(); +}; + +struct NonTemplate { + NonTemplate(const Obj &); + NonTemplate(Obj &&); +}; + +template <typename T> +T Make(); + +NonTemplate PositiveNonTemplate() { + const Obj obj = Make<Obj>(); + return obj; // No warning because Obj is allowed. +} + +struct Other { + Other(); + Other(const Other &); + Other(Other &&); + virtual ~Other(); +}; + +struct OtherNonTemplate { + OtherNonTemplate(const Other &); + OtherNonTemplate(Other &&); +}; + +OtherNonTemplate PositiveOtherNonTemplate() { + const Other obj = Make<Other>(); + return obj; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: constness of 'obj' prevents automatic move [performance-no-automatic-move] +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/type-promotion-in-math-fn-includestyle.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/type-promotion-in-math-fn-includestyle.cpp new file mode 100644 index 0000000000000..747aa8200368d --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/type-promotion-in-math-fn-includestyle.cpp @@ -0,0 +1,13 @@ +// RUN: %check_clang_tidy %s performance-type-promotion-in-math-fn %t -- \ +// RUN: -config="{CheckOptions: {performance-type-promotion-in-math-fn.IncludeStyle: 'google'}}" + +// CHECK-FIXES: #include <cmath> + +double acos(double); + +void check() { + float a; + acos(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: call to 'acos' promotes float to double [performance-type-promotion-in-math-fn] + // CHECK-FIXES: std::acos(a); +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-includestyle.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-includestyle.cpp new file mode 100644 index 0000000000000..d5d99a9b6f018 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-includestyle.cpp @@ -0,0 +1,22 @@ +// RUN: %check_clang_tidy %s performance-unnecessary-value-param %t -- \ +// RUN: -config="{CheckOptions: {performance-unnecessary-value-param.IncludeStyle: 'google' \ +// RUN: }}" -- -isystem %clang_tidy_headers -fno-delayed-template-parsing + +// CHECK-FIXES: #include <utility> + +#include <utility> + +struct ExpensiveMovableType { + ExpensiveMovableType(); + ExpensiveMovableType(ExpensiveMovableType &&); + ExpensiveMovableType(const ExpensiveMovableType &) = default; + ExpensiveMovableType &operator=(const ExpensiveMovableType &) = default; + ExpensiveMovableType &operator=(ExpensiveMovableType &&); + ~ExpensiveMovableType(); +}; + +void PositiveMoveOnCopyConstruction(ExpensiveMovableType E) { + auto F = E; + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: parameter 'E' of type 'ExpensiveMovableType' is passed by value and only copied once; consider moving it to avoid unnecessary copies [performance-unnecessary-value-param] + // CHECK-FIXES: auto F = std::move(E); +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param.cpp index 8dc13d3ed7f85..327b676228a7f 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param.cpp @@ -1,31 +1,8 @@ -// RUN: %check_clang_tidy %s performance-unnecessary-value-param %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy %s performance-unnecessary-value-param %t -- -- -isystem %clang_tidy_headers -fno-delayed-template-parsing // CHECK-FIXES: #include <utility> -namespace std { -template <typename> -struct remove_reference; - -template <typename _Tp> -struct remove_reference { - typedef _Tp type; -}; - -template <typename _Tp> -struct remove_reference<_Tp &> { - typedef _Tp type; -}; - -template <typename _Tp> -struct remove_reference<_Tp &&> { - typedef _Tp type; -}; - -template <typename _Tp> -constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) { - return static_cast<typename std::remove_reference<_Tp>::type &&>(__t); -} -} // namespace std +#include <utility> struct ExpensiveToCopyType { const ExpensiveToCopyType & constReference() const { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
