CPP: Annotate expr_has_no_effect test.

This commit is contained in:
Geoffrey White
2018-11-09 17:23:32 +00:00
parent 3f0e28aea9
commit 09782d145e
3 changed files with 15 additions and 15 deletions

View File

@@ -5,11 +5,11 @@ int external();
class Base {
public:
virtual int thingy() {
1;
1; // BAD
}
int our_thingy() {
Base::thingy();
Base::thingy(); // BAD
return 2;
}
};
@@ -17,14 +17,14 @@ public:
class Derived : public Base {
public:
virtual int thingy() {
external();
external(); // GOOD
return 3;
}
};
void internal() {
Base* ptr = new Derived();
ptr->thingy();
ptr->thingy(); // GOOD
}
}

View File

@@ -1,7 +1,7 @@
template <typename T>
void foo(T x, T y)
{
x << y;
x << y; // GOOD (effect depends on T)
};
struct streamable
@@ -15,9 +15,9 @@ void operator<<(streamable& lhs, streamable& rhs)
int main()
{
int x = 3;
foo(x, x);
foo(x, x); // BAD [NOT DETECTED]
streamable y;
foo(y, y);
foo(y, y); // BAD [NOT DETECTED]
return 0;
}
@@ -34,7 +34,7 @@ int pointless_add_numbers(int lhs, int rhs)
void call_add_numbers()
{
int accum = 0;
add_numbers(accum, 4);
add_numbers(accum, 10);
pointless_add_numbers(accum, 20);
add_numbers(accum, 4); // GOOD
add_numbers(accum, 10); // GOOD
pointless_add_numbers(accum, 20); // BAD
}

View File

@@ -6,18 +6,18 @@ char *pc;
volatile char *pv;
void f(void) {
c;
v;
c; // BAD
v; // (accesses to volatile variables are considered impure)
pc[5];
pc[5]; // BAD
pv[5];
((volatile char *)pc)[5];
*pc;
*pc; // BAD
*pv;
*((volatile char *)pc);
*(pc + 5);
*(pc + 5); // BAD
*(pv + 5);
*((volatile char *)(pc + 5));
*(((volatile char *)pc) + 5);