From 5b81f941201f425d0d518757ac761ef3b562be85 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 23 Apr 2024 22:46:10 +0200 Subject: [PATCH] added `passedByValue` samples --- samples/passedByValue_1/bad.cpp | 11 +++++++++++ samples/passedByValue_1/good.cpp | 11 +++++++++++ samples/passedByValue_1/out.txt | 3 +++ samples/passedByValue_2/bad.cpp | 10 ++++++++++ samples/passedByValue_2/good.cpp | 10 ++++++++++ samples/passedByValue_2/out.txt | 3 +++ 6 files changed, 48 insertions(+) create mode 100644 samples/passedByValue_1/bad.cpp create mode 100644 samples/passedByValue_1/good.cpp create mode 100644 samples/passedByValue_1/out.txt create mode 100644 samples/passedByValue_2/bad.cpp create mode 100644 samples/passedByValue_2/good.cpp create mode 100644 samples/passedByValue_2/out.txt diff --git a/samples/passedByValue_1/bad.cpp b/samples/passedByValue_1/bad.cpp new file mode 100644 index 00000000000..413b4327a74 --- /dev/null +++ b/samples/passedByValue_1/bad.cpp @@ -0,0 +1,11 @@ +class C +{ +public: + explicit C(std::string s) + : _s(s) + { + } + void foo(); +private: + std::string _s; +}; \ No newline at end of file diff --git a/samples/passedByValue_1/good.cpp b/samples/passedByValue_1/good.cpp new file mode 100644 index 00000000000..fd1f1450d17 --- /dev/null +++ b/samples/passedByValue_1/good.cpp @@ -0,0 +1,11 @@ +class C +{ +public: + explicit C(std::string s) + : _s(std::move(s)) + { + } + void foo(); +private: + std::string _s; +}; \ No newline at end of file diff --git a/samples/passedByValue_1/out.txt b/samples/passedByValue_1/out.txt new file mode 100644 index 00000000000..07228031d62 --- /dev/null +++ b/samples/passedByValue_1/out.txt @@ -0,0 +1,3 @@ +samples\passedByValue_1\bad.cpp:4:28: performance: Function parameter 's' should be passed by const reference. [passedByValue] + explicit C(std::string s) + ^ diff --git a/samples/passedByValue_2/bad.cpp b/samples/passedByValue_2/bad.cpp new file mode 100644 index 00000000000..29da7fe9036 --- /dev/null +++ b/samples/passedByValue_2/bad.cpp @@ -0,0 +1,10 @@ +bool foo(std::string s) +{ + return s.empty(); +} + +int main() +{ + std::string s; + foo(s); +} diff --git a/samples/passedByValue_2/good.cpp b/samples/passedByValue_2/good.cpp new file mode 100644 index 00000000000..bd3a941a45b --- /dev/null +++ b/samples/passedByValue_2/good.cpp @@ -0,0 +1,10 @@ +bool foo(const std::string& s) +{ + return s.empty(); +} + +int main() +{ + std::string s; + foo(s); +} diff --git a/samples/passedByValue_2/out.txt b/samples/passedByValue_2/out.txt new file mode 100644 index 00000000000..af76b252ad1 --- /dev/null +++ b/samples/passedByValue_2/out.txt @@ -0,0 +1,3 @@ +samples\passedByValue_2\bad.cpp:1:22: performance: Function parameter 's' should be passed by const reference. [passedByValue] +bool foo(std::string s) + ^