GU0015

November 24, 2021 ยท View on GitHub

Don't assign same more than once

TopicValue
IdGU0015
SeverityWarning
EnabledFalse
CategoryGu.Analyzers.Correctness
CodeSimpleAssignmentAnalyzer

Description

Don't assign same more than once.

Motivation

Assigning more than once in the same scope makes reasoning harder.

public class Foo
{
    public Foo()
    {
        this.Bar = 1;
        ...
        this.Bar = 2;
        ...
    }

    public int Bar { get; }
}

How to fix violations

Assign only once.

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable GU0015 // Don't assign same more than once
Code violating the rule here
#pragma warning restore GU0015 // Don't assign same more than once

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

#pragma warning disable GU0015 // Don't assign same more than once

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness", 
    "GU0015:Don't assign same more than once", 
    Justification = "Reason...")]