Skip to content

Commit

Permalink
BH0007
Browse files Browse the repository at this point in the history
  • Loading branch information
na1307 committed Jan 13, 2025
1 parent e5261ad commit 43cd85d
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 37 deletions.
260 changes: 257 additions & 3 deletions .editorconfig

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bluehill.Analyzers.CodeFixes\Bluehill.Analyzers.CodeFixes.csproj" />
<ProjectReference Include="..\Bluehill.Analyzers\Bluehill.Analyzers.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="tools\*.ps1" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath="" />
Expand Down
37 changes: 37 additions & 0 deletions Bluehill.Analyzers.Pages/Pages/BH0007.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@page "/BH0007"

<h1>BH0007: Prefer System.Threading.Lock over System.Object</h1>

<p>It is recommended to use System.Threading.Lock instead of System.Object for locking.</p>

<h2>Code with violation</h2>

<CodeHighlight>
public class TestClass {
private object? lockObj;

public void TestMethod() {
lockObj = new();

lock (lockObj) {
Console.WriteLine();
}
}
}
</CodeHighlight>

<h2>Fixed Code</h2>

<CodeHighlight>
public class TestClass {
private Lock? lockObj;

public void TestMethod() {
lockObj = new();

lock (lockObj) {
Console.WriteLine();
}
}
}
</CodeHighlight>
1 change: 1 addition & 0 deletions Bluehill.Analyzers.Pages/Pages/Rules.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<li><NavLink href="BH0002">BH0002</NavLink></li>
<li><NavLink href="BH0003">BH0003</NavLink></li>
<li><NavLink href="BH0004ToBH0006">BH0004, BH0005, BH0006</NavLink></li>
<li><NavLink href="BH0007">BH0007</NavLink></li>
</ul>
6 changes: 3 additions & 3 deletions Bluehill.Analyzers.Pages/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"net9.0": {
"Bluehill.Blazor": {
"type": "Direct",
"requested": "[0.8.1, )",
"resolved": "0.8.1",
"contentHash": "UlaWDS0MH66tq0vcje85eG5HBEHPoQ4vYeg+by54j4/dEYRaIYl5wpVnXoMUCYuCrH7Oe/hwC1aueo98PztHOw=="
"requested": "[0.9.0, )",
"resolved": "0.9.0",
"contentHash": "ze8fchl63EqohDUcMqKew94lf/pqMDVLOr5K23rUCjP+xc4XrQ1ZWYNAavEC0aB57fxQ6HXgO5mhlEFZNFtAyw=="
},
"Bluehill.Blazor.GHPages": {
"type": "Direct",
Expand Down
4 changes: 2 additions & 2 deletions Bluehill.Analyzers/BH0004ToBH0006Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private static void register(CompilationStartAnalysisContext context) {
var ixs = context.Compilation.GetTypeByMetadataName("System.Xml.Serialization.IXmlSerializable") ?? throw new InvalidOperationException("Something went wrong");
var ixsgs = (IMethodSymbol)ixs.GetMembers("GetSchema").Single();

context.RegisterSyntaxNodeAction(ac => analyzeMethod(ac, ixs, ixsgs), SyntaxKind.MethodDeclaration);
context.RegisterOperationAction(oc => analyzeInvocation(oc, ixs, ixsgs), OperationKind.Invocation);
context.RegisterSyntaxNodeAction(context => analyzeMethod(context, ixs, ixsgs), SyntaxKind.MethodDeclaration);
context.RegisterOperationAction(context => analyzeInvocation(context, ixs, ixsgs), OperationKind.Invocation);
}

// Analyze method declarations
Expand Down
54 changes: 54 additions & 0 deletions Bluehill.Analyzers/BH0007PreferLockAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace Bluehill.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class BH0007PreferLockAnalyzer : DiagnosticAnalyzer {
public const string DiagnosticId = "BH0007";
private const string category = "Performance";
private static readonly LocalizableString title =
new LocalizableResourceString(nameof(Resources.BH0007AnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString messageFormat =
new LocalizableResourceString(nameof(Resources.BH0007AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString description =
new LocalizableResourceString(nameof(Resources.BH0007AnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor rule =
new(DiagnosticId, title, messageFormat, category, DiagnosticSeverity.Warning, true, description, "https://na1307.github.io/Bluehill.Analyzers/BH0007");

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [rule];

public override void Initialize(AnalysisContext context) {
// Configure generated code analysis
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

// Enable concurrent execution
context.EnableConcurrentExecution();

// Register compilation start action
context.RegisterCompilationStartAction(compilationStartAction);
}

private static void compilationStartAction(CompilationStartAnalysisContext context) {
var lockType = context.Compilation.GetTypeByMetadataName("System.Threading.Lock");

// Lock is not available because it is .NET 8 or lower.
if (lockType is null) {
return;
}

var objectType = context.Compilation.GetTypeByMetadataName("System.Object")!;

context.RegisterOperationAction(context => lockOperationAction(context, objectType), OperationKind.Lock);
}

private static void lockOperationAction(OperationAnalysisContext context, INamedTypeSymbol objectType) {
var lockOperation = (ILockOperation)context.Operation;
var lockedType = lockOperation.LockedValue.Type!;

// Whether the locked type is System.Object
if (!SymbolEqualityComparer.Default.Equals(lockedType, objectType)) {
return;
}

// Report diagnostic
context.ReportDiagnostic(Diagnostic.Create(rule, lockOperation.LockedValue.Syntax.GetLocation()));
}
}
27 changes: 27 additions & 0 deletions Bluehill.Analyzers/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 36 additions & 27 deletions Bluehill.Analyzers/Resources.ko.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -177,4 +177,13 @@
<data name="BH0006AnalyzerTitle" xml:space="preserve">
<value>IXmlSerializable.GetSchema()를 절대 호출해서는 안됨</value>
</data>
<data name="BH0007AnalyzerDescription" xml:space="preserve">
<value>잠금에는 System.Object 대신 System.Threading.Lock을 사용하는 것이 좋습니다.</value>
</data>
<data name="BH0007AnalyzerMessageFormat" xml:space="preserve">
<value>System.Threading.Lock 사용</value>
</data>
<data name="BH0007AnalyzerTitle" xml:space="preserve">
<value>System.Object보다 System.Threading.Lock을 사용하세요</value>
</data>
</root>
9 changes: 9 additions & 0 deletions Bluehill.Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,13 @@
<data name="BH0006AnalyzerTitle" xml:space="preserve">
<value>Never call IXmlSerializable.GetSchema()</value>
</data>
<data name="BH0007AnalyzerDescription" xml:space="preserve">
<value>It is recommended to use System.Threading.Lock instead of System.Object for locking.</value>
</data>
<data name="BH0007AnalyzerMessageFormat" xml:space="preserve">
<value>Use System.Threading.Lock</value>
</data>
<data name="BH0007AnalyzerTitle" xml:space="preserve">
<value>Prefer System.Threading.Lock over System.Object</value>
</data>
</root>
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Bluehill.Blazor" Version="0.8.1" />
<PackageVersion Include="Bluehill.Blazor" Version="0.9.0" />
<PackageVersion Include="Bluehill.Blazor.GHPages" Version="1.0.2" />
<PackageVersion Include="HighlightBlazor" Version="0.1.10" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
Expand Down

0 comments on commit 43cd85d

Please sign in to comment.