Bug Report T583012
Visible to All Users

Refactorings - The Throw Exceptions Contract refactoring does not recognize thrown expressions

created 7 years ago

Consider the following class:

C#
class MyClass { private readonly Version m_version1; private readonly Version m_version2; public MyClass(Version version1, Version version2) { m_version1 = version1 ?? throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null."); m_version2 = version2; } }

If invoking the "Add Contract->Throw Exceptions Contract" in the constructor the following code is generated:

C#
public MyClass(Version version1, Version version2) { if (version1 == null) { throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null."); } if (version2 == null) { throw new ArgumentNullException(nameof(version2), $"{nameof(version2)} is null."); } m_version1 = version1 ?? throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null."); m_version2 = version2; }

There is a redundant null check for version1 now . Expected the following code:

C#
public MyClass(Version version1, Version version2) { if (version2 == null) { throw new ArgumentNullException(nameof(version2), $"{nameof(version2)} is null."); } m_version1 = version1 ?? throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null."); m_version2 = version2; }

or possibly the more compact (should probably be controllable by some option in that case):

C#
public MyClass(Version version1, Version version2) { m_version1 = version1 ?? throw new ArgumentNullException(nameof(version1), $"{nameof(version1)} is null."); m_version2 = version2 ?? throw new ArgumentNullException(nameof(version2), $"{nameof(version2)} is null."); }
Comments (1)
DevExpress Support Team 7 years ago

    Hi Peter,
    Thanks for the code snippets.
    We are working on this issue now and will notify you when we have any results.

    Answers approved by DevExpress Support

    created 7 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.

    Additional information:

    Hi Peter,

    We have fixed the issue with adding a redundant 'null' check when a throw expression is used. As for supporting throw expressions for a generated 'null' check in the 'Throw Exceptions Contract' code provider, this requires more changes in the current architecture of this provider. We have added this task to our backlog and will consider implementing it in future updates. We will notify you of our progress in the context of this ticket.

      Comments (1)
      DevExpress Support Team 7 years ago

        Hi Peter,

        I have good news. We have added the functionality of the Add Contract refactoring as your requested. The latest release which contains this functionality is available on the Visual Studio Market Place. You can read more about new features in the latest release from the link below:
        CodeRush 17.2.6 is Available (and Includes a New Killer Feature for Visual Studio)

        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.