Search Results for

    Show / Hide Table of Contents

    NUnit2057

    Remove unnecessary lambda expression

    Topic Value
    Id NUnit2057
    Severity Info
    Enabled True
    Category Assertion
    Code DelegateUnnecessaryAnalyzer

    Description

    Only the argument value is needed by the Assert.

    Motivation

    The Assert method will call a delegate if passed in, but that is an unnecessary overhead.

    In most cases, only the return value is needed by the assertion.

    There are two exceptions:

    1. When catching exceptions, like in:

      Assert.That(() => instance.Method1(null), Throws.ArgumentNullException);
      
    2. When a method might need to be called multiple times, like in:

      Assert.That(() => task.IsCompleted, Is.True.After(5).Seconds.PollEvery(100).MilliSeconds);
      

    Sometimes developers unnecessarily create a lambda expression for the actual parameter.

    Assert.That(() => instance.Method1(Parameters), Is.EqualTo(expected));
    

    Here, the call is performed inside the Assert and then the result is tested. This is not necessary. The same test can be written as:

    Assert.That(instance.Method1(Parameters), Is.EqualTo(expected));
    

    It is clearer both in looks and in what is being tested here.

    Sometimes the lambda is created implicitly like in:

    Assert.That(instance.Method2, Is.EqualTo(expected));
    

    The fixed code should look like:

    Assert.That(instance.Method2(), Is.EqualTo(expected));
    

    This clearly indicates that the Assert is on the return value of the method.

    How to fix violations

    Remove the anonymous lambda creation () =>. There is a code-fix that can do this for you.

    Configure severity

    Via ruleset file

    Configure the severity per project, for more info see MSDN.

    Via .editorconfig file

    # NUnit2057: Remove unnecessary lambda expression
    dotnet_diagnostic.NUnit2057.severity = chosenSeverity
    

    where chosenSeverity can be one of none, silent, suggestion, warning, or error.

    Via #pragma directive

    #pragma warning disable NUnit2057 // Remove unnecessary lambda expression
    Code violating the rule here
    #pragma warning restore NUnit2057 // Remove unnecessary lambda expression
    

    Or put this at the top of the file to disable all instances.

    #pragma warning disable NUnit2057 // Remove unnecessary lambda expression
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
        "NUnit2057:Remove unnecessary lambda expression",
        Justification = "Reason...")]
    
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0