-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
427 additions
and
37 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters