Skip to content

Commit

Permalink
Activate and customize Roslyn rules (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Djoums authored May 19, 2024
1 parent 744ff1a commit 74c392e
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 79 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ There are several ways you can use the ecoCode analyzers in your .Net projects:

Both the EcoCode NuGet package and Visual Studio extension target .Net Standard 2.0 and can be used in a wide range of projects. See [Microsoft documentation](https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-0#select-net-standard-version) for details about the supported .Net Frameworks in .Net Standard 2.0.

🌿 Rules
🌿 _ecoCode__ Rules
-------------------

|Id|Description|Severity|Enabled|Code fix|
Expand All @@ -45,6 +45,16 @@ Both the EcoCode NuGet package and Visual Studio extension target .Net Standard
|[EC87](https://github.com/green-code-initiative/ecoCode/blob/main/ecocode-rules-specifications/src/main/rules/EC87/csharp/EC87.asciidoc)|Use collection indexer|⚠️|✔️|✔️|
|[EC88](https://github.com/green-code-initiative/ecoCode/blob/main/ecocode-rules-specifications/src/main/rules/EC88/csharp/EC88.asciidoc)|Dispose resource asynchronously|⚠️|✔️|✔️|

🌿 Roslyn rules
-------------------

Those are existing Roslyn rules that are customized by the _ecoCode_ analyzer.

|Id|Description|Severity|Enabled|Code fix|
|--|-----------|:------:|:--------:|:------:|
|[CA1813](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1813)|Avoid unsealed attributes|⚠️|✔️|✔️|
|[CA1860](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1860)|Avoid using 'Enumerable.Any()' extension method|⚠️|✔️|✔️|

🤝 Contribution
---------------

Expand Down
7 changes: 7 additions & 0 deletions assets/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# EcoCode published .editorconfig file to include and customize existing Roslyn rules

root = true

[*.cs]
dotnet_diagnostic.CA1813.severity = warning
dotnet_diagnostic.CA1860.severity = warning
1 change: 1 addition & 0 deletions ecoCode-csharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflows", "Workflows", "{
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Assets", "Assets", "{D10E2FE2-04EB-4F54-90E6-2E0BBA562041}"
ProjectSection(SolutionItems) = preProject
assets\.editorconfig = assets\.editorconfig
CONTRIBUTING.md = CONTRIBUTING.md
icon.jpeg = icon.jpeg
LICENCE.md = LICENCE.md
Expand Down
4 changes: 3 additions & 1 deletion src/EcoCode.Core/Analyzers/EC81.SpecifyStructLayout.Fixer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace EcoCode.Analyzers;
using System.Runtime.InteropServices;

namespace EcoCode.Analyzers;

/// <summary>EC81 fixer: Use struct layout.</summary>
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SpecifyStructLayoutFixer)), Shared]
Expand Down
6 changes: 0 additions & 6 deletions src/EcoCode.Core/Extensions/CompilationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
/// <summary>Extension methods for <see cref="Compilation"/>.</summary>
public static class CompilationExtensions
{
/// <summary>Returns the LINQ Enumerable symbol.</summary>
/// <param name="compilation">The compilation.</param>
/// <returns>The LINQ Enumerable symbol, null if not found.</returns>
public static INamedTypeSymbol? GetLinqEnumerableSymbol(this Compilation compilation) =>
compilation.GetTypeByMetadataName(typeof(System.Linq.Enumerable).FullName);

/// <summary>
/// Gets a type by its metadata name to use for code analysis within a <see cref="Compilation"/>. This method
/// attempts to find the "best" symbol to use for code analysis, which is the symbol matching the first of the
Expand Down
21 changes: 0 additions & 21 deletions src/EcoCode.Core/Extensions/MethodSymbolExtension.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/EcoCode.Core/Extensions/SyntaxNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,4 @@ public static SyntaxList<StatementSyntax> GetLoopStatements(this SyntaxNode node
_ => default,
};
}

/// <summary>Returns the node with a given leading trivia, or the node directly if the trivia is the same.</summary>
/// <param name="node">The node.</param>
/// <param name="trivia">The trivia.</param>
/// <returns>The node with the leading trivia.</returns>
public static TSyntaxNode WithLeadingTriviaIfDifferent<TSyntaxNode>(this TSyntaxNode node, SyntaxTriviaList trivia)
where TSyntaxNode : SyntaxNode =>
node.GetLeadingTrivia() == trivia ? node : node.WithLeadingTrivia(trivia);

/// <summary>Returns the node with a given trailing trivia, or the node directly if the trivia is the same.</summary>
/// <param name="node">The node.</param>
/// <param name="trivia">The trivia.</param>
/// <returns>The node with the trailing trivia.</returns>
public static TSyntaxNode WithTrailingTriviaIfDifferent<TSyntaxNode>(this TSyntaxNode node, SyntaxTriviaList trivia)
where TSyntaxNode : SyntaxNode =>
node.GetTrailingTrivia() == trivia ? node : node.WithTrailingTrivia(trivia);
}
33 changes: 0 additions & 33 deletions src/EcoCode.Core/Extensions/SyntaxTokenExtensions.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/EcoCode.Core/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
global using Microsoft.CodeAnalysis.Simplification;
global using System.Collections.Generic;
global using System.Collections.Immutable;
global using System.Runtime.InteropServices;
global using System.Composition;
global using System.Threading;
global using System.Threading.Tasks;
2 changes: 2 additions & 0 deletions src/EcoCode.Package/EcoCode.Package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<DevelopmentDependency>true</DevelopmentDependency>
<NoPackageAnalysis>true</NoPackageAnalysis>
<RestoreUseStaticGraphEvaluation>true</RestoreUseStaticGraphEvaluation>
<NoDefaultExcludes>true</NoDefaultExcludes> <!-- Required to include .editorconfig in the package -->
</PropertyGroup>

<PropertyGroup>
Expand All @@ -36,6 +37,7 @@
<Content Include="..\..\icon.jpeg" Pack="true" PackagePath="" CopyToOutputDirectory="PreserveNewest" />
<Content Include="..\..\NOTICE.md" Pack="true" PackagePath="" CopyToOutputDirectory="PreserveNewest" />
<Content Include="..\..\README.md" Pack="true" PackagePath="" CopyToOutputDirectory="PreserveNewest" />
<Content Include="..\..\assets\.editorconfig" Pack="true" PackagePath="content/Rules/.editorconfig" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/EcoCode.Vsix/EcoCode.Vsix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Content Include="..\..\LICENCE.md" CopyToOutputDirectory="PreserveNewest" IncludeInVSIX="true" />
<Content Include="..\..\NOTICE.md" CopyToOutputDirectory="PreserveNewest" IncludeInVSIX="true" />
<Content Include="..\..\README.md" CopyToOutputDirectory="PreserveNewest" IncludeInVSIX="true" />
<Content Include="..\..\assets\.editorconfig" CopyToOutputDirectory="PreserveNewest" IncludeInVSIX="true" Link="content\Rules\.editorconfig" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/EcoCode.Vsix/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Assets>
<Asset Type="Microsoft.VisualStudio.Analyzer" d:Source="Project" d:ProjectName="EcoCode.Core" Path="|EcoCode.Core|"/>
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="EcoCode.Core" Path="|EcoCode.Core|"/>
<Asset Type="Microsoft.VisualStudio.EditorConfig" d:Source="File" Path="content/Rules/.editorconfig" />
</Assets>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[17.0,18.0)" DisplayName="Visual Studio core editor" />
Expand Down

0 comments on commit 74c392e

Please sign in to comment.