ReadonlyLocalVariables 2.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ReadonlyLocalVariables --version 2.2.1
                    
NuGet\Install-Package ReadonlyLocalVariables -Version 2.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ReadonlyLocalVariables" Version="2.2.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ReadonlyLocalVariables" Version="2.2.1" />
                    
Directory.Packages.props
<PackageReference Include="ReadonlyLocalVariables">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ReadonlyLocalVariables --version 2.2.1
                    
#r "nuget: ReadonlyLocalVariables, 2.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=ReadonlyLocalVariables&version=2.2.1
                    
Install ReadonlyLocalVariables as a Cake Addin
#tool nuget:?package=ReadonlyLocalVariables&version=2.2.1
                    
Install ReadonlyLocalVariables as a Cake Tool

ReadonlyLocalVariables

Test

Prohibits reassignment of local variables.

Installation

You can download packages from NuGet.

Usage

After installing the analyzer, reassignment to a local variable results in an error. If there are local variables for which you want to allow reassignment in exceptional cases, you can explicitly specify this by adding the ReadonlyLocalVariables.ReassignableVariable attribute to the method.

using ReadonlyLocalVariables;
using System;

class C
{
    private static int Field = 0;

    [ReassignableVariable("reassignable")]  // Explicitly state which normal variables are allowed to be reassigned.
    static void Main(string[] args)
    {
        var normal = 0;
        var reassignable = 0;

        Console.WriteLine(normal);
        Console.WriteLine(reassignable);
        Console.WriteLine(Field);

        normal = 1;        // Normal variables are treated as read-only.
        reassignable = 1;  // Specially marked local variables can be reassigned.
        Field = 1;         // Class members are not inspected because they have the `readonly` keyword.

        Console.WriteLine(normal);
        Console.WriteLine(reassignable);
        Console.WriteLine(Field);
    }
}

out Parameter

Passing an already declared local variable with the out parameter modifier is also prohibited.

var i = 0;
if (int.TryParse("1", out i))  // Error
    Console.WriteLine(i);

To avoid this error, use variable declarations with out var.

if (int.TryParse("1", out var i))
    Console.WriteLine(i);

(Permission by attribute is also possible, although not recommended.)

For Statement

Reassignment of local variables is not inspected in the control of for statements.

for (var i = 0; i < 10; i += 2)  // OK
{
    i -= 1;  // Error
}

Code Fix

The code fix function (implemented in v2.0.0) can correct a no-reassignment error in two ways.

To prevent reassignment of local variable, a new local variable declaration can be added.

var local = 0;
Console.WriteLine(local);

-local = 1;
-Console.WriteLine(local);
+var local1 = 1;
+Console.WriteLine(local1);

References below the new variable declaration are automatically updated. Since the automatically generated identifier names are simplified, it is recommended that they be refactored to appropriate names.

Alternatively, reassignment can be allowed by adding an attribute.

+using ReadonlyLocalVariables;

+[ReassignableVariable("local")]
void Func()
{
    var local = 0;
    Console.WriteLine(local);
    
    local = 1;
    Console.WriteLine(local);
}

Code fix for arguments with out parameter modifiers can be done in the same way.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Added code fixer.