GU0016

November 24, 2021 ยท View on GitHub

Prefer lambda

TopicValue
IdGU0016
SeverityInfo
EnabledFalse
CategoryGu.Analyzers.Correctness
CodeMethodGroupAnalyzer

Description

Prefer lambda to reduce allocations.

Motivation

The generated IL does not cache the delegate. https://github.com/dotnet/roslyn/issues/5835

Remark

Note that method group is nicer syntax, fewer ways to get things wrong. Hence the analyzer is default disabled. Enable it for performance sensitive code.

How to fix violations

Use the code fix.

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable GU0016 // Prefer lambda
Code violating the rule here
#pragma warning restore GU0016 // Prefer lambda

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

#pragma warning disable GU0016 // Prefer lambda

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness", 
    "GU0016:Prefer lambda", 
    Justification = "Reason...")]