The reason not to allow isa<> && cast<> is that cast basically repeats the isa operation (and asserts if it fails, which in this case of course it won’t). But still, it’s more efficient to use dyn_cast so the (implicit) isa happens only once.
Interesting – do the compilers these day can optimize out the second redundant typecheck? And if not, is there something to be done, so both assert and second isa<> would be optimized out?
Typically, the dyn_cast<> operator is used in an if statement or some other flow control statement like this:
if (auto *AI = dyn_cast<AllocationInst>(Val)) {
// ...
}
This form of the if statement effectively combines together a call to isa<> and a call to cast<> into one statement, which is very convenient.
I think, like for all rules in practice, there are exceptions. I used this a couple of times and I don’t feel guilty for doing so.
Although, frequently introducing a variable for the result of the dyncast and then have a ptr && ptr->foo() usually leads to more readable code.
Personally, I consider the coding style as a style guide. You should follow it unless you have strong opinion diverging with objective benefit for doing so.