GU0026

January 1, 2022 ยท View on GitHub

Range operator allocates

TopicValue
IdGU0026
SeverityWarning
EnabledTrue
CategoryGu.Analyzers.Correctness
CodeRangeAnalyzer

Description

Range operator on array or string allocates.

Motivation

xs[1..] returns a new array not a span of the original array which is different from xs[1..] when a span which can be confusing.

How to fix violations

One way is xs.AsSpan()[1..]

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable GU0026 // Range operator allocates
Code violating the rule here
#pragma warning restore GU0026 // Range operator allocates

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

#pragma warning disable GU0026 // Range operator allocates

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness", 
    "GU0026:Range operator allocates", 
    Justification = "Reason...")]