Home

Awesome

EcoCode on NuGet EcoCode on NuGet

EcoCode-C#

ecoCode is a collective project aiming to reduce environmental footprint of software at the code level. The goal of the project is to provide a list of static code analyzers to highlight code structures that may have a negative ecological impact: energy and resources over-consumption, "fatware", shortening terminals' lifespan, etc.

ecoCode is based on evolving catalogs of good practices, for various technologies. This set of Roslyn analyzers implements these catalogs as rules for scanning your C# projects.

License: GPL v3 Contributor Covenant

🚀 Getting Started

There are several ways you can use the ecoCode C# analyzers in your .Net projects:

  1. As a NuGet package
  2. As a .NET tool
  3. As an analyzer for SonarQube.

<a name="nugetPackage"></a>🧩 NuGet package

The package is available on nuget.org at this address : https://www.nuget.org/packages/EcoCode, and can be added to your projects/solutions like any NuGet package. Once referenced and restored, the ecoCode analyzers are automatically integrated in your IDE/compilation process, and will list any applicable info/alert.

Pre-requisite : .Net Standard 2.0, which can be used in a wide range of projects. See Microsoft documentation for details about the supported Frameworks.

<a name="dotnetTool"></a>🧩 .Net tool

The .Net tool is available on nuget.org at this address : https://www.nuget.org/packages/EcoCode.Tool, and can be fetched on your machine using the following command :

dotnet tool install --global EcoCode.Tool

See .Net tools documentation for additional information.

Once installed, you can launch an analyzis on an existing codebase like this :

ecocode-cli analyze path/to/mySolution.sln path/to/myReport.html.

The file to analyze can be a .sln, a .slnx or a .csproj. The report format depends on it's required extension, the following are currently supported : .html, .json and .csv.

Pre-requisite : .Net 8 SDK.

<a name="sonarQube"></a>🧩 Analyzer for SonarQube

EcoCode C# can use SonarScanner for .Net to integrate with SonarQube, and uses a custom import addition to enrich what is reported to Sonar (severity, description, url page, category, and so on). See our dedicated repository for more information.

🌿 EcoCode Rules

IdDescriptionSeverityCode fix
EC69Don’t call loop invariant functions in loop conditions⚠️
EC72Don’t execute SQL queries in loops⚠️
EC75Don’t concatenate strings in loops⚠️
EC81Specify struct layouts⚠️✔️
EC82Variable can be made constantℹ️✔️
EC83Replace Enum ToString() with nameof⚠️✔️
EC84Avoid async void methods⚠️✔️
EC85Make type sealedℹ️✔️
EC86GC.Collect should not be called⚠️
EC87Use collection indexer⚠️✔️
EC88Dispose resource asynchronously⚠️✔️
EC91Use Where before OrderBy⚠️✔️
EC92Use Length to test empty strings⚠️✔️
EC93Return Task directlyℹ️✔️

🌿 Customized Roslyn Rules

EcoCode C# customizes the severity of the following native Roslyn rules.

IdDescriptionOld SeverityNew Severity
CA1001Types that own disposable fields should be disposable💤⚠️
CA1802Use Literals Where Appropriate💤⚠️
CA1805Do not initialize unnecessarily💤⚠️
CA1813Avoid unsealed attributes💤⚠️
CA1816Call GC.SuppressFinalize correctlyℹ️⚠️
CA1821Remove empty finalizersℹ️⚠️
CA1822Mark members as staticℹ️⚠️
CA1824Mark assemblies with NeutralResourcesLanguageAttributeℹ️⚠️
CA1825Avoid zero-length array allocationsℹ️⚠️
CA1826Use property instead of Linq Enumerable methodℹ️⚠️
CA1827Do not use Count()/LongCount() when Any() can be usedℹ️⚠️
CA1828Do not use CountAsync/LongCountAsync when AnyAsync can be usedℹ️⚠️
CA1829Use Length/Count property instead of Enumerable.Count methodℹ️⚠️
CA1830Prefer strongly-typed Append and Insert method overloads on StringBuilderℹ️⚠️
CA1832Use AsSpan or AsMemory instead of Range-based indexers for getting ReadOnlySpan or ReadOnlyMemory portion of an arrayℹ️⚠️
CA1833Use AsSpan or AsMemory instead of Range-based indexers for getting Span or Memory portion of an arrayℹ️⚠️
CA1834Use StringBuilder.Append(char) for single character stringsℹ️⚠️
CA1835Prefer the memory-based overloads of ReadAsync/WriteAsync methods in stream-based classesℹ️⚠️
CA1836Prefer IsEmpty over Count when availableℹ️⚠️
CA1837Use Environment.ProcessId instead of Process.GetCurrentProcess().Idℹ️⚠️
CA1838Avoid StringBuilder parameters for P/Invokes💤⚠️
CA1839Use Environment.ProcessPath instead of Process.GetCurrentProcess().MainModule.FileNameℹ️⚠️
CA1840Use Environment.CurrentManagedThreadId instead of Thread.CurrentThread.ManagedThreadIdℹ️⚠️
CA1841Prefer Dictionary Contains methodsℹ️⚠️
CA1842Do not use 'WhenAll' with a single taskℹ️⚠️
CA1843Do not use 'WaitAll' with a single taskℹ️⚠️
CA1844Provide memory-based overrides of async methods when subclassing 'Stream'ℹ️⚠️
CA1845Use span-based 'string.Concat'ℹ️⚠️
CA1846Prefer AsSpan over Substringℹ️⚠️
CA1847Use String.Contains(char) instead of String.Contains(string) with single charactersℹ️⚠️
CA1850Prefer static HashData method over ComputeHashℹ️⚠️
CA1853Unnecessary call to 'Dictionary.ContainsKey(key)'ℹ️⚠️
CA1854Prefer the IDictionary.TryGetValue(TKey, out TValue) methodℹ️⚠️
CA1855Use Span<T>.Clear() instead of Span<T>.Fill()ℹ️⚠️
CA1858Use StartsWith instead of IndexOfℹ️⚠️
CA1859Prefer concrete types when possible for improved performanceℹ️⚠️
CA1860Avoid using 'Enumerable.Any()' extension methodℹ️⚠️
CA1863Use 'CompositeFormat'💤⚠️
CA1864Prefer the 'IDictionary.TryAdd(TKey, TValue)' methodℹ️⚠️
CA1865-7Use 'string.Method(char)' instead of 'string.Method(string)' for string with single charℹ️⚠️
CA1868Unnecessary call to 'Contains' for setsℹ️⚠️
CA1869Cache and reuse 'JsonSerializerOptions' instancesℹ️⚠️
CA1870Use a cached 'SearchValues' instanceℹ️⚠️
CA1871Do not pass a nullable struct to 'ArgumentNullException.ThrowIfNull'ℹ️⚠️
CA1872Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'ℹ️⚠️
CA2009Do not call ToImmutableCollection on an ImmutableCollection valueℹ️⚠️
CA2215Dispose methods should call base class dispose💤⚠️
CA2218Override GetHashCode on overriding Equalsℹ️⚠️
CA2251Use String.Equals over String.Compare💤⚠️
CA2264Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull'ℹ️⚠️

🤝 Contribution

See contribution on the central ecoCode repository.

🤓 Main contributors

See main contributors on the central ecoCode repository.