<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><br><div dir="ltr"><blockquote type="cite">El 3 oct. 2020, a la(s) 22:06, Oleksandr Koval via cfe-dev <cfe-dev@lists.llvm.org> escribió:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><div dir="ltr"><div>Hi, I'm writing a check that detects and removes redundant `this->` usage. Finding explicit `this->` was simple, now I want to check whether it's safe to remove it. My main concern is such case:</div>
<pre style="color:rgb(0,0,0);background:rgb(255,255,255) none repeat scroll 0% 0%"><span style="color:rgb(128,0,0);font-weight:bold">struct</span> X<span style="color:rgb(128,0,128)">{</span>
<span style="color:rgb(128,0,0);font-weight:bold">void</span> f<span style="color:rgb(128,128,48)">(</span><span style="color:rgb(128,0,0);font-weight:bold">int</span> x<span style="color:rgb(128,128,48)">)</span><span style="color:rgb(128,0,128)">{</span>
<span style="color:rgb(105,105,105)">// without `this->` will use parameter</span>
<span style="color:rgb(128,0,0);font-weight:bold">this</span><span style="color:rgb(128,128,48)">-</span><span style="color:rgb(128,128,48)">></span>x<span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(105,105,105)">// can remove `this->` because</span>
<span style="color:rgb(105,105,105)">// local `y` is not visible yet</span>
<span style="color:rgb(128,0,0);font-weight:bold">this</span><span style="color:rgb(128,128,48)">-</span><span style="color:rgb(128,128,48)">></span>y<span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,0,128)">;</span>
y<span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,0);font-weight:bold">int</span> y<span style="color:rgb(128,0,128)">;</span>
y<span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,128,48)">+</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,128)">}</span>
<span style="color:rgb(128,0,0);font-weight:bold">int</span> x<span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,0);font-weight:bold">int</span> y<span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,128)">}</span><span style="color:rgb(128,0,128)">;</span>
</pre>
<div>I need to know whether a specific name(function or variable) will resolve to the same declaration without `this->` part. My matcher is <i>memberExpr(has(cxxThisExpr()))</i>, I bind MemberExpr and CXXThisExpr, I can rewrite matcher to also bind enclosing CXXMethodDecl. As I understand, simple enumeration of parameter/local variable names won't work(as in case with `y`) because of lookup/visibility rules. Is there some `lookup()` function whose result I can compare against the declaration of original function/variable? I found Sema::LookupName() but can't figure out how to get Scope of the found expression. Can you give me an example please?</div></div></div></blockquote><br><div>Removing this-> from this->y is safe in the sense that the code will still work, because the local y isn't visible yet, but it would be very confusing and error prone for the human reading it. It would subtly break if someone moves the local y declaration higher for example.</div><div><br></div><div>So maybe you should consider *not* suggesting the removal of this-> in a situation like this (when there is a local var with the same name but it's not yet visible) due to code readability, even if it's technically safe to remove it.</div><div><br></div><div>And as a bonus, it becomes easier to implement :)</div><div><br></div><div>-- </div><div>Nicolás</div></body></html>