Skip to content

Commit

Permalink
added more samples (#6812)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Sep 23, 2024
1 parent 38e8d39 commit 7fa2eb5
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 0 deletions.
9 changes: 9 additions & 0 deletions samples/accessMoved/bad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
void foo(std::string);

int main()
{
std::string s = "test";
foo(std::move(s));

std::cout << s << std::endl;
}
9 changes: 9 additions & 0 deletions samples/accessMoved/good.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
void foo(std::string);

int main()
{
std::string s = "test";
foo(s);

std::cout << s << std::endl;
}
9 changes: 9 additions & 0 deletions samples/accessMoved/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
samples\accessMoved\bad.cpp:8:18: warning: Access of moved variable 's'. [accessMoved]
std::cout << s << std::endl;
^
samples\accessMoved\bad.cpp:6:9: note: Calling std::move(s)
foo(std::move(s));
^
samples\accessMoved\bad.cpp:8:18: note: Access of moved variable 's'.
std::cout << s << std::endl;
^
11 changes: 11 additions & 0 deletions samples/multiCondition/bad.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
static void f(bool b)
{
if (b) {}
else if (!b) {}
}

int main()
{
f(true);
return 0;
}
11 changes: 11 additions & 0 deletions samples/multiCondition/good.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
static void f(bool b)
{
if (b) {}
else {}
}

int main()
{
f(true);
return 0;
}
9 changes: 9 additions & 0 deletions samples/multiCondition/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
samples\multiCondition\bad.c:4:14: style: Expression is always true because 'else if' condition is opposite to previous condition at line 3. [multiCondition]
else if (!b) {}
^
samples\multiCondition\bad.c:3:9: note: first condition
if (b) {}
^
samples\multiCondition\bad.c:4:14: note: else if condition is opposite to first condition
else if (!b) {}
^
11 changes: 11 additions & 0 deletions samples/passedByValue_1/bad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class C
{
public:
explicit C(std::string s)
: _s(s)
{
}
void foo();
private:
std::string _s;
};
11 changes: 11 additions & 0 deletions samples/passedByValue_1/good.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class C
{
public:
explicit C(std::string s)
: _s(std::move(s))
{
}
void foo();
private:
std::string _s;
};
3 changes: 3 additions & 0 deletions samples/passedByValue_1/out.txt
Original file line number Diff line number Diff line change
@@ -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)
^
10 changes: 10 additions & 0 deletions samples/passedByValue_2/bad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bool foo(std::string s)
{
return s.empty();
}

int main()
{
std::string s;
foo(s);
}
10 changes: 10 additions & 0 deletions samples/passedByValue_2/good.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bool foo(const std::string& s)
{
return s.empty();
}

int main()
{
std::string s;
foo(s);
}
3 changes: 3 additions & 0 deletions samples/passedByValue_2/out.txt
Original file line number Diff line number Diff line change
@@ -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)
^
7 changes: 7 additions & 0 deletions samples/unreadVariable/bad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void foo(const std::string&, const std::string&);

int main()
{
std::string s1 = "test1", s2 = "test2";
foo(s1, s1);
}
7 changes: 7 additions & 0 deletions samples/unreadVariable/good.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void foo(const std::string&, const std::string&);

int main()
{
std::string s1 = "test1", s2 = "test2";
foo(s1, s2);
}
3 changes: 3 additions & 0 deletions samples/unreadVariable/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
samples\unreadVariable\bad.cpp:5:34: style: Variable 's2' is assigned a value that is never used. [unreadVariable]
std::string s1 = "test1", s2 = "test2";
^

0 comments on commit 7fa2eb5

Please sign in to comment.