Spurious cast warning for C++?

How difficult would it be to add a warning to Clang when the programmer
performs a cast that provably has no effect?

A particular case I have in mind is someone getting confused and calling
std::move on an argument to a copy-constructor for a class that doesn't
implement move semantics.

I would be grateful if someone either (1) told me why this is
difficult/impossible, or (2) gave me some pointers to where I could
start trying to implement it...

This is question is probably better on the clang cfe-dev list.

What does “have no effect” mean in this case? In my mind, there are many casts that have no effect - other than allowing the code to compile, which is quite an important effect in itself (e.g. casting a 32-bit unsigned to a pointer on your typical 32-bit system).

Are you seeking to warn for:

int a;

int b;

a = (int) b;

and similar things?

-llvm-dev

Actually, maybe “spurious cast” is too broad and ambitious. Maybe it’s better to focus specifically on std::move.

The only thing I am really interested in is stuff like

class A {
public:
A &operator=(const A &a) {
// do some copy …
return *this;
}
}

int f() {
A a;
A b;
b = std::move(a);
}

, which can be a source of unforeseen performance bugs (e.g., people think they are moving big vectors but really copying them)

For the case of a redundant or unneeded std::move, I think Clang already has something like that…

Ah, it doesn’t quite catch that particular case.

Richard - would it be reasonable to extend the pessimizing move warning (well, I guess not - maybe as a separate warning) for cases like this:

void f(const foo&);
void g(foo x) {
f(std::move(x));
}

Or maybe there’s already a clang-tidy check for this sort of redundant move?

clang-tidy has related diagnostics, such as those listed at
https://clang.llvm.org/extra/clang-tidy/checks/misc-move-const-arg.html

Not all of them are desirable, IMO, but clang-tidy (rather than
clang-the-compiler) is the right place for them.

-- James

For the case of a redundant or unneeded std::move, I think Clang already
has something like that...

Ah, it doesn't quite catch that particular case.

Richard - would it be reasonable to extend the pessimizing move warning
(well, I guess not - maybe as a separate warning) for cases like this:

void f(const foo&);
void g(foo x) {
  f(std::move(x));
}

Or maybe there's already a clang-tidy check for this sort of redundant
move?

There's already a clang warning for redundant moves (-Wredundant-move). It
doesn't yet catch the above pattern, but it should...