Bug Report T465789
Visible to All Users

CodeRush - The "Convert to Null Coalescing Operation" refactoring is suggested in an invalid scenario

created 8 years ago (modified 8 years ago)

Consider the following code:

C#
using System; namespace CodeRushIssue { class Program { private static int? Foo; static void Main( string[] args ) { var bar = Foo.HasValue ? DoSomething( Foo.Value ) : "baz"; } private static string DoSomething( int value ) { throw new NotImplementedException(); } } }

Code Rush suggests to refactor to

C#
using System; namespace CodeRushIssue { class Program { private static int? Foo; static void Main( string[] args ) { var bar = DoSomething( Foo.Value ) ?? "baz"; } private static string DoSomething( int value ) { throw new NotImplementedException(); } } }

but the new code is not equivalent and throws an exception when Foo is null.

Comments (1)
DevExpress Support Team 8 years ago

    Hello Dominic,

    Thank you for pointing out this issue and providing a code sample.
    I have reproduced this issue, and we will fix this behavior in future releases.
    We will notify you as soon as we have any results.

    Answers approved by DevExpress Support

    created 8 years ago

    We have fixed the issue described in this ticket and will include the fix in our next maintenance update. To apply this solution before the official update, request a hotfix by clicking the corresponding link for product versions you require.

    Note: Hotfixes may be unavailable for beta versions and updates that are about to be released.

      Comments (3)

        The scenario in my original request has been fixed indeed, but an invalid refactoring is still suggested in cases like this:

        double? nullable = null; var value = nullable.HasValue ? nullable.Value.ToString( System.Globalization.CultureInfo.InvariantCulture ) : null; CodeRush refactors into
        double? nullable = null; var value = nullable.Value.ToString( System.Globalization.CultureInfo.InvariantCulture ) ?? null;

          Which throws an exception

          DevExpress Support Team 8 years ago

            Hello,

            I've created a separate ticket on your behalf (T477966: The "Convert to Null Coalescing Operation" refactoring is available when the conditional expression true path expression type differs from the nullable variable value type, but shouldn't.). It has been placed in our processing queue and will be answered shortly.

            Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

            Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.