"Convert to automatic property" and "ref"

I would love to do a global "convert to automatic property" during code cleanup, but in cases where the explicit underyling field is used with "ref", conversion to an automatic property is invalid since you cannot use a property as the target of a ref.

Until this is fixed, is there an explicit RS attribute I can use to preclude an existig explicit property from being considered a candidate for automatic conversion? I could probably add a dummy operation to achieve the same effect, but I would prefer something less confusing.



Here is an example:
/// /// RS will suggest converting Count to an autoproperty. If this change /// is made the "ref _count" becomes "ref Count", which is illegal /// since you can not "ref" a property. /// public class RefAutoPropertyBug { private int _count; ///

/// Increments this instance. /// public void Increment() { Interlocked.Increment ( ref _count ); } /// /// Gets the count. /// /// The count.]]>
public int Count
{
get
{
return _count;
}
}
}

Edited by: Brian Strelioff on Aug 16, 2008 6:26 PM

5 comments

Hello,

I would love to do a global "convert to automatic property" during
code cleanup, but in cases where the explicit underyling field is used
with "ref", conversion to an automatic property is invalid since you
cannot use a property as the target of a ref.


So you imply that R# breaks the code during cleanup.

I cannot reproduce this behavior. For private fields, their ref usages are
respected. For public fields, there is no such suggestion.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

Hi Serge. after your post I edited my original post to include an example.

0

Hello,

after your post I edited my original post to include an
example.


Eh ... I believe it's not possible to change an article already posted to
the News server, so the "update post" option on the Web mirror is clearly
a bug. We'll remove it, thank you for reporting.

Meanwhile, could you please post the example in a reply to the thread, as
the original article still bears the same text as when posted?


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

How strange -- when I open up this thread, I see the change to the original message I posted!

Anyway, here is the example:


UPDATE

Here is an example:
/// /// RS will suggest converting Count to an autoproperty. If this change /// is made the "ref _count" becomes "ref Count", which is illegal /// since you can not "ref" a property. /// public class RefAutoPropertyBug { private int _count; ///

/// Increments this instance. /// public void Increment() { Interlocked.Increment ( ref _count ); } /// /// Gets the count. /// /// The count.]]>
public int Count
{
get
{
return _count;
}
}
}

Edited by: Brian Strelioff on Aug 16, 2008 6:26 PM

0

Hello,

How strange -- when I open up this thread, I see the change to the
original message I posted!


news://news.intellij.net/1183280.35491218900145161.JavaMail.jive@app4.labs.intellij.net

Here is an example:
/// <summary>
/// RS will suggest converting Count to an autoproperty. If this
change
/// is made the "ref _count" becomes "ref Count", which is illegal
/// since you can not "ref" a property.
/// </summary>



Now I see. I've tried a read-write property as an auto property candidate,
and here we have another case, a property that will become "an auto property
with a private setter". Indeed, R# displays a hint in this case, and running
the QFix on the hint breaks the code. Thank you for indicating the bug.

However, as I ran Cleanup Code with Full Cleanup profile, there was no broken
code, as the cleanup would not turn readonly properties into automatic properties
by default.

As I see the situation,
• The QFix should be used with care (if at all) on read-only properties.
They're highlighted with a hint (a two-char-long underline), rather than
a suggestion (a green squiggly).
• The CodeCleanup could still be used, as it wouldn't touch readonly properties.
• The bug has to be fixed on our side :)


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

Please sign in to leave a comment.