<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Feb 3, 2016 at 3:52 PM, Hubert Tong <span dir="ltr"><<a href="mailto:hubert.reinterpretcast@gmail.com" target="_blank">hubert.reinterpretcast@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">hubert.reinterpretcast added inline comments.<br>
<br>
================<br>
<span class="">Comment at: lib/Sema/SemaExpr.cpp:1156-1159<br>
@@ +1155,6 @@<br>
+<br>
+  QualType LHSElemType = dyn_cast<ComplexType>(LHSType) ?<br>
+    cast<ComplexType>(LHSType)->getElementType() : LHSType;<br>
+  QualType RHSElemType = dyn_cast<ComplexType>(RHSType) ?<br>
+    cast<ComplexType>(RHSType)->getElementType() : RHSType;<br>
+<br>
----------------<br>
</span><span class="">rsmith wrote:<br>
> hubert.reinterpretcast wrote:<br>
> > The result of the `dyn_cast` can be saved instead of using `cast` after the `dyn_cast`.<br>
> Do not use `dyn_cast` / `cast` on types, use `LHSType->getAs<...>()` / `LHSType->castAs<...>()` instead. This will do the wrong thing on sugared types (eg, a typedef for a complex type).<br>
</span>Are there contexts in Clang where sugared types don't occur? Is the general guidance to use `getAs` or `castAs` even if the types are known to be canonical (which I agree isn't the case here)?</blockquote><div><br></div><div>If you're intentionally inspecting the immediate type sugar node, then dyn_cast/cast are appropriate. If it's obvious from context that you have a canonical type, then they're OK (and might be marginally faster), but it's an immediate red flag to a reader of the code to see those operations, so they should often be avoided for that reason even if they're correct.</div></div></div></div>